Skip to content

Commit

Permalink
Merge pull request #26 from WillAbides/cfgdown
Browse files Browse the repository at this point in the history
update config format
  • Loading branch information
WillAbides authored Nov 25, 2019
2 parents 8f30e4d + 088febb commit 6e42335
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
37 changes: 28 additions & 9 deletions bindownloader.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
Expand Down
52 changes: 47 additions & 5 deletions bindownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
})
}
4 changes: 2 additions & 2 deletions cmd/bindownloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion script/fmt
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

0 comments on commit 6e42335

Please sign in to comment.