From 3b028d23cb5e41bd89c6030792a7e1b50b6304db Mon Sep 17 00:00:00 2001 From: jaron Date: Wed, 9 Oct 2024 17:49:10 +0800 Subject: [PATCH] feat(gvm): improve usage feat(gvm): improve usage --- .golangci.yml | 12 +++++++++--- cmd/init.go | 25 ++++++++++++++++++++++--- cmd/root.go | 8 ++++++++ utilx/env.go | 27 +++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 utilx/env.go diff --git a/.golangci.yml b/.golangci.yml index a571dbc..38e19b8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,8 +6,14 @@ linters: - gofumpt - tparallel - unconvert - - unparam - wastedassign - - revive - tagliatelle - - misspell \ No newline at end of file + +linters-settings: + gofumpt: + # Module path which contains the source code being formatted. + # Default: "" + module-path: github.com/jaronnie/gvm + # Choose whether to use the extra rules. + # Default: false + extra-rules: true \ No newline at end of file diff --git a/cmd/init.go b/cmd/init.go index af331c7..1d2c053 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -68,11 +68,11 @@ func initx(cmd *cobra.Command, args []string) error { shellRcFile = filepath.Join(global.HomeDir, ".zshrc") } - shellConfigfile, err := os.OpenFile(shellRcFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o744) + shellConfigFile, err := os.OpenFile(shellRcFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o744) if err != nil { return err } - defer shellConfigfile.Close() + defer shellConfigFile.Close() shellRcData, err := os.ReadFile(shellRcFile) if err != nil { @@ -80,7 +80,7 @@ func initx(cmd *cobra.Command, args []string) error { } if !bytes.Contains(shellRcData, []byte("gvm shell setup")) { - _, err = shellConfigfile.Write([]byte(SetUpGVMInUnix)) + _, err = shellConfigFile.Write([]byte(SetUpGVMInUnix)) if err != nil { return err } @@ -104,6 +104,25 @@ func initx(cmd *cobra.Command, args []string) error { return err } + // cp gvm exec binary to $HOME/gvm/bin + path, _ := utilx.LookPath(rootCmd.Use) + if path == "" { + // 如果找不到 gvm, 则复制当前二进制文件到 $HOME/gvm/bin + fileStat, err := os.Stat(os.Args[0]) + if err != nil { + return err + } + file, err := os.ReadFile(os.Args[0]) + if err != nil { + return err + } + _ = os.MkdirAll(filepath.Join(global.GvmConfigDir, "bin"), 0o755) + err = os.WriteFile(filepath.Join(global.GvmConfigDir, "bin", "gvm"), file, fileStat.Mode()) + if err != nil { + return err + } + } + fmt.Printf("🚀please exec `source %s` to activate gvm\n", shellRcFile) return nil diff --git a/cmd/root.go b/cmd/root.go index 0ef5fdd..4cb63c2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -85,6 +86,13 @@ func initGlobalValue() { panic(err) } + // check + if stat, err := os.Stat(filepath.Join(homeDir, rootCmd.Use)); err == nil { + if !stat.IsDir() { + panic("please make sure $HOME/gvm is a dir") + } + } + global.HomeDir = homeDir global.GvmConfigDir = fmt.Sprintf("%s/%s", homeDir, GVM) global.GvmConfigFile = fmt.Sprintf("%s/%s/config.toml", homeDir, GVM) diff --git a/utilx/env.go b/utilx/env.go new file mode 100644 index 0000000..2d5ca64 --- /dev/null +++ b/utilx/env.go @@ -0,0 +1,27 @@ +package utilx + +import ( + "os/exec" + "runtime" + "strings" +) + +func LookPath(xBin string) (string, error) { + suffix := getExeSuffix() + if len(suffix) > 0 && !strings.HasSuffix(xBin, suffix) { + xBin = xBin + suffix + } + + bin, err := exec.LookPath(xBin) + if err != nil { + return "", err + } + return bin, nil +} + +func getExeSuffix() string { + if runtime.GOOS == "windows" { + return ".exe" + } + return "" +}