Skip to content

Commit 7baec21

Browse files
committed
Refactor Go package handler: add v prefix, go mod tidy, and rename to GoPackageUpdater
- Add 'v' prefix to Go module versions (fixes: go get invalid version error) - Run 'go mod tidy' after 'go get' to ensure go.mod and go.sum consistency - Set GOFLAGS=-mod=mod for predictable module behavior - Add debug logging for command output (warnings, etc.) - Remove CommonPackageHandler dependency - Go handler is now fully independent - Rename GoPackageHandler -> GoPackageUpdater (better name for future dependency bump feature) - Change receiver from 'golang' to 'gpu' for clarity - Follows Dependabot/Renovate best practices
1 parent 5cd924b commit 7baec21

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packagehandlers/commonpackagehandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type PackageHandler interface {
2323
func GetCompatiblePackageHandler(vulnDetails *utils.VulnerabilityDetails, details *utils.ScanDetails) (handler PackageHandler) {
2424
switch vulnDetails.Technology {
2525
case techutils.Go:
26-
handler = &GoPackageHandler{}
26+
handler = &GoPackageUpdater{}
2727
case techutils.Poetry:
2828
handler = &PythonPackageHandler{}
2929
case techutils.Pipenv:

packagehandlers/gopackagehandler.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,41 @@ import (
88

99
"github.com/jfrog/frogbot/v2/utils"
1010
golangutils "github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/golang"
11+
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1112
"github.com/jfrog/jfrog-client-go/utils/log"
1213
)
1314

14-
type GoPackageHandler struct {
15-
CommonPackageHandler
15+
type GoPackageUpdater struct {
16+
serverDetails *config.ServerDetails
17+
depsRepo string
1618
}
1719

18-
func (golang *GoPackageHandler) UpdateDependency(vulnDetails *utils.VulnerabilityDetails) error {
19-
if golang.depsRepo != "" {
20-
if err := golangutils.SetArtifactoryAsResolutionServer(golang.serverDetails, golang.depsRepo, golangutils.GoProxyUrlParams{}); err != nil {
20+
func (gpu *GoPackageUpdater) SetCommonParams(serverDetails *config.ServerDetails, depsRepo string) {
21+
gpu.serverDetails = serverDetails
22+
gpu.depsRepo = depsRepo
23+
}
24+
25+
func (gpu *GoPackageUpdater) UpdateDependency(vulnDetails *utils.VulnerabilityDetails) error {
26+
if gpu.depsRepo != "" {
27+
if err := golangutils.SetArtifactoryAsResolutionServer(gpu.serverDetails, gpu.depsRepo, golangutils.GoProxyUrlParams{}); err != nil {
2128
return err
2229
}
2330
}
2431

25-
env := golang.allowLockfileManipulation()
32+
env := gpu.allowLockfileManipulation()
2633

27-
if err := golang.updateDependency(vulnDetails, env); err != nil {
34+
if err := gpu.updateDependency(vulnDetails, env); err != nil {
2835
return err
2936
}
3037

31-
return golang.tidyLockfiles(env)
38+
return gpu.tidyLockfiles(env)
3239
}
3340

34-
func (golang *GoPackageHandler) allowLockfileManipulation() []string {
41+
func (gpu *GoPackageUpdater) allowLockfileManipulation() []string {
3542
return append(os.Environ(), "GOFLAGS=-mod=mod")
3643
}
3744

38-
func (golang *GoPackageHandler) updateDependency(vulnDetails *utils.VulnerabilityDetails, env []string) error {
45+
func (gpu *GoPackageUpdater) updateDependency(vulnDetails *utils.VulnerabilityDetails, env []string) error {
3946
impactedPackage := strings.ToLower(vulnDetails.ImpactedDependencyName)
4047
fixedVersion := strings.TrimSpace(vulnDetails.SuggestedFixedVersion)
4148

@@ -47,19 +54,29 @@ func (golang *GoPackageHandler) updateDependency(vulnDetails *utils.Vulnerabilit
4754
log.Debug(fmt.Sprintf("Running 'go get %s'", fixedPackage))
4855

4956
//#nosec G204 -- False positive - the subprocess only runs after the user's approval.
50-
if output, err := cmd.CombinedOutput(); err != nil {
57+
output, err := cmd.CombinedOutput()
58+
if len(output) > 0 {
59+
log.Debug(fmt.Sprintf("go get output:\n%s", string(output)))
60+
}
61+
62+
if err != nil {
5163
return fmt.Errorf("go get failed: %s\n%s", err.Error(), output)
5264
}
5365
return nil
5466
}
5567

56-
func (golang *GoPackageHandler) tidyLockfiles(env []string) error {
68+
func (gpu *GoPackageUpdater) tidyLockfiles(env []string) error {
5769
cmd := exec.Command("go", "mod", "tidy")
5870
cmd.Env = env
5971
log.Debug("Running 'go mod tidy'")
6072

6173
//#nosec G204 -- False positive - the subprocess only runs after the user's approval.
62-
if output, err := cmd.CombinedOutput(); err != nil {
74+
output, err := cmd.CombinedOutput()
75+
if len(output) > 0 {
76+
log.Debug(fmt.Sprintf("go mod tidy output:\n%s", string(output)))
77+
}
78+
79+
if err != nil {
6380
return fmt.Errorf("go mod tidy failed: %s\n%s", err.Error(), output)
6481
}
6582
return nil

0 commit comments

Comments
 (0)