Skip to content

Commit

Permalink
prune
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 5, 2022
1 parent e14e23b commit 5f89ec8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/gobrew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var allowedArgs = []string{
"install",
"use",
"uninstall",
"prune",
"version",
"self-update",
}
Expand Down Expand Up @@ -72,6 +73,8 @@ func main() {
gb.Use(versionArg)
case "uninstall":
gb.Uninstall(versionArg)
case "prune":
gb.Prune()
case "version":
gb.Version(version)
case "self-update":
Expand Down Expand Up @@ -123,6 +126,7 @@ Usage:
gobrew uninstall <version> Uninstall <version>
gobrew list List installed versions
gobrew self-update Self update this tool
gobrew prune Uninstall all go versions except current version
gobrew version Show gobrew version
gobrew help Show this message
Expand Down
30 changes: 28 additions & 2 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type Command interface {
Uninstall(version string)
Install(version string)
Use(version string)
Version(version string)
Upgrade(version string)
Prune()
Version(currentVersion string)
Upgrade(currentVersion string)
Helper
}

Expand Down Expand Up @@ -85,6 +86,31 @@ func (gb *GoBrew) getArch() string {
return runtime.GOOS + "-" + runtime.GOARCH
}

// Prune removes all installed versions of go execept current version
func (gb *GoBrew) Prune() error {
currentVersion := gb.CurrentVersion()
utils.Infof("[Info] Current version: %s \n", currentVersion)

entries, err := os.ReadDir(gb.versionsDir)
utils.CheckError(err, "[Error]: List versions failed")
files := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
utils.CheckError(err, "[Error]: List versions failed")
files = append(files, info)
}

for _, f := range files {
if f.Name() != currentVersion {
version := f.Name()
utils.Infof("[Info] Uninstalling version: %s \n", version)
gb.Uninstall(version)
utils.Infof("[Info] Uninstalled version: %s \n", version)
}
}
return nil
}

// ListVersions that are installed by dir ls
// highlight the version that is currently symbolic linked
func (gb *GoBrew) ListVersions() error {
Expand Down

0 comments on commit 5f89ec8

Please sign in to comment.