Skip to content

Commit 8797818

Browse files
committed
Upgrade only previously installed tools. Minor refactor to Upgrade() for better portability
1 parent f8df2d8 commit 8797818

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

cmd/upgrade/upgrade.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,42 @@ func Cmd() *cobra.Command {
1919
ValidArgs: append(toolMap.Names(), "all"),
2020
Short: "Upgrade an existing tool",
2121
Long: "Upgrades one or more tools from the provided list. It's valid to specify multiple tools: in this case, all tools provided will be upgraded. If no specific tools are provided, all are (installed and) upgraded by default.",
22-
RunE: func(cmd *cobra.Command, args []string) error {
23-
return run(cmd, args, toolMap)
22+
RunE: func(_ *cobra.Command, args []string) error {
23+
if len(args) == 0 || utils.Contains(args, "all") {
24+
// If user explicitly passes 'all' or doesn't specify which tools to install,
25+
// upgrade everything that's been installed locally
26+
installedTools, err := tool.ListInstalled()
27+
if err != nil {
28+
return err
29+
}
30+
args = []string{}
31+
for _, installedTool := range installedTools {
32+
args = append(args, installedTool.Name())
33+
}
34+
}
35+
return Upgrade(args)
2436
},
2537
}
2638
return upgradeCmd
2739
}
2840

29-
func run(cmd *cobra.Command, args []string, toolMap tool.Map) error {
30-
if len(args) == 0 || utils.Contains(args, "all") {
31-
// If user doesn't specify, or explicitly passes 'all', upgrade all the things
32-
args = toolMap.Names()
41+
42+
// Upgrade upgrades the provided tools to their latest versions
43+
func Upgrade(tools []string) error {
44+
toolMap := tool.GetMap()
45+
46+
upgradeList := []tool.Tool{}
47+
for _, toolName := range tools {
48+
t, found := toolMap[toolName]
49+
if !found {
50+
return fmt.Errorf("failed to locate '%s' in list of supported tools", toolName)
51+
}
52+
upgradeList = append(upgradeList, t)
3353
}
3454

3555
fmt.Println("Upgrading the following tools: ")
36-
upgradeList := []tool.Tool{}
37-
for _, toolName := range args {
38-
fmt.Printf("- %s\n", toolName)
39-
upgradeList = append(upgradeList, toolMap[toolName])
56+
for _, t := range upgradeList {
57+
fmt.Printf("- %s\n", t.Name())
4058
}
4159

4260
err := tool.Install(upgradeList)

pkg/tool/tool.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,24 @@ func RemoveInstallDir() error {
193193
}
194194
return os.RemoveAll(installDir)
195195
}
196+
197+
// ListInstalled returns a slice containing all tools the current machine has installed
198+
func ListInstalled() ([]Tool, error) {
199+
tools := GetMap()
200+
installedTools := []Tool{}
201+
installDir, err := InstallDir()
202+
if err != nil {
203+
return installedTools, err
204+
}
205+
206+
for _, tool := range tools {
207+
installed, err := tool.Installed(installDir)
208+
if err != nil {
209+
return installedTools, err
210+
}
211+
if installed {
212+
installedTools = append(installedTools, tool)
213+
}
214+
}
215+
return installedTools, nil
216+
}

0 commit comments

Comments
 (0)