/
cli.go
330 lines (304 loc) · 9.09 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Package cli implements the command-line interface of UPM.
package cli
import (
"fmt"
"os"
"github.com/replit/upm/internal/backends"
"github.com/replit/upm/internal/config"
"github.com/replit/upm/internal/util"
"github.com/spf13/cobra"
)
// parseOutputFormat takes "table" or "json" and returns an
// outputFormat enum value.
func parseOutputFormat(formatStr string) outputFormat {
switch formatStr {
case "table":
return outputFormatTable
case "json":
return outputFormatJSON
default:
util.Die(`Error: invalid format %#v (must be "table" or "json")`, formatStr)
return 0
}
}
// version is set at build time to a Git tag or the string
// "development version" when not tagging a release.
var version = "unknown version"
// getVersion returns a string that can be printed when calling 'upm
// --version'.
func getVersion() string {
return "upm " + version
}
// DoCLI reads the command-line arguments and runs the appropriate
// code, then exits the process (or returns to indicate normal exit).
func DoCLI() {
backends.SetupAll()
var language string
var formatStr string
var guess bool
var forceLock bool
var forceInstall bool
var forceGuess bool
var all bool
var ignoredPackages []string
var ignoredPaths []string
var upgrade bool
var name string
cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: "upm",
Version: getVersion(),
}
rootCmd.SetVersionTemplate(`{{.Version}}` + "\n")
// Not sorting the root command options because none of the
// documented ways to disable sorting work for it (the root
// command itself has the options sorted correctly, but they
// are alphabetized in the help strings for subcommands).
rootCmd.PersistentFlags().StringVarP(
&language, "lang", "l", "", "specify project language(s) manually",
)
rootCmd.PersistentFlags().BoolVarP(
&config.Quiet, "quiet", "q", false, "don't show what commands are being run",
)
rootCmd.PersistentFlags().StringSliceVar(
&ignoredPackages, "ignored-packages", []string{},
"packages to ignore when guessing or adding (comma-separated)",
)
rootCmd.PersistentFlags().StringSliceVar(
&ignoredPaths, "ignored-paths", []string{},
"paths to ignore when guessing (comma-separated)",
)
rootCmd.PersistentFlags().BoolP(
"help", "h", false, "display command-line usage",
)
rootCmd.PersistentFlags().BoolP(
"version", "v", false, "display command version",
)
cmdWhichLanguage := &cobra.Command{
Use: "which-language",
Short: "Query language autodetection",
Long: "Ask which language your project is autodetected as",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runWhichLanguage(language)
},
}
rootCmd.AddCommand(cmdWhichLanguage)
cmdListLanguages := &cobra.Command{
Use: "list-languages",
Short: "List supported languages",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runListLanguages()
},
}
rootCmd.AddCommand(cmdListLanguages)
cmdSearch := &cobra.Command{
Use: "search QUERY...",
Short: "Search for packages online",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
queries := args
outputFormat := parseOutputFormat(formatStr)
runSearch(language, queries, outputFormat)
},
}
cmdSearch.Flags().SortFlags = false
cmdSearch.Flags().StringVarP(
&formatStr, "format", "f", "table", `output format ("table" or "json")`,
)
rootCmd.AddCommand(cmdSearch)
cmdInfo := &cobra.Command{
Aliases: []string{"show"},
Use: "info PACKAGE",
Short: "Show package information from online registry",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkg := args[0]
outputFormat := parseOutputFormat(formatStr)
runInfo(language, pkg, outputFormat)
},
}
cmdInfo.Flags().SortFlags = false
cmdInfo.Flags().StringVarP(
&formatStr, "format", "f", "table", `output format ("table" or "json")`,
)
rootCmd.AddCommand(cmdInfo)
cmdAdd := &cobra.Command{
Use: `add "PACKAGE[ SPEC]"...`,
Short: "Add packages to the specfile",
Run: func(cmd *cobra.Command, args []string) {
pkgSpecStrs := args
runAdd(language, pkgSpecStrs, upgrade, guess, forceGuess,
ignoredPackages, forceLock, forceInstall, name)
},
}
cmdAdd.Flags().SortFlags = false
cmdAdd.Flags().BoolVarP(
&upgrade, "upgrade", "u", false, "upgrade all packages to latest allowed versions",
)
cmdAdd.Flags().BoolVarP(
&guess, "guess", "g", false, "guess additional packages to add",
)
cmdAdd.Flags().BoolVarP(
&forceLock, "force-lock", "f", false, "rewrite lockfile even if up to date",
)
cmdAdd.Flags().BoolVarP(
&forceInstall, "force-install", "F", false, "reinstall packages even if up to date",
)
cmdAdd.Flags().BoolVar(
&forceGuess, "force-guess", false, "bypass cache when guessing dependencies",
)
cmdAdd.Flags().StringVarP(
&name, "name", "n", "", "specify project name",
)
rootCmd.AddCommand(cmdAdd)
cmdRemove := &cobra.Command{
Use: "remove PACKAGE...",
Short: "Remove packages from the specfile",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkgs := args
runRemove(language, pkgs, upgrade, forceLock, forceInstall)
},
}
cmdRemove.Flags().SortFlags = false
cmdRemove.Flags().BoolVarP(
&upgrade, "upgrade", "u", false, "upgrade all packages to latest allowed versions",
)
cmdRemove.Flags().BoolVarP(
&forceLock, "force-lock", "f", false, "rewrite lockfile even if up to date",
)
cmdRemove.Flags().BoolVarP(
&forceInstall, "force-install", "F", false, "reinstall packages even if up to date",
)
rootCmd.AddCommand(cmdRemove)
updateAliases := []string{"update", "upgrade"}
cmdLock := &cobra.Command{
Aliases: updateAliases,
Use: "lock",
Short: "Generate the lockfile from the specfile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
for _, updateAlias := range updateAliases {
if cmd.CalledAs() == updateAlias {
upgrade = true
}
}
runLock(language, upgrade, forceLock, forceInstall)
},
}
cmdLock.Flags().SortFlags = false
cmdLock.Flags().BoolVarP(
&upgrade, "upgrade", "u", false, "upgrade all packages to latest allowed versions",
)
cmdLock.Flags().BoolVarP(
&forceLock, "force-lock", "f", false, "rewrite lockfile even if up to date",
)
cmdLock.Flags().BoolVarP(
&forceInstall, "force-install", "F", false, "reinstall packages even if up to date",
)
rootCmd.AddCommand(cmdLock)
cmdInstall := &cobra.Command{
Use: "install",
Short: "Install packages from the lockfile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runInstall(language, forceInstall)
},
}
cmdInstall.Flags().SortFlags = false
cmdInstall.Flags().BoolVarP(
&forceInstall, "force", "F", false, "reinstall packages even if up to date",
)
rootCmd.AddCommand(cmdInstall)
cmdList := &cobra.Command{
Use: "list",
Short: "List packages from the specfile (or lockfile)",
Long: "List packages from the specfile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
outputFormat := parseOutputFormat(formatStr)
runList(language, all, outputFormat)
},
}
cmdInstall.Flags().SortFlags = false
cmdList.Flags().BoolVarP(
&all, "all", "a", false, "list packages from the lockfile instead",
)
cmdList.Flags().StringVarP(
&formatStr, "format", "f", "table", `output format ("table" or "json")`,
)
rootCmd.AddCommand(cmdList)
cmdGuess := &cobra.Command{
Use: "guess",
Short: "Guess what packages are needed by your project",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
util.AddIngoredPaths(ignoredPaths)
runGuess(language, all, forceGuess, ignoredPackages)
},
}
cmdGuess.Flags().SortFlags = false
cmdGuess.Flags().BoolVarP(
&all, "all", "a", false, "list even packages already in the specfile",
)
cmdGuess.Flags().BoolVarP(
&forceGuess, "force", "f", false, "bypass cache",
)
rootCmd.AddCommand(cmdGuess)
cmdShowSpecfile := &cobra.Command{
Use: "show-specfile",
Short: "Print the filename of the specfile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runShowSpecfile(language)
},
}
rootCmd.AddCommand(cmdShowSpecfile)
cmdShowLockfile := &cobra.Command{
Use: "show-lockfile",
Short: "Print the filename of the lockfile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runShowLockfile(language)
},
}
rootCmd.AddCommand(cmdShowLockfile)
cmdShowPackageDir := &cobra.Command{
Use: "show-package-dir",
Short: "Print the directory where packages are installed",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runShowPackageDir(language)
},
}
rootCmd.AddCommand(cmdShowPackageDir)
specialArgs := map[string](func()){}
for _, helpFlag := range []string{"-help", "-?"} {
specialArgs[helpFlag] = func() {
err := rootCmd.Usage()
if err != nil {
panic(err)
}
os.Exit(0)
}
}
for _, versionFlag := range []string{"-version", "-V"} {
specialArgs[versionFlag] = func() {
fmt.Println(getVersion())
os.Exit(0)
}
}
if len(os.Args) >= 2 {
fn, ok := specialArgs[os.Args[1]]
if ok {
fn()
}
}
util.ChdirToUPM()
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}