|
17 | 17 |
|
18 | 18 | import static com.redhat.exhort.impl.ExhortApi.debugLoggingIsNeeded; |
19 | 19 |
|
| 20 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
20 | 23 | import com.github.packageurl.MalformedPackageURLException; |
21 | 24 | import com.github.packageurl.PackageURL; |
22 | 25 | import com.redhat.exhort.Api; |
|
40 | 43 | import java.util.List; |
41 | 44 | import java.util.Map; |
42 | 45 | import java.util.Objects; |
43 | | -import java.util.Optional; |
44 | 46 | import java.util.TreeMap; |
45 | 47 | import java.util.logging.Logger; |
46 | 48 | import java.util.regex.Pattern; |
@@ -69,7 +71,7 @@ public String getMainModuleVersion() { |
69 | 71 | public GoModulesProvider(Path manifest) { |
70 | 72 | super(Type.GOLANG, manifest); |
71 | 73 | this.goExecutable = Operations.getExecutable("go", "version"); |
72 | | - this.goEnvironmentVariableForPurl = getQualifiers(true); |
| 74 | + this.goEnvironmentVariableForPurl = getQualifiers(); |
73 | 75 | this.mainModuleVersion = getDefaultMainModuleVersion(); |
74 | 76 | } |
75 | 77 |
|
@@ -379,45 +381,39 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine |
379 | 381 | .collect(Collectors.toList()); |
380 | 382 | } |
381 | 383 |
|
382 | | - private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) { |
383 | | - if (includeOsAndArch) { |
384 | | - String goEnvironmentVariables = Operations.runProcessGetOutput(null, goExecutable, "env"); |
385 | | - var qualifiers = new TreeMap<String, String>(); |
386 | | - qualifiers.put("type", "module"); |
387 | | - Optional<String> hostArch = |
388 | | - getEnvironmentVariable(goEnvironmentVariables, GO_HOST_ARCHITECTURE_ENV_NAME); |
389 | | - Optional<String> hostOS = |
390 | | - getEnvironmentVariable(goEnvironmentVariables, GO_HOST_OPERATION_SYSTEM_ENV_NAME); |
391 | | - if (hostArch.isPresent()) { |
392 | | - qualifiers.put("goarch", hostArch.get()); |
393 | | - } |
394 | | - if (hostOS.isPresent()) { |
395 | | - qualifiers.put("goos", hostOS.get()); |
396 | | - } |
397 | | - return qualifiers; |
| 384 | + private TreeMap<String, String> getQualifiers() { |
| 385 | + var goEnvironmentVariables = getGoEnvironmentVariables(); |
| 386 | + var qualifiers = new TreeMap<String, String>(); |
| 387 | + qualifiers.put("type", "module"); |
| 388 | + if (goEnvironmentVariables.containsKey(GO_HOST_ARCHITECTURE_ENV_NAME)) { |
| 389 | + qualifiers.put("goarch", goEnvironmentVariables.get(GO_HOST_ARCHITECTURE_ENV_NAME)); |
| 390 | + } |
| 391 | + if (goEnvironmentVariables.containsKey(GO_HOST_OPERATION_SYSTEM_ENV_NAME)) { |
| 392 | + qualifiers.put("goos", goEnvironmentVariables.get(GO_HOST_OPERATION_SYSTEM_ENV_NAME)); |
398 | 393 | } |
399 | 394 |
|
400 | | - return new TreeMap<>(Map.of("type", "module")); |
| 395 | + return qualifiers; |
401 | 396 | } |
402 | 397 |
|
403 | | - private static Optional<String> getEnvironmentVariable( |
404 | | - String goEnvironmentVariables, String envName) { |
405 | | - int i = goEnvironmentVariables.indexOf(String.format("%s=", envName)); |
406 | | - if (i == -1) { |
407 | | - return Optional.empty(); |
408 | | - } |
409 | | - int beginIndex = i + String.format("%s=", envName).length(); |
410 | | - int endOfLineIndex = |
411 | | - goEnvironmentVariables.substring(beginIndex).indexOf(System.lineSeparator()); |
412 | | - |
413 | | - String envValue; |
414 | | - if (endOfLineIndex == -1) { |
415 | | - envValue = goEnvironmentVariables.substring(beginIndex); |
416 | | - } else { |
417 | | - envValue = goEnvironmentVariables.substring(beginIndex).substring(0, endOfLineIndex); |
| 398 | + private Map<String, String> getGoEnvironmentVariables() { |
| 399 | + String goEnvironmentVariables = |
| 400 | + Operations.runProcessGetOutput(null, goExecutable, "env", "--json"); |
| 401 | + JsonNode tree; |
| 402 | + try { |
| 403 | + tree = new ObjectMapper().readTree(goEnvironmentVariables); |
| 404 | + } catch (JsonProcessingException e) { |
| 405 | + log.warning(String.format("Failed to parse go environment variables: %s", e.getMessage())); |
| 406 | + return new HashMap<>(); |
418 | 407 | } |
419 | | - |
420 | | - return Optional.of(envValue.replaceAll("\"", "")); |
| 408 | + var envMap = new HashMap<String, String>(); |
| 409 | + tree.fields() |
| 410 | + .forEachRemaining( |
| 411 | + entry -> { |
| 412 | + String key = entry.getKey(); |
| 413 | + String value = entry.getValue().asText(); |
| 414 | + envMap.put(key, value); |
| 415 | + }); |
| 416 | + return envMap; |
421 | 417 | } |
422 | 418 |
|
423 | 419 | private String buildGoModulesDependencies(Path manifestPath) { |
|
0 commit comments