Skip to content

Commit 81745a3

Browse files
authored
fix: parse go env in json format (guacsec#167)
Use the `json` format for the `go env` command to make it OS agnostic Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 5ba8f7c commit 81745a3

1 file changed

Lines changed: 31 additions & 36 deletions

File tree

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

Lines changed: 31 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

@@ -380,45 +382,38 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine
380382
.collect(Collectors.toList());
381383
}
382384

383-
private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) {
384-
if (includeOsAndArch) {
385-
String goEnvironmentVariables = Operations.runProcessGetOutput(null, goExecutable, "env");
386-
var qualifiers = new TreeMap<String, String>();
387-
qualifiers.put("type", "module");
388-
Optional<String> hostArch =
389-
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_ARCHITECTURE_ENV_NAME);
390-
Optional<String> hostOS =
391-
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_OPERATION_SYSTEM_ENV_NAME);
392-
if (hostArch.isPresent()) {
393-
qualifiers.put("goarch", hostArch.get());
394-
}
395-
if (hostOS.isPresent()) {
396-
qualifiers.put("goos", hostOS.get());
397-
}
398-
return qualifiers;
385+
private TreeMap<String, String> getQualifiers() {
386+
var goEnvironmentVariables = getGoEnvironmentVariables();
387+
var qualifiers = new TreeMap<String, String>();
388+
qualifiers.put("type", "module");
389+
if (goEnvironmentVariables.containsKey(GO_HOST_ARCHITECTURE_ENV_NAME)) {
390+
qualifiers.put("goarch", goEnvironmentVariables.get(GO_HOST_ARCHITECTURE_ENV_NAME));
391+
}
392+
if (goEnvironmentVariables.containsKey(GO_HOST_OPERATION_SYSTEM_ENV_NAME)) {
393+
qualifiers.put("goos", goEnvironmentVariables.get(GO_HOST_OPERATION_SYSTEM_ENV_NAME));
399394
}
400395

401-
return new TreeMap<>(Map.of("type", "module"));
396+
return qualifiers;
402397
}
403398

404-
private static Optional<String> getEnvironmentVariable(
405-
String goEnvironmentVariables, String envName) {
406-
int i = goEnvironmentVariables.indexOf(String.format("%s=", envName));
407-
if (i == -1) {
408-
return Optional.empty();
409-
}
410-
int beginIndex = i + String.format("%s=", envName).length();
411-
int endOfLineIndex =
412-
goEnvironmentVariables.substring(beginIndex).indexOf(System.lineSeparator());
413-
414-
String envValue;
415-
if (endOfLineIndex == -1) {
416-
envValue = goEnvironmentVariables.substring(beginIndex);
417-
} else {
418-
envValue = goEnvironmentVariables.substring(beginIndex).substring(0, endOfLineIndex);
399+
private Map<String, String> getGoEnvironmentVariables() {
400+
String goEnvironmentVariables =
401+
Operations.runProcessGetOutput(null, goExecutable, "env", "--json");
402+
JsonNode tree;
403+
try {
404+
tree = new ObjectMapper().readTree(goEnvironmentVariables);
405+
} catch (JsonProcessingException e) {
406+
throw new RuntimeException("Failed to parse go environment variables: " + e.getMessage());
419407
}
420-
421-
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;
422417
}
423418

424419
private String buildGoModulesDependencies(Path manifestPath) {

0 commit comments

Comments
 (0)