diff --git a/display/base.go b/display/base.go index 55dcbc7f..6c748c0a 100644 --- a/display/base.go +++ b/display/base.go @@ -142,6 +142,10 @@ func (_ _CLI) UpdateStatus(s Status, msg string) { func (_ _CLI) Display(level MsgLevel, msg string) { termenv.ClearLine() - fmt.Println(termenv.String().Foreground(level.fColor()).Styled(fmt.Sprintf("[%s]", level.String())), msg) + if level == MsgError { + fmt.Println(termenv.String().Foreground(level.fColor()).Styled(fmt.Sprintf("[%s] %s", level.String(), msg))) + } else { + fmt.Println(termenv.String().Foreground(level.fColor()).Styled(fmt.Sprintf("[%s]", level.String())), msg) + } statusRepaint() } diff --git a/inspector/managed_inspect.go b/inspector/managed_inspect.go index c540333a..a684bf12 100644 --- a/inspector/managed_inspect.go +++ b/inspector/managed_inspect.go @@ -47,7 +47,10 @@ func managedInspectScan(ctx *ScanContext) error { } if inspector.CheckDir(path) { logger.Debug.Println("Matched", inspector, path) - rs, e := inspector.Inspect(path) + rs, e := inspector.Inspect(&base.ScanTask{ + ProjectDir: path, + UI: ctx.UI(), + }) if e != nil { logger.Info.Println("inspect failed.", inspector.String(), e.Error()) logger.Debug.Printf("%+v\n", e) diff --git a/module/base/common.go b/module/base/common.go index 5b40baa2..574c5e95 100644 --- a/module/base/common.go +++ b/module/base/common.go @@ -5,6 +5,7 @@ import ( "github.com/google/uuid" "github.com/pkg/errors" "murphysec-cli-simple/api" + "murphysec-cli-simple/display" "regexp" "strings" ) @@ -63,7 +64,7 @@ func mapVoDependency(d []Dependency) []api.VoDependency { type Inspector interface { fmt.Stringer CheckDir(dir string) bool - Inspect(dir string) ([]Module, error) + Inspect(task *ScanTask) ([]Module, error) PackageManagerType() PackageManagerType } @@ -121,3 +122,10 @@ func UnwrapToInspectorError(e error) *InspectorError { } return nil } + +// TODO: refactor to interface + +type ScanTask struct { + ProjectDir string + UI display.UI +} diff --git a/module/composer/composer.go b/module/composer/composer.go index 440cc625..a061bd1f 100644 --- a/module/composer/composer.go +++ b/module/composer/composer.go @@ -23,8 +23,8 @@ func (i *Inspector) CheckDir(dir string) bool { return utils.IsFile(filepath.Join(dir, "composer.json")) } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { - +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + dir := task.ProjectDir manifest, e := readManifest(filepath.Join(dir, "composer.json")) if e != nil { return nil, e diff --git a/module/go_mod/go.go b/module/go_mod/go.go index 3ad38221..ccbb3a6c 100644 --- a/module/go_mod/go.go +++ b/module/go_mod/go.go @@ -3,8 +3,10 @@ package go_mod import ( "bytes" "encoding/json" + "fmt" "github.com/pkg/errors" "io" + "murphysec-cli-simple/display" "murphysec-cli-simple/logger" "murphysec-cli-simple/module/base" "murphysec-cli-simple/utils" @@ -25,8 +27,8 @@ func (i *Inspector) CheckDir(dir string) bool { return utils.IsFile(filepath.Join(dir, "go.mod")) } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { - return ScanGoProject(dir) +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + return ScanGoProject(task.ProjectDir, task) } func (i *Inspector) PackageManagerType() base.PackageManagerType { @@ -37,12 +39,14 @@ func New() base.Inspector { return &Inspector{} } -func ScanGoProject(dir string) ([]base.Module, error) { +func ScanGoProject(dir string, task *base.ScanTask) ([]base.Module, error) { version, e := execGoVersion() if e != nil { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】识别到您的环境中 Go 无法正常运行,可能会导致检测结果不完整,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) return nil, ErrGoEnv } if e := execGoModTidy(dir); e != nil { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】通过 Go获取依赖信息失败,可能会导致检测结果不完整或失败,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) logger.Err.Println("go mod tidy execute failed.", e.Error()) return nil, e } diff --git a/module/gradle/gradle.go b/module/gradle/gradle.go index 0617d49c..4bc22f45 100644 --- a/module/gradle/gradle.go +++ b/module/gradle/gradle.go @@ -3,6 +3,7 @@ package gradle import ( "fmt" "github.com/pkg/errors" + "murphysec-cli-simple/display" "murphysec-cli-simple/logger" "murphysec-cli-simple/module/base" "os" @@ -26,10 +27,12 @@ func (i *Inspector) PackageManagerType() base.PackageManagerType { return base.PMGradle } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + dir := task.ProjectDir logger.Debug.Println("gradle inspect dir:", dir) info, e := evalGradleInfo(dir) if e != nil { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】识别到目录下没有 gradlew 文件或您的环境中 Gradle 无法正常运行,可能会导致检测结果不完整,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) logger.Info.Println("check gradle failed", e.Error()) return nil, e } @@ -38,7 +41,7 @@ func (i *Inspector) Inspect(dir string) ([]base.Module, error) { if e != nil { logger.Info.Println("fetch gradle projects failed.", e.Error()) } - logger.Debug.Println("Gradle proje`cts:", strings.Join(projects, ", ")) + logger.Debug.Println("Gradle projects:", strings.Join(projects, ", ")) var rs []base.Module { depInfo, e := evalGradleDependencies(dir, "", info) @@ -51,6 +54,7 @@ func (i *Inspector) Inspect(dir string) ([]base.Module, error) { for _, projectId := range projects { depInfo, e := evalGradleDependencies(dir, projectId, info) if e != nil { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】通过 Gradle 获取依赖信息失败,可能会导致检测结果不完整或失败,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) logger.Info.Println("evalGradleDependencies failed.", projectId, e.Error()) } else { rs = append(rs, depInfo.BaseModule(filepath.Join(dir, "build.gradle"))) diff --git a/module/maven/base.go b/module/maven/base.go index a75abf07..1b67991c 100644 --- a/module/maven/base.go +++ b/module/maven/base.go @@ -25,8 +25,8 @@ func (i *Inspector) CheckDir(dir string) bool { return utils.IsFile(filepath.Join(dir, "pom.xml")) } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { - return ScanMavenProject(dir) +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + return ScanMavenProject(task.ProjectDir, task) } func (i *Inspector) PackageManagerType() base.PackageManagerType { diff --git a/module/maven/maven.go b/module/maven/maven.go index 3d0133df..f3cb46f2 100644 --- a/module/maven/maven.go +++ b/module/maven/maven.go @@ -2,6 +2,7 @@ package maven import ( "fmt" + "murphysec-cli-simple/display" "murphysec-cli-simple/logger" "murphysec-cli-simple/module/base" "path/filepath" @@ -20,7 +21,7 @@ func (d Dependency) String() string { var MvnSkipped = base.NewInspectError("java", "Mvn inspect is skipped, please check you maven environment.") -func ScanMavenProject(dir string) ([]base.Module, error) { +func ScanMavenProject(dir string, task *base.ScanTask) ([]base.Module, error) { var modules []base.Module var deps map[Coordinate][]Dependency moduleFileMapping := map[Coordinate]string{} @@ -30,8 +31,11 @@ func ScanMavenProject(dir string) ([]base.Module, error) { if doMvnScan { deps, e = scanMvnDependency(dir) if e != nil { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】通过 Maven获取依赖信息失败,可能会导致检测结果不完整或失败,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) logger.Err.Printf("mvn scan failed: %+v\n", e) } + } else { + task.UI.Display(display.MsgError, fmt.Sprintf("【%s】识别到您的环境中 Maven 无法正常运行,可能会导致检测结果不完整,访问https://www.murphysec.com/docs/quick-start/language-support/ 了解详情", dir)) } // analyze pom file { diff --git a/module/maven/mvn_helper.go b/module/maven/mvn_helper.go index dddec5fe..c233a1a8 100644 --- a/module/maven/mvn_helper.go +++ b/module/maven/mvn_helper.go @@ -87,6 +87,7 @@ func scanMvnDependency(projectDir string) (map[Coordinate][]Dependency, error) { } if e := cmd.Wait(); e != nil { logger.Err.Println("Mvn terminated with err.", e.Error()) + return mvnResult, e } else { logger.Info.Println("Mvn terminated with no err") } diff --git a/module/npm/npm.go b/module/npm/npm.go index 0aae64e7..fd12bc80 100644 --- a/module/npm/npm.go +++ b/module/npm/npm.go @@ -27,8 +27,8 @@ func (i *Inspector) CheckDir(dir string) bool { utils.IsFile(filepath.Join(dir, "package-lock.json")) } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { - return ScanNpmProject(dir) +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + return ScanNpmProject(task.ProjectDir) } func (i *Inspector) PackageManagerType() base.PackageManagerType { diff --git a/module/python/python.go b/module/python/python.go index 1db4a822..64530d7d 100644 --- a/module/python/python.go +++ b/module/python/python.go @@ -55,7 +55,8 @@ func parsePyImport(input string) []string { return rs } -func (i Inspector) Inspect(dir string) ([]base.Module, error) { +func (i Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + dir := task.ProjectDir componentMap := map[string]string{} requirementsFiles := map[string]struct{}{} ignoreSet := map[string]struct{}{} diff --git a/module/yarn/yarnlock.go b/module/yarn/yarnlock.go index 754fe212..2c621c68 100644 --- a/module/yarn/yarnlock.go +++ b/module/yarn/yarnlock.go @@ -27,7 +27,8 @@ func (i *Inspector) CheckDir(dir string) bool { return e == nil && !info.IsDir() } -func (i *Inspector) Inspect(dir string) ([]base.Module, error) { +func (i *Inspector) Inspect(task *base.ScanTask) ([]base.Module, error) { + dir := task.ProjectDir logger.Info.Println("yarn inspect.", dir) rs, e := analyzeYarnDep(dir)