-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
183 lines (171 loc) · 4.36 KB
/
cli.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cli
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/harrybrwn/govm"
)
// Variables that are set using ldflags
var (
version string
commit string
built string
completion = "false"
)
func NewRootCmd() *cobra.Command {
var conf = govm.NewDefaultManager()
c := &cobra.Command{
Use: "govm",
Short: "Manage different versions of Go",
Long: "Manage different versions of Go",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(*cobra.Command, []string) error {
return nil
},
Version: fmt.Sprintf("%s %s built %s", version, commit, built),
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: completion != "true", // disable 'completion' when generating docs
},
}
c.AddCommand(
newUseCmd(&conf),
newListCmd(&conf),
newDownloadCmd(&conf),
newRemoveCmd(&conf),
newUninstallCmd(&conf),
newEnvCmd(&conf),
)
return c
}
func newUseCmd(conf *govm.Manager) *cobra.Command {
c := &cobra.Command{
Use: "use <version>",
Short: "Switch to a specified version of Go",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
versions, err := conf.List()
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
versionStrings := make([]string, len(versions))
for i, v := range versions {
versionStrings[i] = v.String()
}
return versionStrings, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
var toUse string
if len(args) == 0 {
v, err := govm.ReadVersionFile(conf.VersionFile)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf(
"give version number or use %q file",
conf.VersionFile,
)
}
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "using version from %q\n", conf.VersionFile)
toUse = v.String()
} else {
toUse = cleanVersionInput(args[0])
}
return conf.Use(toUse)
},
}
return c
}
func newListCmd(m *govm.Manager) *cobra.Command {
c := &cobra.Command{
Use: "list",
Short: "List all the installed versions of go",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, _ []string) error {
names, err := m.List()
if err != nil {
return err
}
for _, name := range names {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", name.String())
}
return nil
},
}
return c
}
func newDownloadCmd(conf *govm.Manager) *cobra.Command {
var alsoUse bool
c := &cobra.Command{
Use: "download <version>",
Short: "Download a different version of Go",
Aliases: []string{"dl"},
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
v := cleanVersionInput(args[0])
err := conf.Download(cmd.OutOrStdout(), v)
if err != nil {
return err
}
if alsoUse {
return conf.Use(v)
}
return nil
},
ValidArgsFunction: func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
versions, err := govm.GetGoVersions()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return versions, cobra.ShellCompDirectiveNoFileComp
},
}
c.Flags().BoolVar(&alsoUse, "use", alsoUse, "set this version after downloading it")
return c
}
func newRemoveCmd(conf *govm.Manager) *cobra.Command {
c := &cobra.Command{
Use: "remove <version>",
Aliases: []string{"rm"},
Short: "Remove an installation.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
v := cleanVersionInput(args[0])
return os.RemoveAll(conf.Installation(v))
},
}
return c
}
func newUninstallCmd(conf *govm.Manager) *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Remove all go versions installed",
RunE: func(cmd *cobra.Command, args []string) error {
return conf.Uninstall()
},
}
}
func newEnvCmd(conf *govm.Manager) *cobra.Command {
return &cobra.Command{
Use: "env",
Short: "Print shell variables needed to for govm to manage your go versions.",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprintf(
cmd.OutOrStdout(),
"export GOROOT=\"%s\"\n",
filepath.Join(conf.Base, conf.GoDir),
)
return nil
},
}
}
func cleanVersionInput(in string) string {
if in[0] == 'v' {
in = in[1:]
}
in = strings.TrimPrefix(in, "go")
return in
}