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"`