Skip to content

Commit d9e83dc

Browse files
committed
refactor: toolchains config, add AUTO_BUILD_TOOLCHAIN_CONFIG
1 parent 6fef244 commit d9e83dc

File tree

10 files changed

+272
-103
lines changed

10 files changed

+272
-103
lines changed

cmd/murphy/internal/scan/cmd.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/murphysecurity/murphysec/inspector"
2020
"github.com/murphysecurity/murphysec/model"
2121
"github.com/murphysecurity/murphysec/module/gradle"
22-
"github.com/murphysecurity/murphysec/module/maven"
2322
"github.com/murphysecurity/murphysec/scanerr"
23+
"github.com/murphysecurity/murphysec/toolver"
2424
"github.com/murphysecurity/murphysec/utils"
2525
"github.com/murphysecurity/murphysec/utils/must"
2626
"github.com/repeale/fp-go"
@@ -33,7 +33,6 @@ var isDeep bool
3333
var noBuild bool
3434
var projectNameCli string
3535
var projectsNameCli string
36-
var mavenSettingsPath string
3736
var onlyTaskId bool
3837
var privateSourceId string
3938
var privateSourceName string
@@ -91,7 +90,7 @@ func DfCmd() *cobra.Command {
9190
c.Flags().StringArrayVar(&mavenModuleName, "maven-module-name", make([]string, 0), "retains module")
9291
c.Flags().StringVar(&projectNameCli, "project-name", "", "specify project name")
9392
c.Flags().StringVar(&projectsNameCli, "projects-name", "", "specify projects name(group)")
94-
c.Flags().StringVar(&mavenSettingsPath, "maven-settings", "", "specify the path of maven settings")
93+
c.Flags().StringVar(&toolver.Default.Maven.MavenSettingPath, "maven-settings", "", "specify the path of maven settings")
9594
c.Flags().BoolVar(&onlyTaskId, "only-task-id", false, "print task id after task created, the scan result will not be printed")
9695
c.Flags().StringArrayVar(&projectTagNames, "project-tag", make([]string, 0), "specify the tag of the project")
9796
c.Flags().StringVar(&sbomOutputConfig, "sbom-output", "-", "Specify the SBOM output file path, use \"-\" to output to stdout")
@@ -271,10 +270,6 @@ func dfScanRun(cmd *cobra.Command, args []string) {
271270
ctx = ui.With(ctx, ui.CLI)
272271
}
273272

274-
if mavenSettingsPath != "" {
275-
//nolint:all
276-
ctx = context.WithValue(ctx, maven.M2SettingsFilePathCtxKey, mavenSettingsPath)
277-
}
278273
scanDir := args[0]
279274
scanDir, e := commonScanPreCheck(ctx, scanDir)
280275
if e != nil {

env/maven.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,16 @@
11
package env
22

33
import (
4-
"github.com/murphysecurity/murphysec/utils"
54
"os"
6-
"path/filepath"
7-
"runtime"
85
"strings"
96
)
107

11-
var IdeaMavenHome string
12-
var IdeaMavenConf string
13-
var IdeaMavenJre string
148
var MvnCommandTimeout = envi("MVN_COMMAND_TIMEOUT", 0)
159
var DisableMvnCommand = strings.TrimSpace(os.Getenv("NO_MVN")) != ""
1610
var MavenCentral string
1711

1812
func init() {
19-
var s string
20-
s = strings.TrimSpace(os.Getenv("IDEA_MAVEN_CONF"))
21-
if s != "" && utils.IsFile(s) && strings.ToLower(filepath.Ext(s)) == ".xml" {
22-
IdeaMavenConf = s
23-
}
24-
25-
IdeaMavenHome = locateMavenIdeaMavenHome()
26-
27-
s = strings.TrimSpace(os.Getenv("IDEA_MAVEN_JRE"))
28-
if s != "" && utils.IsDir(s) {
29-
IdeaMavenJre = s
30-
}
31-
3213
if strings.TrimSpace(os.Getenv("SKIP_MAVEN_CENTRAL")) == "" {
3314
MavenCentral = "https://repo1.maven.org/maven2/"
3415
}
3516
}
36-
37-
func locateMavenIdeaMavenHome() string {
38-
// todo: refactor
39-
var s = strings.TrimSpace(os.Getenv("IDEA_MAVEN_HOME"))
40-
if s == "" {
41-
return ""
42-
}
43-
if !filepath.IsAbs(s) {
44-
abs, e := filepath.Abs(s)
45-
if e == nil {
46-
s = abs
47-
}
48-
}
49-
if _s, e := filepath.EvalSymlinks(s); e == nil && _s != "" {
50-
s = _s
51-
}
52-
if runtime.GOOS == "windows" {
53-
return locateMavenIdeaMavenHomeWindows(s)
54-
}
55-
return locateMavenIdeaMavenHomeUnix(s)
56-
}
57-
58-
func locateMavenIdeaMavenHomeUnix(s string) string {
59-
if utils.IsFile(s) {
60-
return s
61-
}
62-
suffixes := []string{"mvn", "bin/mvn"}
63-
for _, it := range suffixes {
64-
target := filepath.Join(s, it)
65-
if utils.IsFile(target) {
66-
return target
67-
}
68-
}
69-
return ""
70-
}
71-
72-
func locateMavenIdeaMavenHomeWindows(s string) string {
73-
if utils.IsFile(s) {
74-
ext := strings.ToLower(filepath.Ext(s))
75-
if ext == ".exe" || ext == ".bat" || ext == ".cmd" {
76-
return s
77-
}
78-
return ""
79-
}
80-
var suffixes = []string{
81-
"mvn.cmd",
82-
"mvn.bat",
83-
"mvn.exe",
84-
"bin\\mvn.cmd",
85-
"bin\\mvn.bat",
86-
"bin\\mvn.exe",
87-
}
88-
for _, suffix := range suffixes {
89-
target := filepath.Join(s, suffix)
90-
if utils.IsFile(target) {
91-
return target
92-
}
93-
}
94-
return ""
95-
}

module/maven/m2setting.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"os"
8+
"path/filepath"
9+
"runtime"
10+
"strings"
11+
712
"github.com/antchfx/xmlquery"
813
"github.com/mitchellh/go-homedir"
914
"github.com/murphysecurity/murphysec/env"
1015
"github.com/murphysecurity/murphysec/infra/logctx"
16+
"github.com/murphysecurity/murphysec/toolver"
1117
"github.com/murphysecurity/murphysec/utils"
1218
"github.com/murphysecurity/murphysec/utils/must"
1319
"go.uber.org/zap"
14-
"os"
15-
"path/filepath"
16-
"runtime"
17-
"strings"
1820
)
1921

2022
type UserConfig struct {
@@ -100,9 +102,10 @@ func locateMvnInstallPath(ctx context.Context) string {
100102
}
101103

102104
func mavenSettingsPaths(ctx context.Context) (paths []string) {
103-
// IDEA specified path
104-
if env.IdeaMavenConf != "" {
105-
paths = append(paths, env.IdeaMavenConf)
105+
// specified path
106+
var toolConfig = toolver.Get(ctx)
107+
if toolConfig.Maven.MavenSettingPath != "" {
108+
paths = append(paths, toolConfig.Maven.MavenSettingPath)
106109
}
107110
// user path
108111
var homeDir = os.Getenv("M2_HOME")

module/maven/mvn_cmd_utils.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ import (
1313

1414
"github.com/murphysecurity/murphysec/env"
1515
"github.com/murphysecurity/murphysec/infra/logctx"
16+
"github.com/murphysecurity/murphysec/toolver"
1617
"go.uber.org/zap"
1718
)
1819

19-
const M2SettingsFilePathCtxKey = "MavenSettingsFilePathCtxKey"
20-
2120
type MvnCommandInfo struct {
2221
Path string `json:"path"`
2322
MvnVersion string `json:"mvn_version"`
2423
UserSettingsPath string `json:"user_settings_path"`
2524
JavaHome string `json:"java_home"`
25+
26+
AdditionalArgs []string `json:"additional_args,omitempty"`
27+
28+
AdditionalPrependArgs []string `json:"additional_prepend_args,omitempty"`
2629
}
2730

2831
func (m MvnCommandInfo) String() string {
@@ -33,12 +36,14 @@ func (m MvnCommandInfo) Command(ctx context.Context, args ...string) *exec.Cmd {
3336
if ctx == nil {
3437
ctx = context.TODO()
3538
}
36-
var _args = make([]string, 0, len(args)+5)
39+
var _args = make([]string, 0)
40+
_args = append(_args, m.AdditionalPrependArgs...)
3741
if m.UserSettingsPath != "" {
3842
_args = append(_args, "--settings", m.UserSettingsPath)
3943
}
4044
_args = append(_args, "--batch-mode")
4145
_args = append(_args, args...)
46+
_args = append(_args, m.AdditionalArgs...)
4247
cmd := exec.CommandContext(ctx, m.Path, _args...)
4348
if m.JavaHome != "" {
4449
cmd.Env = os.Environ()
@@ -55,7 +60,7 @@ type _MvnCommandResult struct {
5560
}
5661

5762
func CheckMvnCommand(ctx context.Context, isNoBuild bool) (info *MvnCommandInfo, err error) {
58-
63+
var toolVer = toolver.Get(ctx)
5964
var logger = logctx.Use(ctx)
6065
if cachedMvnCommandResult != nil {
6166
if cachedMvnCommandResult.e != nil {
@@ -83,18 +88,15 @@ func CheckMvnCommand(ctx context.Context, isNoBuild bool) (info *MvnCommandInfo,
8388
}
8489

8590
info = &MvnCommandInfo{}
86-
info.Path = env.IdeaMavenHome
91+
info.Path = toolVer.Maven.MavenCommand
8792
if info.Path == "" {
8893
info.Path = getMvnCommandOs()
8994
}
9095
if info.Path == "" {
9196
return nil, ErrMvnNotFound
9297
}
93-
info.JavaHome = env.IdeaMavenJre
94-
info.UserSettingsPath = env.IdeaMavenConf
95-
if r, ok := ctx.Value(M2SettingsFilePathCtxKey).(string); ok {
96-
info.UserSettingsPath = r
97-
}
98+
info.JavaHome = toolVer.Maven.JavaHome
99+
info.UserSettingsPath = toolVer.Maven.MavenSettingPath
98100
// check version
99101
ver, e := checkMvnVersion(ctx, info.Path, info.JavaHome)
100102
if e != nil {

toolver/env.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package toolver
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"sync"
7+
)
8+
9+
var envOnce sync.Once
10+
var envConfig *Config
11+
12+
func getFromEnv() *Config {
13+
envOnce.Do(func() {
14+
var s = os.Getenv("AUTO_BUILD_TOOLCHAIN_CONFIG")
15+
if s != "" {
16+
var o Config
17+
if json.Unmarshal([]byte(s), &o) != nil {
18+
envConfig = &o
19+
}
20+
}
21+
})
22+
return envConfig
23+
}

toolver/get.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package toolver
2+
3+
import (
4+
"context"
5+
"reflect"
6+
)
7+
8+
var Default Config
9+
var postprocessors []func(context.Context, *Config)
10+
11+
func Get(ctx context.Context) *Config {
12+
return mergeConfig(mergeConfig(&Default, getIntellijConfig()), getFromEnv())
13+
}
14+
15+
func mergeConfig(old *Config, new *Config) *Config {
16+
if old == nil && new == nil {
17+
return nil
18+
}
19+
if old == nil {
20+
c := *new
21+
return &c
22+
}
23+
if new == nil {
24+
c := *old
25+
return &c
26+
}
27+
28+
result := *old
29+
30+
mergeStruct(reflect.ValueOf(&result).Elem(), reflect.ValueOf(new).Elem())
31+
32+
return &result
33+
}
34+
35+
func mergeStruct(dst, src reflect.Value) {
36+
for i := 0; i < dst.NumField(); i++ {
37+
dstField := dst.Field(i)
38+
srcField := src.Field(i)
39+
40+
if !srcField.IsValid() || !srcField.CanInterface() {
41+
continue
42+
}
43+
44+
switch dstField.Kind() {
45+
case reflect.Struct:
46+
mergeStruct(dstField, srcField)
47+
default:
48+
if !srcField.IsZero() {
49+
if dstField.CanSet() {
50+
dstField.Set(srcField)
51+
}
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)