Skip to content

Commit 9bb413e

Browse files
committed
fix: parse go env in json format
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 752384e commit 9bb413e

1 file changed

Lines changed: 32 additions & 36 deletions

File tree

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

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import static com.redhat.exhort.impl.ExhortApi.debugLoggingIsNeeded;
1919

20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.JsonNode;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
2023
import com.github.packageurl.MalformedPackageURLException;
2124
import com.github.packageurl.PackageURL;
2225
import com.redhat.exhort.Api;
@@ -40,7 +43,6 @@
4043
import java.util.List;
4144
import java.util.Map;
4245
import java.util.Objects;
43-
import java.util.Optional;
4446
import java.util.TreeMap;
4547
import java.util.logging.Logger;
4648
import java.util.regex.Pattern;
@@ -69,7 +71,7 @@ public String getMainModuleVersion() {
6971
public GoModulesProvider(Path manifest) {
7072
super(Type.GOLANG, manifest);
7173
this.goExecutable = Operations.getExecutable("go", "version");
72-
this.goEnvironmentVariableForPurl = getQualifiers(true);
74+
this.goEnvironmentVariableForPurl = getQualifiers();
7375
this.mainModuleVersion = getDefaultMainModuleVersion();
7476
}
7577

@@ -379,45 +381,39 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine
379381
.collect(Collectors.toList());
380382
}
381383

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));
398393
}
399394

400-
return new TreeMap<>(Map.of("type", "module"));
395+
return qualifiers;
401396
}
402397

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<>();
418407
}
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;
421417
}
422418

423419
private String buildGoModulesDependencies(Path manifestPath) {

0 commit comments

Comments
 (0)