Skip to content

Commit

Permalink
Merge pull request #183 from WillAbides/alldeps
Browse files Browse the repository at this point in the history
install/extract/download all dependencies with --all
  • Loading branch information
WillAbides authored Nov 16, 2023
2 parents 432020c + b1cf665 commit 3cd477b
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions cmd/bindown/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var kongVars = kong.Vars{
"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`,
"all_deps_help": `select all dependencies`,
}

type rootCmd struct {
Expand Down Expand Up @@ -252,19 +253,37 @@ func (c fmtCmd) Run(ctx *runContext, cli *rootCmd) error {

type installCmd struct {
Force bool `kong:"help=${install_force_help}"`
Dependency []string `kong:"required=true,arg,help=${download_dependency_help},predictor=bin"`
Dependency []string `kong:"arg,name=dependency,help=${download_dependency_help},predictor=bin"`
All bool `kong:"help=${all_deps_help}"`
TargetFile string `kong:"type=path,name=output,type=file,help=${install_target_file_help}"`
System bindown.System `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) BeforeApply(k *kong.Context) error {
optionalDependency(k)
return nil
}

func (d *installCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
if len(d.Dependency) > 1 && d.TargetFile != "" {
return fmt.Errorf("cannot specify --output when installing multiple dependencies")
if d.All {
if len(d.Dependency) > 0 {
return fmt.Errorf("cannot specify dependencies when using --all")
}
d.Dependency = allDependencies(config)
}
switch len(d.Dependency) {
case 0:
return fmt.Errorf("must specify at least one dependency")
case 1:
default:
if d.TargetFile != "" {
return fmt.Errorf("cannot specify --output when installing multiple dependencies")
}
}
for _, dep := range d.Dependency {
var pth string
Expand All @@ -284,15 +303,30 @@ func (d *installCmd) Run(ctx *runContext) error {
type downloadCmd struct {
Force bool `kong:"help=${download_force_help}"`
System bindown.System `kong:"name=system,default=${system_default},help=${system_help},predictor=allSystems"`
Dependency []string `kong:"required=true,arg,help=${download_dependency_help},predictor=bin"`
Dependency []string `kong:"arg,help=${download_dependency_help},predictor=bin"`
All bool `kong:"help=${all_deps_help}"`
AllowMissingChecksum bool `kong:"name=allow-missing-checksum,help=${allow_missing_checksum}"`
}

func (d *downloadCmd) BeforeApply(k *kong.Context) error {
optionalDependency(k)
return nil
}

func (d *downloadCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
if d.All {
if len(d.Dependency) > 0 {
return fmt.Errorf("cannot specify dependencies when using --all")
}
d.Dependency = allDependencies(config)
}
if len(d.Dependency) == 0 {
return fmt.Errorf("must specify at least one dependency")
}
for _, dep := range d.Dependency {
var pth string
pth, err = config.DownloadDependency(dep, d.System, &bindown.ConfigDownloadDependencyOpts{
Expand All @@ -309,15 +343,30 @@ func (d *downloadCmd) Run(ctx *runContext) error {

type extractCmd struct {
System bindown.System `kong:"name=system,default=${system_default},help=${system_help},predictor=allSystems"`
Dependency []string `kong:"required=true,arg,help=${extract_dependency_help},predictor=bin"`
Dependency []string `kong:"arg,help=${extract_dependency_help},predictor=bin"`
All bool `kong:"help=${all_deps_help}"`
AllowMissingChecksum bool `kong:"name=allow-missing-checksum,help=${allow_missing_checksum}"`
}

func (d *extractCmd) BeforeApply(k *kong.Context) error {
optionalDependency(k)
return nil
}

func (d *extractCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
if d.All {
if len(d.Dependency) > 0 {
return fmt.Errorf("cannot specify dependencies when using --all")
}
d.Dependency = allDependencies(config)
}
if len(d.Dependency) == 0 {
return fmt.Errorf("must specify at least one dependency")
}
for _, dep := range d.Dependency {
var pth string
pth, err = config.ExtractDependency(dep, d.System, &bindown.ConfigExtractDependencyOpts{
Expand All @@ -330,3 +379,13 @@ func (d *extractCmd) Run(ctx *runContext) error {
}
return nil
}

// optionalDependency sets dependency positional to optional. We do this because we want to allow --all to be
// equivalent to specifying all dependencies but want the help output to indicate that a dependency is required.
func optionalDependency(k *kong.Context) {
for _, pos := range k.Selected().Positional {
if pos.Name == "dependency" {
pos.Required = false
}
}
}

0 comments on commit 3cd477b

Please sign in to comment.