Skip to content

Commit

Permalink
make download and version subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Nov 24, 2019
1 parent e39204b commit 879f7a6
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions cmd/bindownloader/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"path"
"runtime"
Expand All @@ -20,47 +21,60 @@ var kongVars = kong.Vars{
"cellar_dir_help": `directory where downloads will be cached`,
}

var version = "unknown"

var cli struct {
Version versionCmd `kong:"cmd"`
Download downloadCmd `kong:"cmd"`
Config string `kong:"type=path,help=${config_help},default=${config_default}"`
CellarDir string `kong:"type=path,help=${cellar_dir_help}"`
}

type versionCmd struct{}

func (*versionCmd) Run(k *kong.Context) error {
k.Printf("version %s", version)
return nil
}

type downloadCmd struct {
Arch string `kong:"help=${arch_help},default=${arch_default}"`
OS string `kong:"help=${os_help},default=${os_default}"`
Config string `kong:"type=path,help=${config_help},default=${config_default}"`
Force bool `kong:"help=${force_help}"`
TargetFile string `kong:"arg,help='file to download'"`
CellarDir string `kong:"type=path,help=${cellar_dir_help}"`
TargetFile string `kong:"required=true,arg,help='file to download'"`
}

func main() {
parser := kong.Must(&cli, kongVars, kong.UsageOnError())
kctx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)

func (d *downloadCmd) Run(*kong.Context) error {
config, err := bindownloader.LoadConfigFile(cli.Config)
if err != nil {
kctx.Errorf("error loading config from %q\n", cli.Config)
os.Exit(1)
return fmt.Errorf("error loading config from %q\n", cli.Config)
}
binary := path.Base(d.TargetFile)
binDir := path.Dir(d.TargetFile)

binary := path.Base(cli.TargetFile)
binDir := path.Dir(cli.TargetFile)

downloader := config.Downloader(binary, cli.OS, cli.Arch)
downloader := config.Downloader(binary, d.OS, d.Arch)
if downloader == nil {
kctx.Errorf(`no downloader configured for:
return fmt.Errorf(`no downloader configured for:
bin: %s
os: %s
arch: %s
`, binary, cli.OS, cli.Arch)
os.Exit(1)
`, binary, d.OS, d.Arch)
}

installOpts := bindownloader.InstallOpts{
DownloaderName: binary,
TargetDir: binDir,
Force: cli.Force,
Force: d.Force,
CellarDir: cli.CellarDir,
}

err = downloader.Install(installOpts)
return downloader.Install(installOpts)
}

func main() {
parser := kong.Must(&cli, kongVars, kong.UsageOnError())

kctx.FatalIfErrorf(err)
kongCtx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)
kongCtx.FatalIfErrorf(kongCtx.Run(kongCtx))
}

0 comments on commit 879f7a6

Please sign in to comment.