From 6713a3ec049076583fc6cf504c1f607f147b65cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 2 Sep 2024 16:14:39 +0300 Subject: [PATCH 1/2] print reqs info after installation msg --- internal/runner/runner.go | 76 --------------------------------------- pkg/install.go | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 25cf9fa..bffc04f 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "os/exec" - "runtime" "strings" "github.com/projectdiscovery/gologger" @@ -15,9 +14,7 @@ import ( "github.com/projectdiscovery/pdtm/pkg/types" "github.com/projectdiscovery/pdtm/pkg/utils" errorutil "github.com/projectdiscovery/utils/errors" - osutils "github.com/projectdiscovery/utils/os" stringsutil "github.com/projectdiscovery/utils/strings" - "github.com/projectdiscovery/utils/syscallutil" ) var excludedToolList = []string{"nuclei-templates"} @@ -119,7 +116,6 @@ func (r *Runner) Run() error { if err := pkg.GoInstall(r.options.Path, tool); err != nil { gologger.Error().Msgf("%s: %s", tool.Name, err) } - printRequirementInfo(tool) continue } @@ -134,7 +130,6 @@ func (r *Runner) Run() error { } } } - printRequirementInfo(tool) } else { gologger.Error().Msgf("error while installing %s: %s not found in the list", toolName, toolName) } @@ -194,49 +189,6 @@ func isGoInstalled() bool { return true } -func printRequirementInfo(tool types.Tool) { - specs := getSpecs(tool) - - printTitle := true - stringBuilder := &strings.Builder{} - for _, spec := range specs { - if requirementSatisfied(spec.Name) { - continue - } - if printTitle { - stringBuilder.WriteString(fmt.Sprintf("%s\n", au.Bold(tool.Name+" requirements:").String())) - printTitle = false - } - instruction := getFormattedInstruction(spec) - isRequired := getRequirementStatus(spec) - stringBuilder.WriteString(fmt.Sprintf("%s %s\n", isRequired, instruction)) - } - if stringBuilder.Len() > 0 { - gologger.Info().Msgf("%s", stringBuilder.String()) - } -} - -func getRequirementStatus(spec types.ToolRequirementSpecification) string { - if spec.Required { - return au.Yellow("required").String() - } - return au.BrightGreen("optional").String() -} - -func getFormattedInstruction(spec types.ToolRequirementSpecification) string { - return strings.Replace(spec.Instruction, "$CMD", spec.Command, 1) -} - -func getSpecs(tool types.Tool) []types.ToolRequirementSpecification { - var specs []types.ToolRequirementSpecification - for _, requirement := range tool.Requirements { - if requirement.OS == runtime.GOOS { - specs = append(specs, requirement.Specification...) - } - } - return specs -} - // UpdateCache creates/updates cache file func UpdateCache(toolList []types.Tool) error { b, err := json.Marshal(toolList) @@ -280,31 +232,3 @@ func (r *Runner) ListToolsAndEnv(tools []types.Tool) error { // Close the runner instance func (r *Runner) Close() {} - -func requirementSatisfied(requirementName string) bool { - if strings.HasPrefix(requirementName, "lib") { - libNames := appendLibExtensionForOS(requirementName) - for _, libName := range libNames { - _, sysErr := syscallutil.LoadLibrary(libName) - if sysErr == nil { - return true - } - } - return false - } - _, execErr := exec.LookPath(requirementName) - return execErr == nil -} - -func appendLibExtensionForOS(lib string) []string { - switch { - case osutils.IsWindows(): - return []string{fmt.Sprintf("%s.dll", lib), lib} - case osutils.IsLinux(): - return []string{fmt.Sprintf("%s.so", lib), lib} - case osutils.IsOSX(): - return []string{fmt.Sprintf("%s.dylib", lib), lib} - default: - return []string{lib} - } -} diff --git a/pkg/install.go b/pkg/install.go index 72ddffd..a006cc8 100644 --- a/pkg/install.go +++ b/pkg/install.go @@ -21,6 +21,8 @@ import ( "github.com/projectdiscovery/gologger" ospath "github.com/projectdiscovery/pdtm/pkg/path" "github.com/projectdiscovery/pdtm/pkg/types" + osutils "github.com/projectdiscovery/utils/os" + "github.com/projectdiscovery/utils/syscallutil" ) var ( @@ -34,6 +36,7 @@ func Install(path string, tool types.Tool) error { return types.ErrIsInstalled } gologger.Info().Msgf("installing %s...", tool.Name) + printRequirementInfo(tool) version, err := install(tool, path) if err != nil { return err @@ -48,6 +51,7 @@ func GoInstall(path string, tool types.Tool) error { return types.ErrIsInstalled } gologger.Info().Msgf("installing %s with go install...", tool.Name) + printRequirementInfo(tool) cmd := exec.Command("go", "install", "-v", fmt.Sprintf("github.com/projectdiscovery/%s/%s", tool.Name, tool.GoInstallPath)) cmd.Env = append(os.Environ(), "GOBIN="+path) if output, err := cmd.CombinedOutput(); err != nil { @@ -225,3 +229,74 @@ func downloadZip(reader io.Reader, toolName, path string) error { } return nil } + +func printRequirementInfo(tool types.Tool) { + specs := getSpecs(tool) + + printTitle := true + stringBuilder := &strings.Builder{} + for _, spec := range specs { + if requirementSatisfied(spec.Name) { + continue + } + if printTitle { + stringBuilder.WriteString(fmt.Sprintf("%s\n", au.Bold(tool.Name+" requirements:").String())) + printTitle = false + } + instruction := getFormattedInstruction(spec) + isRequired := getRequirementStatus(spec) + stringBuilder.WriteString(fmt.Sprintf("%s %s\n", isRequired, instruction)) + } + if stringBuilder.Len() > 0 { + gologger.Info().Msgf("%s", stringBuilder.String()) + } +} + +func getRequirementStatus(spec types.ToolRequirementSpecification) string { + if spec.Required { + return au.Yellow("required").String() + } + return au.BrightGreen("optional").String() +} + +func getFormattedInstruction(spec types.ToolRequirementSpecification) string { + return strings.Replace(spec.Instruction, "$CMD", spec.Command, 1) +} + +func getSpecs(tool types.Tool) []types.ToolRequirementSpecification { + var specs []types.ToolRequirementSpecification + for _, requirement := range tool.Requirements { + if requirement.OS == runtime.GOOS { + specs = append(specs, requirement.Specification...) + } + } + return specs +} + +func requirementSatisfied(requirementName string) bool { + if strings.HasPrefix(requirementName, "lib") { + libNames := appendLibExtensionForOS(requirementName) + for _, libName := range libNames { + _, sysErr := syscallutil.LoadLibrary(libName) + if sysErr == nil { + return true + } + } + return false + } + _, execErr := exec.LookPath(requirementName) + return execErr == nil +} + +func appendLibExtensionForOS(lib string) []string { + switch { + case osutils.IsWindows(): + return []string{fmt.Sprintf("%s.dll", lib), lib} + case osutils.IsLinux(): + return []string{fmt.Sprintf("%s.so", lib), lib} + case osutils.IsOSX(): + return []string{fmt.Sprintf("%s.dylib", lib), lib} + default: + return []string{lib} + } +} From 49c34c43570ba9932503369edd7720f973358c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 2 Sep 2024 16:58:03 +0300 Subject: [PATCH 2/2] minor --- internal/runner/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index bffc04f..72ddb7c 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -213,7 +213,7 @@ func FetchFromCache() ([]types.Tool, error) { // ListToolsAndEnv prints the list of tools func (r *Runner) ListToolsAndEnv(tools []types.Tool) error { - gologger.Info().Msgf(path.GetOsData() + "\n") + gologger.Info().Msgf("%s\n", path.GetOsData()) gologger.Info().Msgf("Path to download project binary: %s\n", r.options.Path) var fmtMsg string if path.IsSet(r.options.Path) {