From e39204b2cace4855087e4610c781d36b55c46320 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sun, 24 Nov 2019 11:52:10 -0600 Subject: [PATCH 1/4] add script/bindownloader --- script/bindownloader | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 script/bindownloader diff --git a/script/bindownloader b/script/bindownloader new file mode 100755 index 0000000..28e02ba --- /dev/null +++ b/script/bindownloader @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")" + +make -s bin/bindownloader + +# shellcheck disable=SC2068 +bin/bindownloader $@ From 879f7a6c2dbee5a203918ca4e949a81d6bec02a2 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sun, 24 Nov 2019 11:53:12 -0600 Subject: [PATCH 2/4] make download and version subcommands --- cmd/bindownloader/main.go | 54 ++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/cmd/bindownloader/main.go b/cmd/bindownloader/main.go index b23178c..90d0803 100644 --- a/cmd/bindownloader/main.go +++ b/cmd/bindownloader/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "path" "runtime" @@ -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)) } From 1822d9bd99894e5e3cb3e76a843cc00758be9991 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sun, 24 Nov 2019 14:21:30 -0600 Subject: [PATCH 3/4] reorder buildtools.json --- buildtools.json | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/buildtools.json b/buildtools.json index 2f99fd7..6b3e85e 100644 --- a/buildtools.json +++ b/buildtools.json @@ -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": [ From a2d1596affba93815665cb2277d449447eedc725 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sun, 24 Nov 2019 14:59:12 -0600 Subject: [PATCH 4/4] add "config format" command --- Makefile | 8 +++--- cmd/bindownloader/main.go | 54 +++++++++++++++++++++++++++------------ downloader.go | 12 ++++----- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index aeb5136..86642c0 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/bindownloader/main.go b/cmd/bindownloader/main.go index 90d0803..b9cc6ec 100644 --- a/cmd/bindownloader/main.go +++ b/cmd/bindownloader/main.go @@ -1,7 +1,9 @@ package main import ( + "encoding/json" "fmt" + "io/ioutil" "os" "path" "runtime" @@ -11,23 +13,26 @@ 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"` - Config string `kong:"type=path,help=${config_help},default=${config_default}"` - CellarDir string `kong:"type=path,help=${cellar_dir_help}"` + 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{} @@ -45,9 +50,9 @@ type downloadCmd struct { } func (d *downloadCmd) Run(*kong.Context) error { - config, err := bindownloader.LoadConfigFile(cli.Config) + config, err := bindownloader.LoadConfigFile(cli.Configfile) if err != nil { - return fmt.Errorf("error loading config from %q\n", cli.Config) + return fmt.Errorf("error loading config from %q", cli.Configfile) } binary := path.Base(d.TargetFile) binDir := path.Dir(d.TargetFile) @@ -57,8 +62,7 @@ func (d *downloadCmd) Run(*kong.Context) error { return fmt.Errorf(`no downloader configured for: bin: %s os: %s -arch: %s -`, binary, d.OS, d.Arch) +arch: %s`, binary, d.OS, d.Arch) } installOpts := bindownloader.InstallOpts{ @@ -71,6 +75,24 @@ arch: %s return downloader.Install(installOpts) } +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()) diff --git a/downloader.go b/downloader.go index fce2040..1189fa2 100644 --- a/downloader.go +++ b/downloader.go @@ -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"`