Skip to content

Commit

Permalink
Merge pull request #25 from WillAbides/validate
Browse files Browse the repository at this point in the history
config vaidate
  • Loading branch information
WillAbides authored Nov 25, 2019
2 parents 5da28c0 + d69ca1f commit 8f30e4d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cmd/bindownloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/alecthomas/kong"
"github.com/willabides/bindownloader"
Expand All @@ -14,11 +16,14 @@ var configKongVars = kong.Vars{
"config_format_help": `formats the config file`,
"config_checksums_help": `update checksums in the config file`,
"config_checksums_bin_help": `name of the binary to update`,
"config_validate_bin_help": `name of the binary to validate`,
"config_validate_help": `validate that downloads work`,
}

type configCmd struct {
Format configFmtCmd `kong:"cmd,help=${config_format_help}"`
UpdateChecksums configUpdateChecksumsCmd `kong:"cmd,help=${config_checksums_bin_help}"`
Validate configValidateCmd `kong:"cmd,help=${config_validate_help}"`
}

type configFmtCmd struct{}
Expand Down Expand Up @@ -70,3 +75,66 @@ func (d *configUpdateChecksumsCmd) Run(*kong.Context) error {

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

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

func (d configValidateCmd) Run(kctx *kong.Context) error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
tmpDir, err := ioutil.TempDir("", "bindownloader")
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")
}

downloaders, ok := config[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 := bindownloader.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
}
}
return nil
}
10 changes: 10 additions & 0 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ func (d *Downloader) moveOrLinkBin(targetDir, extractDir string) error {
return err
}

targetDir, err = filepath.EvalSymlinks(targetDir)
if err != nil {
return err
}

extractedBin, err = filepath.EvalSymlinks(extractedBin)
if err != nil {
return err
}

dst, err := filepath.Rel(targetDir, extractedBin)
if err != nil {
return err
Expand Down

0 comments on commit 8f30e4d

Please sign in to comment.