@@ -5,6 +5,10 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "github.com/murphysecurity/murphysec/env"
9+ "github.com/murphysecurity/murphysec/infra/sl"
10+ "github.com/repeale/fp-go"
11+ "go.uber.org/zap"
812 "io"
913 "os/exec"
1014 "path/filepath"
@@ -30,53 +34,53 @@ func (Inspector) CheckDir(dir string) bool {
3034 return utils .IsFile (filepath .Join (dir , "go.mod" ))
3135}
3236
33- // func (Inspector) InspectProject(ctx context.Context) error {
34- // task := model.UseInspectionTask(ctx)
35- // logger := logctx.Use(ctx)
36- // modFilePath := filepath.Join(task.Dir(), "go.mod")
37- // logger.Debug("Reading go.mod", zap.String("path", modFilePath))
38- // data, e := utils.ReadFileLimited(modFilePath, 1024*1024*4)
39- // if e != nil {
40- // return errors.WithMessage(e, "Open GoMod file")
41- // }
42- // logger.Debug("Parsing go.mod")
43- // f, e := modfile.ParseLax(filepath.Base(modFilePath), data, nil)
44- // if e != nil {
45- // return errors.WithMessage(e, "Parse go mod failed")
46- // }
47- // var dependencies []model.DependencyItem
48- // if !env.DoNotBuild {
49- // // try command go list
50- // dependencies, e = doGoList(ctx, task.Dir())
51- // if e != nil {
52- // if errors.Is(e, _ErrGoNotFound) {
53- // logger.Debug("Go not found, skip GoList")
54- // } else {
55- // // log it and go on
56- // logger.Warn("GoList failed", zap.Error(e))
57- // }
58- // dependencies = append(dependencies, fp.Map(mapRequireToDependencyItem)(sl.FilterNotNull(f.Require))...)
59- // }
60- // }
61- // if len(dependencies) == 0 {
62- // if !env.DoNotBuild {
63- // logger.Warn("no dependencies found, backup")
64- // }
65- // dependencies = append(dependencies, fp.Map(mapRequireToDependencyItem)(sl.FilterNotNull(f.Require))...)
66- // }
67- // m := model.Module{
68- // PackageManager: "gomod",
69- // ModulePath: modFilePath,
70- // ModuleName: "<NoNameModule>",
71- // Dependencies: dependencies,
72- // }
73- // if f.Module != nil {
74- // m.ModuleVersion = f.Module.Mod.Version
75- // m.ModuleName = f.Module.Mod.Path
76- // }
77- // task.AddModule(m)
78- // return nil
79- // }
37+ func (Inspector ) InspectProject (ctx context.Context ) error {
38+ task := model .UseInspectionTask (ctx )
39+ logger := logctx .Use (ctx )
40+ modFilePath := filepath .Join (task .Dir (), "go.mod" )
41+ logger .Debug ("Reading go.mod" , zap .String ("path" , modFilePath ))
42+ data , e := utils .ReadFileLimited (modFilePath , 1024 * 1024 * 4 )
43+ if e != nil {
44+ return errors .WithMessage (e , "Open GoMod file" )
45+ }
46+ logger .Debug ("Parsing go.mod" )
47+ f , e := modfile .ParseLax (filepath .Base (modFilePath ), data , nil )
48+ if e != nil {
49+ return errors .WithMessage (e , "Parse go mod failed" )
50+ }
51+ var dependencies []model.DependencyItem
52+ if ! env .DoNotBuild {
53+ // try command go list
54+ dependencies , e = doGoList (ctx , task .Dir ())
55+ if e != nil {
56+ if errors .Is (e , _ErrGoNotFound ) {
57+ logger .Debug ("Go not found, skip GoList" )
58+ } else {
59+ // log it and go on
60+ logger .Warn ("GoList failed" , zap .Error (e ))
61+ }
62+ dependencies = append (dependencies , fp .Map (mapRequireToDependencyItem )(sl .FilterNotNull (f .Require ))... )
63+ }
64+ }
65+ if len (dependencies ) == 0 {
66+ if ! env .DoNotBuild {
67+ logger .Warn ("no dependencies found, backup" )
68+ }
69+ dependencies = append (dependencies , fp .Map (mapRequireToDependencyItem )(sl .FilterNotNull (f .Require ))... )
70+ }
71+ m := model.Module {
72+ PackageManager : "gomod" ,
73+ ModulePath : modFilePath ,
74+ ModuleName : "<NoNameModule>" ,
75+ Dependencies : dependencies ,
76+ }
77+ if f .Module != nil {
78+ m .ModuleVersion = f .Module .Mod .Version
79+ m .ModuleName = f .Module .Mod .Path
80+ }
81+ task .AddModule (m )
82+ return nil
83+ }
8084
8185func mapRequireToDependencyItem (it * modfile.Require ) model.DependencyItem {
8286 return model.DependencyItem {
0 commit comments