From 39296c0c93f69e45ce304c783d40f0454bb6b6d2 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 25 Nov 2019 12:49:31 -0600 Subject: [PATCH 1/2] don't always download goimports --- script/fmt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/fmt b/script/fmt index f9cad4a..7ad33c5 100755 --- a/script/fmt +++ b/script/fmt @@ -4,6 +4,6 @@ set -e CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")" -make -s bin/goimports +[ -f bin/goimports ] || make -s bin/goimports bin/goimports -w . From 088febb06dab5f430bc90c01e54e741146fc8367 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 25 Nov 2019 12:50:06 -0600 Subject: [PATCH 2/2] update config format --- bindownloader.go | 37 +++++++++++++++++++------- bindownloader_test.go | 52 +++++++++++++++++++++++++++++++++---- cmd/bindownloader/config.go | 4 +-- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/bindownloader.go b/bindownloader.go index 9cfed7b..ca41426 100644 --- a/bindownloader.go +++ b/bindownloader.go @@ -1,25 +1,42 @@ package bindownloader import ( + "bytes" "encoding/json" "fmt" "io" + "io/ioutil" "os" "strings" ) //LoadConfig returns a Config from a config reader -func LoadConfig(config io.Reader) (Config, error) { - var dls Config - err := json.NewDecoder(config).Decode(&dls) +func LoadConfig(config io.Reader) (*Config, error) { + configBytes, err := ioutil.ReadAll(config) if err != nil { return nil, err } - return dls, nil + decoder := json.NewDecoder(bytes.NewReader(configBytes)) + decoder.DisallowUnknownFields() + var cfg Config + err = decoder.Decode(&cfg) + if err != nil { + decoder = json.NewDecoder(bytes.NewReader(configBytes)) + decoder.DisallowUnknownFields() + dls := cfg.Downloaders + err = decoder.Decode(&dls) + if err == nil { + cfg.Downloaders = dls + } + } + if err != nil { + return nil, err + } + return &cfg, err } //LoadConfigFile returns a Config from the path to a config file -func LoadConfigFile(configFile string) (Config, error) { +func LoadConfigFile(configFile string) (*Config, error) { configReader, err := os.Open(configFile) //nolint:gosec if err != nil { return nil, fmt.Errorf("couldn't read config file: %s", configFile) @@ -28,12 +45,14 @@ func LoadConfigFile(configFile string) (Config, error) { return LoadConfig(configReader) } -// Config map binary names to Config -type Config map[string][]*Downloader +//Config is downloaders configuration +type Config struct { + Downloaders map[string][]*Downloader `json:"downloaders,omitempty"` +} // Downloader returns a Downloader for the given binary, os and arch. -func (c Config) Downloader(binary, os, arch string) *Downloader { - l, ok := c[binary] +func (c *Config) Downloader(binary, os, arch string) *Downloader { + l, ok := c.Downloaders[binary] if !ok { return nil } diff --git a/bindownloader_test.go b/bindownloader_test.go index ea2b176..5308f29 100644 --- a/bindownloader_test.go +++ b/bindownloader_test.go @@ -11,7 +11,49 @@ import ( ) func TestLoadConfig(t *testing.T) { - t.Run("success", func(t *testing.T) { + t.Run("current format", func(t *testing.T) { + dir, teardown := tmpDir(t) + defer teardown() + file := filepath.Join(dir, "buildtools.json") + + // language=json + content := ` +{ + "downloaders": { + "gobin": [ + { + "os": "darwin", + "arch": "amd64", + "url": "https://github.com/myitcv/gobin/releases/download/v0.0.10/darwin-amd64", + "checksum": "84ed966949e06bebd7d006bc343caf9d736932fd8b37df5cb5b268a28d07bd30", + "archive_path": "darwin-amd64", + "link": true + }, + { + "os": "linux", + "arch": "amd64", + "url": "https://github.com/myitcv/gobin/releases/download/v0.0.10/linux-amd64", + "checksum": "415266d9af98578067051653f5057ea267c51ebf085408df48b118a8b978bac6", + "archive_path": "linux-amd64" + } + ] + } +} +` + err := ioutil.WriteFile(file, []byte(content), 0640) + require.NoError(t, err) + fileReader, err := os.Open(file) + require.NoError(t, err) + defer func() { + require.NoError(t, fileReader.Close()) + }() + cfg, err := LoadConfig(fileReader) + assert.NoError(t, err) + assert.Equal(t, "darwin-amd64", cfg.Downloaders["gobin"][0].ArchivePath) + assert.True(t, cfg.Downloaders["gobin"][0].Link) + }) + + t.Run("downloaders only", func(t *testing.T) { dir, teardown := tmpDir(t) defer teardown() file := filepath.Join(dir, "buildtools.json") @@ -45,9 +87,9 @@ func TestLoadConfig(t *testing.T) { defer func() { require.NoError(t, fileReader.Close()) }() - d, err := LoadConfig(fileReader) - assert.NoError(t, err) - assert.Equal(t, "darwin-amd64", d["gobin"][0].ArchivePath) - assert.True(t, d["gobin"][0].Link) + cfg, err := LoadConfig(fileReader) + require.NoError(t, err) + assert.Equal(t, "darwin-amd64", cfg.Downloaders["gobin"][0].ArchivePath) + assert.True(t, cfg.Downloaders["gobin"][0].Link) }) } diff --git a/cmd/bindownloader/config.go b/cmd/bindownloader/config.go index d48a57f..67e7b9c 100644 --- a/cmd/bindownloader/config.go +++ b/cmd/bindownloader/config.go @@ -52,7 +52,7 @@ func (d *configUpdateChecksumsCmd) Run(*kong.Context) error { binary := path.Base(d.TargetFile) binDir := path.Dir(d.TargetFile) - downloaders, ok := config[binary] + downloaders, ok := config.Downloaders[binary] if !ok { return fmt.Errorf("nothing configured for %q", binary) } @@ -108,7 +108,7 @@ func (d configValidateCmd) Run(kctx *kong.Context) error { cellarDir = filepath.Join(tmpDir, "cellar") } - downloaders, ok := config[binary] + downloaders, ok := config.Downloaders[binary] if !ok { return fmt.Errorf("nothing configured for %q", binary) }