From 71ac792d257294517b124a965756820786609618 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 25 Nov 2019 11:36:12 -0600 Subject: [PATCH 1/3] make sure symlinks are always evalled before creating a link --- downloader.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/downloader.go b/downloader.go index 66ac393..fc7a3c3 100644 --- a/downloader.go +++ b/downloader.go @@ -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 From 29553f04ca519ca02b3c5d9e006351b9bf23f017 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 25 Nov 2019 11:38:13 -0600 Subject: [PATCH 2/3] add "bindownloader config validate" --- cmd/bindownloader/config.go | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/cmd/bindownloader/config.go b/cmd/bindownloader/config.go index e3af7cd..0ba5406 100644 --- a/cmd/bindownloader/config.go +++ b/cmd/bindownloader/config.go @@ -4,7 +4,9 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "path" + "path/filepath" "github.com/alecthomas/kong" "github.com/willabides/bindownloader" @@ -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{} @@ -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 +} From d69ca1fae5255cc793cd09f963841813e0a14f15 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 25 Nov 2019 11:45:44 -0600 Subject: [PATCH 3/3] fix err shadow --- cmd/bindownloader/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bindownloader/config.go b/cmd/bindownloader/config.go index 0ba5406..d48a57f 100644 --- a/cmd/bindownloader/config.go +++ b/cmd/bindownloader/config.go @@ -90,7 +90,7 @@ func (d configValidateCmd) Run(kctx *kong.Context) error { return err } defer func() { - err := os.RemoveAll(tmpDir) + err = os.RemoveAll(tmpDir) if err != nil { kctx.Errorf("error deleting temp directory, %q", tmpDir) }