Skip to content

Commit

Permalink
refactor: 打印一些用户友好的文本
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed May 16, 2022
1 parent 50a4ca3 commit cbfea2c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 17 deletions.
6 changes: 5 additions & 1 deletion display/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
5 changes: 4 additions & 1 deletion inspector/managed_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion module/base/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"
"murphysec-cli-simple/api"
"murphysec-cli-simple/display"
"regexp"
"strings"
)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -121,3 +122,10 @@ func UnwrapToInspectorError(e error) *InspectorError {
}
return nil
}

// TODO: refactor to interface

type ScanTask struct {
ProjectDir string
UI display.UI
}
4 changes: 2 additions & 2 deletions module/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions module/go_mod/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions module/gradle/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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")))
Expand Down
4 changes: 2 additions & 2 deletions module/maven/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion module/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package maven

import (
"fmt"
"murphysec-cli-simple/display"
"murphysec-cli-simple/logger"
"murphysec-cli-simple/module/base"
"path/filepath"
Expand All @@ -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{}
Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions module/maven/mvn_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions module/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion module/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down
3 changes: 2 additions & 1 deletion module/yarn/yarnlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit cbfea2c

Please sign in to comment.