Skip to content

Commit

Permalink
add go clean -modcache after each install, to remove mod cache that…
Browse files Browse the repository at this point in the history
… is no longer needed (#19)

Signed-off-by: Tiger Wang <tigerwang@outlook.com>
  • Loading branch information
tigerinus authored May 31, 2023
1 parent 2295a47 commit 4b3bb8b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
21 changes: 14 additions & 7 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ Copyright © 2022 Tiger Wang <tiger@tensorsmart.com>
package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions common/exec.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4b3bb8b

Please sign in to comment.