-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.go
322 lines (289 loc) · 10.9 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
package main
import (
"context"
"fmt"
"io"
"os"
"runtime"
"time"
"github.com/alecthomas/kong"
"github.com/willabides/bindown/v3"
"github.com/willabides/kongplete"
)
var kongVars = kong.Vars{
"configfile_help": `file with bindown config. default is the first one of bindown.yml, bindown.yaml, bindown.json, .bindown.yml, .bindown.yaml or .bindown.json`,
"cache_help": `directory downloads will be cached`,
"install_help": `download, extract and install a dependency`,
"system_default": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
"system_help": `target system in the format of <os>/<architecture>`,
"systems_help": `target systems in the format of <os>/<architecture>`,
"add_checksums_help": `add checksums to the config file`,
"prune_checksums_help": `remove unnecessary checksums from the config file`,
"config_format_help": `formats the config file`,
"config_validate_help": `validate that installs work`,
"config_install_completions_help": `install shell completions`,
"config_extract_path_help": `output path to directory where the downloaded archive is extracted`,
"install_force_help": `force install even if it already exists`,
"install_target_file_help": `where to write the file`,
"install_dependency_help": `dependency to install`,
"download_force_help": `force download even if the file already exists`,
"download_target_file_help": `filename and path for the downloaded file. Default downloads to cache.`,
"download_dependency_help": `name of the dependency to download`,
"allow_missing_checksum": `allow missing checksums`,
"download_help": `download a dependency but don't extract or install it`,
"extract_dependency_help": `name of the dependency to extract`,
"extract_help": `download and extract a dependency but don't install it`,
"extract_target_dir_help": `path to extract to. Default extracts to cache.`,
"checksums_dep_help": `name of the dependency to update`,
"trust_cache_help": `trust the cache contents and do not recheck existing downloads and extracts in the cache`,
}
type rootCmd struct {
JSONConfig bool `kong:"name=json,help='treat config file as json instead of yaml'"`
Configfile string `kong:"type=path,help=${configfile_help},env='BINDOWN_CONFIG_FILE'"`
CacheDir string `kong:"name=cache,type=path,help=${cache_help},env='BINDOWN_CACHE'"`
TrustCache *bool `kong:"help=${trust_cache_help},env='BINDOWN_TRUST_CACHE'"`
Quiet bool `kong:"short='q',help='suppress output to stdout'"`
Download downloadCmd `kong:"cmd,help=${download_help}"`
Extract extractCmd `kong:"cmd,help=${extract_help}"`
Install installCmd `kong:"cmd,help=${install_help}"`
Format fmtCmd `kong:"cmd,help=${config_format_help}"`
Dependency dependencyCmd `kong:"cmd,help='manage dependencies'"`
Template templateCmd `kong:"cmd,help='manage templates'"`
TemplateSource templateSourceCmd `kong:"cmd,help='manage template sources'"`
SupportedSystem supportedSystemCmd `kong:"cmd,help='manage supported systems'"`
Checksums checksumsCmd `kong:"cmd,help='manage checksums'"`
Init initCmd `kong:"cmd,help='create an empty config file'"`
Cache cacheCmd `kong:"cmd,help='manage the cache'"`
Version versionCmd `kong:"cmd,help='show bindown version'"`
InstallCompletions kongplete.InstallCompletions `kong:"cmd,help=${config_install_completions_help}"`
AddChecksums addChecksumsCmd `kong:"cmd,hidden"`
Validate validateCmd `kong:"cmd,hidden"`
}
var defaultConfigFilenames = []string{
"bindown.yml",
"bindown.yaml",
"bindown.json",
".bindown.yml",
".bindown.yaml",
".bindown.json",
}
func loadConfigFile(ctx *runContext, noDefaultDirs bool) (*bindown.ConfigFile, error) {
filename := ctx.rootCmd.Configfile
if filename == "" {
for _, configFilename := range defaultConfigFilenames {
info, err := os.Stat(configFilename)
if err == nil && !info.IsDir() {
filename = configFilename
break
}
}
}
configFile, err := bindown.LoadConfigFile(ctx, filename, noDefaultDirs)
if err != nil {
return nil, err
}
if ctx.rootCmd.CacheDir != "" {
configFile.Cache = ctx.rootCmd.CacheDir
}
if ctx.rootCmd.TrustCache != nil {
configFile.TrustCache = *ctx.rootCmd.TrustCache
}
return configFile, nil
}
type runContext struct {
parent context.Context
stdin io.Reader
stdout io.Writer
rootCmd *rootCmd
}
func newRunContext(ctx context.Context) *runContext {
return &runContext{
parent: ctx,
}
}
func (r *runContext) Deadline() (deadline time.Time, ok bool) {
return r.parent.Deadline()
}
func (r *runContext) Done() <-chan struct{} {
return r.parent.Done()
}
func (r *runContext) Err() error {
return r.parent.Err()
}
func (r *runContext) Value(key any) any {
return r.parent.Value(key)
}
type runOpts struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
cmdName string
exitHandler func(int)
}
// Run let's light this candle
func Run(ctx context.Context, args []string, opts *runOpts) {
if opts == nil {
opts = &runOpts{}
}
var root rootCmd
runCtx := newRunContext(ctx)
runCtx.rootCmd = &root
runCtx.stdin = opts.stdin
if runCtx.stdin == nil {
runCtx.stdin = os.Stdin
}
runCtx.stdout = opts.stdout
if runCtx.stdout == nil {
runCtx.stdout = os.Stdout
}
stderr := opts.stderr
if stderr == nil {
stderr = os.Stderr
}
kongOptions := []kong.Option{
kong.HelpOptions{Compact: true},
kong.BindTo(runCtx, &runCtx),
kongVars,
kong.UsageOnError(),
kong.Writers(runCtx.stdout, stderr),
}
if opts.exitHandler != nil {
kongOptions = append(kongOptions, kong.Exit(opts.exitHandler))
}
if opts.cmdName != "" {
kongOptions = append(kongOptions, kong.Name(opts.cmdName))
}
parser := kong.Must(&root, kongOptions...)
runCompletion(ctx, parser)
kongCtx, err := parser.Parse(args)
parser.FatalIfErrorf(err)
if root.Quiet {
runCtx.stdout = io.Discard
kongCtx.Stdout = io.Discard
}
err = kongCtx.Run()
parser.FatalIfErrorf(err)
}
func runCompletion(ctx context.Context, parser *kong.Kong) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
kongplete.Complete(parser,
kongplete.WithPredictor("bin", binCompleter(ctx)),
kongplete.WithPredictor("allSystems", allSystemsCompleter),
kongplete.WithPredictor("templateSource", templateSourceCompleter(ctx)),
kongplete.WithPredictor("system", systemCompleter(ctx)),
kongplete.WithPredictor("localTemplate", localTemplateCompleter(ctx)),
kongplete.WithPredictor("localTemplateFromSource", localTemplateFromSourceCompleter(ctx)),
kongplete.WithPredictor("template", templateCompleter(ctx)),
)
}
type initCmd struct{}
func (c *initCmd) Run(ctx *runContext) error {
for _, filename := range defaultConfigFilenames {
info, err := os.Stat(filename)
if err == nil && !info.IsDir() {
return fmt.Errorf("%s already exists", filename)
}
}
configfile := ctx.rootCmd.Configfile
if configfile == "" {
configfile = ".bindown.yaml"
}
file, err := os.Create(configfile)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
cfg := &bindown.ConfigFile{
Filename: file.Name(),
}
return cfg.Write(ctx.rootCmd.JSONConfig)
}
type fmtCmd struct{}
func (c fmtCmd) Run(ctx *runContext, cli *rootCmd) error {
ctx.rootCmd.CacheDir = ""
config, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
return config.Write(ctx.rootCmd.JSONConfig)
}
// validateCmd is a deprecated synonym for dependencyValidateCmd
type validateCmd struct {
Dependency string `kong:"required=true,arg,predictor=bin"`
Systems []bindown.SystemInfo `kong:"name=system,predictor=allSystems"`
}
func (d validateCmd) Run(ctx *runContext) error {
return dependencyValidateCmd(d).Run(ctx)
}
type installCmd struct {
Force bool `kong:"help=${install_force_help}"`
Dependency string `kong:"required=true,arg,help=${download_dependency_help},predictor=bin"`
TargetFile string `kong:"type=path,name=output,type=file,help=${install_target_file_help}"`
System bindown.SystemInfo `kong:"name=system,default=${system_default},help=${system_help},predictor=allSystems"`
AllowMissingChecksum bool `kong:"name=allow-missing-checksum,help=${allow_missing_checksum}"`
}
func (d *installCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
pth, err := config.InstallDependency(d.Dependency, d.System, &bindown.ConfigInstallDependencyOpts{
TargetPath: d.TargetFile,
Force: d.Force,
AllowMissingChecksum: d.AllowMissingChecksum,
})
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "installed %s to %s\n", d.Dependency, pth)
return nil
}
type downloadCmd struct {
Force bool `kong:"help=${download_force_help}"`
System bindown.SystemInfo `kong:"name=system,default=${system_default},help=${system_help},predictor=allSystems"`
Dependency string `kong:"required=true,arg,help=${download_dependency_help},predictor=bin"`
TargetFile string `kong:"name=output,help=${download_target_file_help}"`
AllowMissingChecksum bool `kong:"name=allow-missing-checksum,help=${allow_missing_checksum}"`
}
func (d *downloadCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
pth, err := config.DownloadDependency(d.Dependency, d.System, &bindown.ConfigDownloadDependencyOpts{
TargetFile: d.TargetFile,
Force: d.Force,
AllowMissingChecksum: d.AllowMissingChecksum,
})
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "downloaded %s to %s\n", d.Dependency, pth)
return nil
}
type extractCmd struct {
System bindown.SystemInfo `kong:"name=system,default=${system_default},help=${system_help},predictor=allSystems"`
Dependency string `kong:"required=true,arg,help=${extract_dependency_help},predictor=bin"`
TargetDir string `kong:"name=output,help=${extract_target_dir_help}"`
AllowMissingChecksum bool `kong:"name=allow-missing-checksum,help=${allow_missing_checksum}"`
}
func (d *extractCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
pth, err := config.ExtractDependency(d.Dependency, d.System, &bindown.ConfigExtractDependencyOpts{
TargetDirectory: d.TargetDir,
Force: false,
AllowMissingChecksum: d.AllowMissingChecksum,
})
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "extracted %s to %s\n", d.Dependency, pth)
return nil
}