Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit c6dbb9f

Browse files
committed
Tidy up -mod argument stringification
1 parent b13b54f commit c6dbb9f

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,24 @@ const (
157157
ModVendor
158158
)
159159

160-
func (m ModMode) String() string {
160+
func (m ModMode) argsForGoVersion(version string) []string {
161161
switch m {
162162
case ModUnset:
163-
return ""
163+
return []string{}
164164
case ModReadonly:
165-
return "-mod=readonly"
165+
return []string{"-mod=readonly"}
166166
case ModMod:
167-
return "-mod=mod"
167+
if semver.Compare(getEnvGoVersion(), "1.14") < 0 {
168+
log.Printf("%s < %s", getEnvGoVersion(), "1.14")
169+
return []string{} // -mod=mod is the default behaviour for go <= 1.13, and is not accepted as an argument
170+
} else {
171+
log.Printf("%s >= %s", getEnvGoVersion(), "1.14")
172+
return []string{"-mod=mod"}
173+
}
168174
case ModVendor:
169-
return "-mod=vendor"
170-
}
171-
return ""
172-
}
173-
174-
// modModIfSupported returns `ModMod` if that flag is supported, or `ModUnset` if it is not, in
175-
// which case the behavior should be identical to `ModMod`.
176-
func modModIfSupported() ModMode {
177-
if semver.Compare(getEnvGoVersion(), "1.14") < 0 {
178-
return ModUnset
179-
} else {
180-
return ModMod
175+
return []string{"-mod=vendor"}
181176
}
177+
return nil
182178
}
183179

184180
// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
@@ -245,7 +241,7 @@ func main() {
245241
if util.FileExists("vendor/modules.txt") {
246242
modMode = ModVendor
247243
} else if util.DirExists("vendor") {
248-
modMode = modModIfSupported()
244+
modMode = ModMod
249245
}
250246

251247
if modMode == ModVendor {
@@ -270,7 +266,7 @@ func main() {
270266
log.Println("Adding a version directive to the go.mod file as the modules.txt does not have explicit annotations")
271267
if !addVersionToMod(goMod, "1.13") {
272268
log.Println("Failed to add a version to the go.mod file to fix explicitly required package bug; not using vendored dependencies")
273-
modMode = modModIfSupported()
269+
modMode = ModMod
274270
}
275271
}
276272
}
@@ -435,7 +431,7 @@ func main() {
435431
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
436432
// the vendor directory up to date.
437433
if !checkVendor() {
438-
modMode = modModIfSupported()
434+
modMode = ModMod
439435
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
440436
}
441437
}
@@ -507,14 +503,14 @@ func main() {
507503
log.Fatalf("Unable to determine current directory: %s\n", err.Error())
508504
}
509505

510-
var cmd *exec.Cmd
511-
if depMode == GoGetWithModules && modMode.String() != "" {
512-
log.Printf("Running extractor command '%s %s ./...' from directory '%s'.\n", extractor, modMode, cwd)
513-
cmd = exec.Command(extractor, modMode.String(), "./...")
514-
} else {
515-
log.Printf("Running extractor command '%s ./...' from directory '%s'.\n", extractor, cwd)
516-
cmd = exec.Command(extractor, "./...")
506+
extractorArgs := []string{}
507+
if depMode == GoGetWithModules {
508+
extractorArgs = append(extractorArgs, modMode.argsForGoVersion(getEnvGoVersion())...)
517509
}
510+
extractorArgs = append(extractorArgs, "./...")
511+
512+
log.Printf("Running extractor command '%s %v' from directory '%s'.\n", extractor, extractorArgs, cwd)
513+
cmd := exec.Command(extractor, extractorArgs...)
518514
cmd.Stdout = os.Stdout
519515
cmd.Stderr = os.Stderr
520516
err = cmd.Run()

0 commit comments

Comments
 (0)