Skip to content

Commit

Permalink
Merge pull request #321 from projectdiscovery/print_reqs_after_instal…
Browse files Browse the repository at this point in the history
…l_msg

print reqs info before any error
  • Loading branch information
Mzack9999 committed Sep 2, 2024
2 parents 3028cb3 + 49c34c4 commit 85a4aac
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 77 deletions.
78 changes: 1 addition & 77 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

"github.com/projectdiscovery/gologger"
Expand All @@ -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"}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -261,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) {
Expand All @@ -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}
}
}
75 changes: 75 additions & 0 deletions pkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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}
}
}

0 comments on commit 85a4aac

Please sign in to comment.