Skip to content

Commit

Permalink
feat(gvm): improve usage
Browse files Browse the repository at this point in the history
feat(gvm): improve usage
  • Loading branch information
jaronnie committed Oct 9, 2024
1 parent d27496f commit 3b028d2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
12 changes: 9 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ linters:
- gofumpt
- tparallel
- unconvert
- unparam
- wastedassign
- revive
- tagliatelle
- misspell

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
25 changes: 22 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ 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 {
return err
}

if !bytes.Contains(shellRcData, []byte("gvm shell setup")) {
_, err = shellConfigfile.Write([]byte(SetUpGVMInUnix))
_, err = shellConfigFile.Write([]byte(SetUpGVMInUnix))
if err != nil {
return err
}
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions utilx/env.go
Original file line number Diff line number Diff line change
@@ -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 ""
}

0 comments on commit 3b028d2

Please sign in to comment.