Skip to content

Commit

Permalink
Merge pull request #22 from WillAbides/subcommands
Browse files Browse the repository at this point in the history
refactor cli
  • Loading branch information
WillAbides authored Nov 24, 2019
2 parents 990c632 + a2d1596 commit 9a1dfff
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 52 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ bin/bindownloader: gobuildcache
bins += bin/bindownloader

bin/golangci-lint: bin/bindownloader
bin/bindownloader $@
bin/bindownloader download $@
bins += bin/golangci-lint

bin/gobin: bin/bindownloader
bin/bindownloader $@
bin/bindownloader download $@
bins += bin/gobin

bin/goreleaser: bin/bindownloader
bin/bindownloader $@
bin/bindownloader download $@
bins += bin/goreleaser

bin/semver-next: bin/bindownloader
bin/bindownloader $@
bin/bindownloader download $@
bins += bin/semver-next

GOIMPORTS_REF := 8aaa1484dc108aa23dcf2d4a09371c0c9e280f6b
Expand Down
24 changes: 12 additions & 12 deletions buildtools.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,34 @@
"link": true
}
],
"protoc": [
"goreleaser": [
{
"os": "darwin",
"arch": "amd64",
"url": "https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-osx-x86_64.zip",
"checksum": "fd2e8a52b9b173bf90633f346901f4dcf3083769ea24f917809932d29ffa1410",
"archive_path": "bin/protoc"
"url": "https://github.com/goreleaser/goreleaser/releases/download/v0.120.7/goreleaser_Darwin_x86_64.tar.gz",
"checksum": "2ec8bb354cca2936d0722e7da770c37e2ba6cc90de4a1cea186e20968c47b663"
},
{
"os": "linux",
"arch": "amd64",
"url": "https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-linux-x86_64.zip",
"checksum": "77410d08e9a3c1ebb68afc13ee0c0fb4272c01c20bfd289adfb51b1c622bab07",
"archive_path": "bin/protoc"
"url": "https://github.com/goreleaser/goreleaser/releases/download/v0.120.7/goreleaser_Linux_x86_64.tar.gz",
"checksum": "771f2ad8219078b16a3e82097e9805309f6516640f0c6ab6b87f9b085a8ad743"
}
],
"goreleaser": [
"protoc": [
{
"os": "darwin",
"arch": "amd64",
"url": "https://github.com/goreleaser/goreleaser/releases/download/v0.120.7/goreleaser_Darwin_x86_64.tar.gz",
"checksum": "2ec8bb354cca2936d0722e7da770c37e2ba6cc90de4a1cea186e20968c47b663"
"url": "https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-osx-x86_64.zip",
"checksum": "fd2e8a52b9b173bf90633f346901f4dcf3083769ea24f917809932d29ffa1410",
"archive_path": "bin/protoc"
},
{
"os": "linux",
"arch": "amd64",
"url": "https://github.com/goreleaser/goreleaser/releases/download/v0.120.7/goreleaser_Linux_x86_64.tar.gz",
"checksum": "771f2ad8219078b16a3e82097e9805309f6516640f0c6ab6b87f9b085a8ad743"
"url": "https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-linux-x86_64.zip",
"checksum": "77410d08e9a3c1ebb68afc13ee0c0fb4272c01c20bfd289adfb51b1c622bab07",
"archive_path": "bin/protoc"
}
],
"semver-next": [
Expand Down
96 changes: 66 additions & 30 deletions cmd/bindownloader/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
Expand All @@ -10,57 +13,90 @@ import (
)

var kongVars = kong.Vars{
"arch_help": `download for this architecture`,
"arch_default": runtime.GOARCH,
"os_help": `download for this operating system`,
"os_default": runtime.GOOS,
"config_help": `file with tool definitions`,
"config_default": `buildtools.json`,
"force_help": `force download even if it already exists`,
"cellar_dir_help": `directory where downloads will be cached`,
"arch_help": `download for this architecture`,
"arch_default": runtime.GOARCH,
"os_help": `download for this operating system`,
"os_default": runtime.GOOS,
"configfile_help": `file with tool definitions`,
"configfile_default": `buildtools.json`,
"force_help": `force download even if it already exists`,
"cellar_dir_help": `directory where downloads will be cached`,
"config_format_help": `formats the config file`,
"download_help": `download a bin`,
}

var version = "unknown"

var cli struct {
Version versionCmd `kong:"cmd"`
Download downloadCmd `kong:"cmd,help=${download_help}"`
Config configCmd `kong:"cmd"`
Configfile string `kong:"type=path,help=${configfile_help},default=${configfile_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)

config, err := bindownloader.LoadConfigFile(cli.Config)
func (d *downloadCmd) Run(*kong.Context) error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
kctx.Errorf("error loading config from %q\n", cli.Config)
os.Exit(1)
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
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)
arch: %s`, 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)
}

kctx.FatalIfErrorf(err)
type configCmd struct {
Format configFmtCmd `kong:"cmd,help=${config_format_help}"`
}

type configFmtCmd struct{}

func (c configFmtCmd) Run() error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return err
}
b, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(cli.Configfile, b, 0600)
}

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

kongCtx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)
kongCtx.FatalIfErrorf(kongCtx.Run(kongCtx))
}
12 changes: 6 additions & 6 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import (

// Downloader downloads a binary
type Downloader struct {
URL string `json:"url"`
Checksum string `json:"checksum"`
BinName string `json:"bin"`
ArchivePath string `json:"archive_path"`
Link bool `json:"link"`
OS string `json:"os"`
Arch string `json:"arch"`
URL string `json:"url"`
Checksum string `json:"checksum,omitempty"`
ArchivePath string `json:"archive_path,omitempty"`
Link bool `json:"link,omitempty"`
BinName string `json:"bin,omitempty"`

// Deprecated: use ArchivePath
MoveFrom string `json:"move-from"`
MoveFrom string `json:"move-from,omitempty"`

// Deprecated: use ArchivePath and Link
LinkSource string `json:"symlink,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions script/bindownloader
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -e

CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")"

make -s bin/bindownloader

# shellcheck disable=SC2068
bin/bindownloader $@

0 comments on commit 9a1dfff

Please sign in to comment.