From 21b5ca7989f131caff8f7cb9d0b50e7fc8d2a5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 5 Sep 2023 12:41:21 +0000 Subject: [PATCH 1/5] add go install support as fallback --- internal/runner/runner.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index be0e6f4..50f2e66 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -107,6 +107,14 @@ func (r *Runner) Run() error { } else { gologger.Error().Msgf("error while installing %s: %s", toolName, err) } + + gologger.Info().Msgf("trying to install %s using go install", toolName) + if err := fallbackGoInstall(toolName); err != nil { + gologger.Error().Msgf("error while installing %s using go install: %s", toolName, err) + } else { + gologger.Info().Msgf("successfully installed %s using go install", toolName) + } + } tool := getTool(toolName, toolList) printRequirementInfo(tool) @@ -152,6 +160,17 @@ func (r *Runner) Run() error { return nil } +func fallbackGoInstall(toolName string) error { + cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/v2/cmd/%s@latest", toolName, toolName)) + if _, err := cmd.CombinedOutput(); err != nil { + cmd = exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/cmd/%s@latest", toolName, toolName)) + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("go install failed for %s: %s", toolName, string(output)) + } + } + return nil +} + func printRequirementInfo(tool *types.Tool) { specs := getSpecs(tool) From d100b0b388a4161f16207a1dd7621fb17c766d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 25 Sep 2023 12:50:15 +0000 Subject: [PATCH 2/5] use go install path from pdtm api --- internal/runner/runner.go | 16 ++++++---------- pkg/types/types.go | 11 ++++++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 50f2e66..c122339 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -101,6 +101,7 @@ func (r *Runner) Run() error { continue } if i, ok := utils.Contains(toolList, toolName); ok { + tool := getTool(toolName, toolList) if err := pkg.Install(r.options.Path, toolList[i]); err != nil { if err == types.ErrIsInstalled { gologger.Info().Msgf("%s: %s", toolName, err) @@ -109,14 +110,12 @@ func (r *Runner) Run() error { } gologger.Info().Msgf("trying to install %s using go install", toolName) - if err := fallbackGoInstall(toolName); err != nil { + if err := fallbackGoInstall(tool); err != nil { gologger.Error().Msgf("error while installing %s using go install: %s", toolName, err) } else { gologger.Info().Msgf("successfully installed %s using go install", toolName) } - } - tool := getTool(toolName, toolList) printRequirementInfo(tool) } else { gologger.Error().Msgf("error while installing %s: %s not found in the list", toolName, toolName) @@ -160,13 +159,10 @@ func (r *Runner) Run() error { return nil } -func fallbackGoInstall(toolName string) error { - cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/v2/cmd/%s@latest", toolName, toolName)) - if _, err := cmd.CombinedOutput(); err != nil { - cmd = exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/cmd/%s@latest", toolName, toolName)) - if output, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("go install failed for %s: %s", toolName, string(output)) - } +func fallbackGoInstall(tool *types.Tool) error { + cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/%s", tool.Name, tool.GoInstallPath)) + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("go install failed for %s: %s", tool.Name, string(output)) } return nil } diff --git a/pkg/types/types.go b/pkg/types/types.go index ba58c8e..f4244d7 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -13,11 +13,12 @@ var ( ) type Tool struct { - Name string `json:"name"` - Repo string `json:"repo"` - Version string `json:"version"` - Requirements []ToolRequirement `json:"requirements"` - Assets map[string]string `json:"assets"` + Name string `json:"name"` + Repo string `json:"repo"` + Version string `json:"version"` + GoInstallPath string `json:"go_install_path" yaml:"go_install_path"` + Requirements []ToolRequirement `json:"requirements"` + Assets map[string]string `json:"assets"` } type ToolRequirement struct { From bfaf476575ce1b8e33dbf698cd4286ada6f1549f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 25 Sep 2023 12:50:29 +0000 Subject: [PATCH 3/5] set GOBIN --- internal/runner/runner.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index c122339..53c4e3d 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -160,6 +160,10 @@ func (r *Runner) Run() error { } func fallbackGoInstall(tool *types.Tool) error { + err := os.Setenv("GOBIN", defaultPath) + if err != nil { + return fmt.Errorf("failed to set GOBIN: %s", err) + } cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/%s", tool.Name, tool.GoInstallPath)) if output, err := cmd.CombinedOutput(); err != nil { return fmt.Errorf("go install failed for %s: %s", tool.Name, string(output)) From ce653bb2fb443ceadf6897ec308ad7693735c45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Wed, 27 Sep 2023 13:03:57 +0000 Subject: [PATCH 4/5] do not `go install` if the tool is already installed --- internal/runner/runner.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 53c4e3d..62069f0 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -103,17 +103,16 @@ func (r *Runner) Run() error { if i, ok := utils.Contains(toolList, toolName); ok { tool := getTool(toolName, toolList) if err := pkg.Install(r.options.Path, toolList[i]); err != nil { - if err == types.ErrIsInstalled { + if errors.Is(err, types.ErrIsInstalled) { gologger.Info().Msgf("%s: %s", toolName, err) } else { gologger.Error().Msgf("error while installing %s: %s", toolName, err) - } - - gologger.Info().Msgf("trying to install %s using go install", toolName) - if err := fallbackGoInstall(tool); err != nil { - gologger.Error().Msgf("error while installing %s using go install: %s", toolName, err) - } else { - gologger.Info().Msgf("successfully installed %s using go install", toolName) + gologger.Info().Msgf("trying to install %s using go install", toolName) + if err := fallbackGoInstall(tool); err != nil { + gologger.Error().Msgf("error while installing %s using go install: %s", toolName, err) + } else { + gologger.Info().Msgf("successfully installed %s using go install", toolName) + } } } printRequirementInfo(tool) From b5d3686c194a8c53cc555d3f8b73596718ea4b59 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Wed, 27 Sep 2023 18:10:02 +0200 Subject: [PATCH 5/5] better scoping --- internal/runner/runner.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 62069f0..c5b1ad5 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -159,11 +159,8 @@ func (r *Runner) Run() error { } func fallbackGoInstall(tool *types.Tool) error { - err := os.Setenv("GOBIN", defaultPath) - if err != nil { - return fmt.Errorf("failed to set GOBIN: %s", err) - } cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/%s", tool.Name, tool.GoInstallPath)) + cmd.Env = append(os.Environ(), "GOBIN="+defaultPath) if output, err := cmd.CombinedOutput(); err != nil { return fmt.Errorf("go install failed for %s: %s", tool.Name, string(output)) }