Skip to content

Commit

Permalink
add "config format" command
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Nov 24, 2019
1 parent 1822d9b commit a2d1596
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 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
54 changes: 38 additions & 16 deletions cmd/bindownloader/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
Expand All @@ -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{}
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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())

Expand Down
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

0 comments on commit a2d1596

Please sign in to comment.