diff --git a/cmd/install.go b/cmd/install.go index 3d32b3b..c32684f 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -4,10 +4,8 @@ Copyright © 2022 Tiger Wang package cmd import ( - "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" "github.com/spf13/cobra" @@ -30,11 +28,20 @@ var installCmd = &cobra.Command{ os.Exit(1) } - execCmd := exec.Command("go", "install", packageName) - execCmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%s", installPath)) - execCmd.Stdout = os.Stdout - execCmd.Stderr = os.Stderr - execCmd.Dir = os.TempDir() + execCmd := common.GoInstallCmd(packageName, installPath) + + defer func() { + if _, err := os.Stat(installPath); err != nil { + return + } + + _logger.Info("good: cleaning up mod cache at %s...\n", installPath) + + execCmd = common.GoCleanModCacheCmd(installPath) + if err := execCmd.Run(); err != nil { + _logger.Debug(err.Error()) + } + }() _logger.Info("good: installing to %s...\n", installPath) if err := execCmd.Run(); err != nil { diff --git a/common/exec.go b/common/exec.go new file mode 100644 index 0000000..782b78c --- /dev/null +++ b/common/exec.go @@ -0,0 +1,26 @@ +package common + +import ( + "fmt" + "os" + "os/exec" +) + +func GoInstallCmd(packageName, installPath string) *exec.Cmd { + execCmd := exec.Command("go", "install", packageName) + execCmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%s", installPath)) + execCmd.Stdout = os.Stdout + execCmd.Stderr = os.Stderr + execCmd.Dir = os.TempDir() + + return execCmd +} + +func GoCleanModCacheCmd(installPath string) *exec.Cmd { + execCmd := exec.Command("go", "clean", "-modcache") + execCmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%s", installPath)) + execCmd.Stdout = os.Stdout + execCmd.Stderr = os.Stderr + execCmd.Dir = os.TempDir() + return execCmd +}