From 1ef824c0b4cbe36572354243fbfbd5ec824ff2e8 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Mon, 27 Sep 2021 01:45:02 -0400 Subject: [PATCH] Fix go generate core/format.go (#725) --- core/format.go | 2 +- infra/vformat/main.go | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/format.go b/core/format.go index 439f5ada..99dbff47 100644 --- a/core/format.go +++ b/core/format.go @@ -1,4 +1,4 @@ package core //go:generate go install -v golang.org/x/tools/cmd/goimports@latest -//go:generate go run ../infra/vformat/ +//go:generate go run ../infra/vformat/main.go -pwd ./.. diff --git a/infra/vformat/main.go b/infra/vformat/main.go index e6c50d32..0d837b49 100644 --- a/infra/vformat/main.go +++ b/infra/vformat/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "go/build" "io/ioutil" @@ -11,6 +12,8 @@ import ( "strings" ) +var directory = flag.String("pwd", "", "Working directory of Xray vformat.") + // envFile returns the name of the Go environment configuration file. // Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166 func envFile() (string, error) { @@ -106,12 +109,22 @@ func RunMany(binary string, args, files []string) { } func main() { - pwd, err := os.Getwd() - if err != nil { - fmt.Println("Can not get current working directory.") - os.Exit(1) + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of vformat:\n") + flag.PrintDefaults() + } + flag.Parse() + + if !filepath.IsAbs(*directory) { + pwd, wdErr := os.Getwd() + if wdErr != nil { + fmt.Println("Can not get current working directory.") + os.Exit(1) + } + *directory = filepath.Join(pwd, *directory) } + pwd := *directory GOBIN := GetGOBIN() binPath := os.Getenv("PATH") pathSlice := []string{pwd, GOBIN, binPath} @@ -140,7 +153,7 @@ func main() { } rawFilesSlice := make([]string, 0) - walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error { + walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) return err