Skip to content

Commit

Permalink
refactor for testability (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides authored Nov 27, 2019
1 parent 2c2e0ae commit b71d6dc
Show file tree
Hide file tree
Showing 19 changed files with 948 additions and 478 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ bin/semver-next: bin/bindown
bin/bindown download $@
bins += bin/semver-next

MOCKGEN_REF := 9fa652df1129bef0e734c9cf9bf6dbae9ef3b9fa
bin/mockgen: bin/gobin
GOBIN=${CURDIR}/bin \
bin/gobin github.com/golang/mock/mockgen@$(MOCKGEN_REF);
bins += bin/mockgen

GOIMPORTS_REF := 8aaa1484dc108aa23dcf2d4a09371c0c9e280f6b
bin/goimports: bin/gobin
GOBIN=${CURDIR}/bin \
Expand Down
86 changes: 0 additions & 86 deletions bindown.go

This file was deleted.

95 changes: 0 additions & 95 deletions bindown_test.go

This file was deleted.

119 changes: 17 additions & 102 deletions cmd/bindown/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/alecthomas/kong"
"github.com/willabides/bindown/v2"
"github.com/willabides/bindown/v2/pkg/config"
"go.uber.org/multierr"
)

var configKongVars = kong.Vars{
Expand All @@ -29,128 +26,46 @@ type configCmd struct {
type configFmtCmd struct{}

func (c configFmtCmd) Run() error {
config, err := bindown.LoadConfigFile(cli.Configfile)
configFile, err := config.NewConfigFile(cli.Configfile)
if err != nil {
return err
}
b, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(cli.Configfile, b, 0600)
return configFile.WriteFile()
}

type configUpdateChecksumsCmd struct {
TargetFile string `kong:"required=true,arg,help=${config_checksums_bin_help}"`
}

func (d *configUpdateChecksumsCmd) Run(kctx *kong.Context) error {
config, err := bindown.LoadConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
tmpDir, err := ioutil.TempDir("", "bindown")
func (d *configUpdateChecksumsCmd) Run(*kong.Context) error {
configFile, err := config.NewConfigFile(cli.Configfile)
if err != nil {
return err
}
defer func() {
err = os.RemoveAll(tmpDir)
if err != nil {
kctx.Errorf("error deleting temp directory, %q", tmpDir)
}
}()

binary := path.Base(d.TargetFile)
binDir := path.Dir(d.TargetFile)

cellarDir := cli.CellarDir
if cellarDir == "" {
cellarDir = filepath.Join(tmpDir, "cellar")
}

downloaders, ok := config.Downloaders[binary]
if !ok {
return fmt.Errorf("nothing configured for %q", binary)
}

for _, downloader := range downloaders {
err = downloader.UpdateChecksum(bindown.UpdateChecksumOpts{
DownloaderName: binary,
CellarDir: cellarDir,
TargetDir: binDir,
})
if err != nil {
return err
}
}

b, err := json.MarshalIndent(&config, "", " ")
binary := filepath.Base(d.TargetFile)
err = configFile.UpdateChecksums(binary, cli.CellarDir)
if err != nil {
return err
}

return ioutil.WriteFile(cli.Configfile, b, 0600)
return configFile.WriteFile()
}

type configValidateCmd struct {
Bin string `kong:"required=true,arg,help=${config_validate_bin_help}"`
}

func (d configValidateCmd) Run(kctx *kong.Context) error {
config, err := bindown.LoadConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
tmpDir, err := ioutil.TempDir("", "bindown")
config, err := config.NewConfigFile(cli.Configfile)
if err != nil {
return err
}
defer func() {
err = os.RemoveAll(tmpDir)
if err != nil {
kctx.Errorf("error deleting temp directory, %q", tmpDir)
}
}()

binary := path.Base(d.Bin)
binDir := filepath.Join(tmpDir, "bin")
err = os.MkdirAll(binDir, 0700)
if err != nil {
return err
}

cellarDir := cli.CellarDir
if cellarDir == "" {
cellarDir = filepath.Join(tmpDir, "cellar")
err = config.Validate(d.Bin, cli.CellarDir)
if err == nil {
return nil
}

downloaders, ok := config.Downloaders[binary]
if !ok {
return fmt.Errorf("nothing configured for %q", binary)
}

for _, downloader := range downloaders {
dlJSON, err := json.MarshalIndent(downloader, "", " ")
if err != nil {
return err
}

installOpts := bindown.InstallOpts{
DownloaderName: binary,
TargetDir: binDir,
Force: true,
CellarDir: cellarDir,
}

err = downloader.Install(installOpts)
if err != nil {
return fmt.Errorf("could not validate downloader:\n%s", string(dlJSON))
}

err = os.Remove(filepath.Join(binDir, downloader.BinName))
if err != nil {
return err
}
errs := multierr.Errors(err)
for _, gotErr := range errs {
kctx.Printf("%s\n", gotErr.Error())
}
return nil
return fmt.Errorf("could not validate")
}
15 changes: 4 additions & 11 deletions cmd/bindown/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"runtime"

"github.com/alecthomas/kong"
"github.com/willabides/bindown/v2"
"github.com/willabides/bindown/v2/pkg/config"
)

var downloadKongVars = kong.Vars{
Expand All @@ -26,27 +26,20 @@ type downloadCmd struct {
}

func (d *downloadCmd) Run(*kong.Context) error {
config, err := bindown.LoadConfigFile(cli.Configfile)
cfg, err := config.NewConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
binary := path.Base(d.TargetFile)
binDir := path.Dir(d.TargetFile)

downloader := config.Downloader(binary, d.OS, d.Arch)
downloader := cfg.Downloader(binary, d.OS, d.Arch)
if downloader == nil {
return fmt.Errorf(`no downloader configured for:
bin: %s
os: %s
arch: %s`, binary, d.OS, d.Arch)
}

installOpts := bindown.InstallOpts{
DownloaderName: binary,
TargetDir: binDir,
Force: d.Force,
CellarDir: cli.CellarDir,
}

return downloader.Install(installOpts)
return downloader.Install(binary, cli.CellarDir, binDir, d.Force)
}
Loading

0 comments on commit b71d6dc

Please sign in to comment.