Skip to content

Commit caa7ea6

Browse files
committed
fix: allow missing go env vars
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent f0db270 commit caa7ea6

4 files changed

Lines changed: 33 additions & 25 deletions

File tree

src/main/java/com/redhat/exhort/providers/GoModulesProvider.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Objects;
43+
import java.util.Optional;
4344
import java.util.TreeMap;
4445
import java.util.logging.Logger;
4546
import java.util.regex.Pattern;
@@ -52,6 +53,7 @@
5253
public final class GoModulesProvider extends Provider {
5354

5455
public static final String PROP_EXHORT_GO_MVS_LOGIC_ENABLED = "EXHORT_GO_MVS_LOGIC_ENABLED";
56+
private static final String GO_BINARY = "go";
5557
private static final Logger log = LoggersFactory.getLogger(GoModulesProvider.class.getName());
5658
private static final String GO_HOST_ARCHITECTURE_ENV_NAME = "GOHOSTARCH";
5759
private static final String GO_HOST_OPERATION_SYSTEM_ENV_NAME = "GOHOSTOS";
@@ -67,8 +69,8 @@ public String getMainModuleVersion() {
6769

6870
public GoModulesProvider(Path manifest) {
6971
super(Type.GOLANG, manifest);
70-
this.goExecutable = Operations.getExecutable("go", "version");
71-
this.goEnvironmentVariableForPurl = getQualifiers(true);
72+
this.goExecutable = Operations.getExecutable(GO_BINARY, "version");
73+
this.goEnvironmentVariableForPurl = getQualifiers();
7274
this.mainModuleVersion = getDefaultMainModuleVersion();
7375
}
7476

@@ -320,9 +322,9 @@ private Sbom buildSbomFromGraph(
320322

321323
private Map<String, List<String>> getFinalPackagesVersionsForModule(
322324
Map<String, List<String>> edges, Path manifestPath) {
323-
Operations.runProcessGetOutput(manifestPath.getParent(), "go", "mod", "download");
325+
Operations.runProcessGetOutput(manifestPath.getParent(), GO_BINARY, "mod", "download");
324326
String finalVersionsForAllModules =
325-
Operations.runProcessGetOutput(manifestPath.getParent(), "go", "list", "-m", "all");
327+
Operations.runProcessGetOutput(manifestPath.getParent(), GO_BINARY, "list", "-m", "all");
326328
Map<String, String> finalModulesVersions =
327329
Arrays.stream(finalVersionsForAllModules.split(System.lineSeparator()))
328330
.filter(string -> string.trim().split(" ").length == 2)
@@ -378,26 +380,32 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine
378380
.collect(Collectors.toList());
379381
}
380382

381-
private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) {
382-
if (includeOsAndArch) {
383-
String goEnvironmentVariables = Operations.runProcessGetOutput(null, goExecutable, "env");
384-
String hostArch =
385-
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_ARCHITECTURE_ENV_NAME);
386-
String hostOS =
387-
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_OPERATION_SYSTEM_ENV_NAME);
388-
return new TreeMap<>(Map.of("type", "module", "goos", hostOS, "goarch", hostArch));
383+
private TreeMap<String, String> getQualifiers() {
384+
String goEnvironmentVariables = Operations.runProcessGetOutput(null, goExecutable, "env");
385+
var hostArch = getEnvironmentVariable(goEnvironmentVariables, GO_HOST_ARCHITECTURE_ENV_NAME);
386+
var hostOS = getEnvironmentVariable(goEnvironmentVariables, GO_HOST_OPERATION_SYSTEM_ENV_NAME);
387+
var qualifiers = new TreeMap<String, String>();
388+
qualifiers.put("type", "module");
389+
if (hostOS.isPresent()) {
390+
qualifiers.put("goos", hostOS.get());
389391
}
390-
391-
return new TreeMap<>(Map.of("type", "module"));
392+
if (hostArch.isPresent()) {
393+
qualifiers.put("goarch", hostArch.get());
394+
}
395+
return qualifiers;
392396
}
393397

394-
private static String getEnvironmentVariable(String goEnvironmentVariables, String envName) {
398+
private static Optional<String> getEnvironmentVariable(
399+
String goEnvironmentVariables, String envName) {
395400
int i = goEnvironmentVariables.indexOf(String.format("%s=", envName));
401+
if (i == -1) {
402+
return Optional.empty();
403+
}
396404
int beginIndex = i + String.format("%s=", envName).length();
397405
int endOfLineIndex =
398406
goEnvironmentVariables.substring(beginIndex).indexOf(System.lineSeparator());
399407
String envValue = goEnvironmentVariables.substring(beginIndex).substring(0, endOfLineIndex);
400-
return envValue.replaceAll("\"", "");
408+
return Optional.of(envValue.replaceAll("\"", ""));
401409
}
402410

403411
private String buildGoModulesDependencies(Path manifestPath) {

src/main/java/com/redhat/exhort/providers/JavaMavenProvider.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public int hashCode() {
372372
private String selectMvnRuntime(final Path manifestPath) {
373373
boolean preferWrapper = Operations.getWrapperPreference(MVN);
374374
if (preferWrapper) {
375-
String wrapperName = isWindows() ? "mvnw.cmd" : "mvnw";
375+
String wrapperName = Operations.isWindows() ? "mvnw.cmd" : "mvnw";
376376
String mvnw = traverseForMvnw(wrapperName, manifestPath.toString());
377377
if (mvnw != null) {
378378
try {
@@ -423,13 +423,9 @@ public static String traverseForMvnw(
423423
public static String normalizePath(String thePath) {
424424
Path normalized = Paths.get(thePath).toAbsolutePath().normalize();
425425
String result = normalized.toString();
426-
if (isWindows()) {
426+
if (Operations.isWindows()) {
427427
result = result.toLowerCase();
428428
}
429429
return result;
430430
}
431-
432-
private static boolean isWindows() {
433-
return System.getProperty("os.name").toLowerCase().contains("win");
434-
}
435431
}

src/main/java/com/redhat/exhort/providers/JavaScriptYarnProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
public final class JavaScriptYarnProvider extends JavaScriptProvider {
3434

3535
public static final String LOCK_FILE = "yarn.lock";
36-
public static final String CMD_NAME = "yarn";
36+
public static final String CMD_NAME = Operations.isWindows() ? "yarn.cmd" : "yarn";
3737

38-
private static final Pattern versionPattern = Pattern.compile("^([0-9]+)\\.");
38+
private static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.");
3939

4040
private final YarnProcessor processor;
4141

@@ -79,7 +79,7 @@ private YarnProcessor resolveVersion(Path manifestPath) {
7979
var output =
8080
Operations.runProcessGetOutput(
8181
manifestPath.getParent(), new String[] {cmd, "-v"}, getExecEnvAsArgs());
82-
var matcher = versionPattern.matcher(output);
82+
var matcher = VERSION_PATTERN.matcher(output);
8383
if (matcher.find()) {
8484
var majorVersion = Integer.parseInt(matcher.group(1));
8585
if (majorVersion == 1) {

src/main/java/com/redhat/exhort/tools/Operations.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,8 @@ public static Optional<String> getGitRootDir(String cwd) {
349349
}
350350
return Optional.empty();
351351
}
352+
353+
public static boolean isWindows() {
354+
return System.getProperty("os.name").toLowerCase().contains("win");
355+
}
352356
}

0 commit comments

Comments
 (0)