-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcommands.go
83 lines (74 loc) · 2.32 KB
/
subcommands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/json"
"fmt"
"github.com/Houndie/toolbox/pkg/toolbox"
"github.com/spf13/cobra"
)
var doCommand = &cobra.Command{
Use: "do <command>",
Short: "Run a command using the vendored version of tools",
Long: "Edits the PATH to reflect the tool vendor directly, and runs the given command.",
RunE: func(cmd *cobra.Command, args []string) error {
err := toolbox.DoOpts(args, makeOptions()...)
if err == nil {
return nil
}
return err
},
}
var addCommand = &cobra.Command{
Use: "add <dependency> [version]",
Short: "Add a new dependency",
Long: "Adds dependency to the list of dependencies managed by toolbox. If a version is provided, adds that version as well.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return toolbox.AddVer(args[0], args[1], makeOptions()...)
}
return toolbox.Add(args[0], makeOptions()...)
},
}
var removeCommand = &cobra.Command{
Use: "remove <dependency>",
Short: "Remove a dependency",
Long: "Removes a dependency, and attempts to remove the executable of the same name as well.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return toolbox.Remove(args[0], makeOptions()...)
},
}
var syncCommand = &cobra.Command{
Use: "sync",
Short: "Make sure all dependencies are at the correct version",
Long: "Uses go install to install all of our dependencies. Installs from module cache if they are found, from the internet if not.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return toolbox.Sync(makeOptions()...)
},
}
var listCommand = &cobra.Command{
Use: "list",
Short: "Lists all tool dependencies and their information",
Long: "Parses tools.go and go.mod, and prints the information in easy to parse json",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
tools, err := toolbox.List(makeOptions()...)
if err != nil {
return err
}
j, err := json.MarshalIndent(&tools, "", "\t")
if err != nil {
return fmt.Errorf("error marshalling tools to json: %w", err)
}
fmt.Println(string(j))
return nil
},
}
func init() {
rootCmd.AddCommand(doCommand)
rootCmd.AddCommand(addCommand)
rootCmd.AddCommand(removeCommand)
rootCmd.AddCommand(syncCommand)
rootCmd.AddCommand(listCommand)
}