Skip to content

Commit feed996

Browse files
committed
fix: unix output in windows envs
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 88c9143 commit feed996

3 files changed

Lines changed: 47 additions & 44 deletions

File tree

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

Lines changed: 39 additions & 42 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,14 +71,14 @@ 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

7678
@Override
7779
public Content provideStack() throws IOException {
7880
// check for custom executable
79-
Sbom sbom = getDependenciesSbom(manifest, true);
81+
var sbom = getDependenciesSbom(manifest, true);
8082
return new Content(
8183
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
8284
}
@@ -138,7 +140,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
138140
boolean matchManifestVersions =
139141
Environment.getBoolean(Provider.PROP_MATCH_MANIFEST_VERSIONS, false);
140142
if (matchManifestVersions) {
141-
String[] goModGraphLines = goModulesResult.split(System.lineSeparator());
143+
String[] goModGraphLines = goModulesResult.split(Operations.GENERIC_LINE_SEPARATOR);
142144
performManifestVersionsCheck(goModGraphLines, manifestPath);
143145
}
144146
if (!buildTree) {
@@ -152,7 +154,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
152154
private void performManifestVersionsCheck(String[] goModGraphLines, Path manifestPath) {
153155
try {
154156
String goModLines = Files.readString(manifestPath);
155-
String[] lines = goModLines.split(System.lineSeparator());
157+
String[] lines = goModLines.split(Operations.GENERIC_LINE_SEPARATOR);
156158
String root = getParentVertex(goModGraphLines[0]);
157159
List<String> comparisonLines =
158160
Arrays.stream(goModGraphLines)
@@ -271,7 +273,8 @@ private Sbom buildSbomFromGraph(
271273
// iterate over go mod graph line by line and create map , with each entry to contain module as
272274
// a key , and
273275
// value of list of that module' dependencies.
274-
List<String> linesList = Arrays.asList(goModulesResult.split(System.lineSeparator()));
276+
List<String> linesList =
277+
Arrays.asList(goModulesResult.split(Operations.GENERIC_LINE_SEPARATOR));
275278

276279
int startingIndex = 0;
277280
for (String line : linesList) {
@@ -325,7 +328,7 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
325328
String finalVersionsForAllModules =
326329
Operations.runProcessGetOutput(manifestPath.getParent(), "go", "list", "-m", "all");
327330
Map<String, String> finalModulesVersions =
328-
Arrays.stream(finalVersionsForAllModules.split(System.lineSeparator()))
331+
Arrays.stream(finalVersionsForAllModules.split(Operations.GENERIC_LINE_SEPARATOR))
329332
.filter(string -> string.trim().split(" ").length == 2)
330333
.collect(
331334
Collectors.toMap(
@@ -379,45 +382,39 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine
379382
.collect(Collectors.toList());
380383
}
381384

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;
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));
398394
}
399395

400-
return new TreeMap<>(Map.of("type", "module"));
396+
return qualifiers;
401397
}
402398

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);
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+
log.warning(String.format("Failed to parse go environment variables: %s", e.getMessage()));
407+
return new HashMap<>();
418408
}
419-
420-
return Optional.of(envValue.replaceAll("\"", ""));
409+
var envMap = new HashMap<String, String>();
410+
tree.fields()
411+
.forEachRemaining(
412+
entry -> {
413+
String key = entry.getKey();
414+
String value = entry.getValue().asText();
415+
envMap.put(key, value);
416+
});
417+
return envMap;
421418
}
422419

423420
private String buildGoModulesDependencies(Path manifestPath) {
@@ -437,7 +434,7 @@ private String buildGoModulesDependencies(Path manifestPath) {
437434
}
438435

439436
private Sbom buildSbomFromList(String golangDeps, List<PackageURL> ignoredDeps) {
440-
String[] allModulesFlat = golangDeps.split(System.lineSeparator());
437+
String[] allModulesFlat = golangDeps.split(Operations.GENERIC_LINE_SEPARATOR);
441438
String parentVertex = getParentVertex(allModulesFlat[0]);
442439
PackageURL root = toPurl(parentVertex, "@", this.goEnvironmentVariableForPurl);
443440
// Get only direct dependencies of root package/module, and that's it.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.github.packageurl.PackageURL;
2121
import com.redhat.exhort.providers.javascript.model.Manifest;
2222
import com.redhat.exhort.sbom.Sbom;
23+
import com.redhat.exhort.tools.Operations;
2324
import java.nio.file.Path;
2425
import java.util.Map;
2526
import java.util.TreeMap;
@@ -36,6 +37,7 @@ public YarnBerryProcessor(String packageManager, Manifest manifest) {
3637
super(packageManager, manifest);
3738
}
3839

40+
@Override
3941
public String[] installCmd(Path manifestDir) {
4042
if (manifestDir != null) {
4143
return new String[] {
@@ -89,7 +91,9 @@ private boolean isRoot(String name) {
8991

9092
@Override
9193
public String parseDepTreeOutput(String output) {
92-
return "[" + output.trim().replace(System.lineSeparator(), "").replace("}{", "},{") + "]";
94+
return "["
95+
+ output.trim().replaceAll(Operations.GENERIC_LINE_SEPARATOR, "").replace("}{", "},{")
96+
+ "]";
9397
}
9498

9599
private PackageURL purlFromNode(String normalizedLocator, JsonNode node) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
/** Utility class used for executing process on the operating system. * */
3636
public final class Operations {
37-
37+
// Some package providers might return Unix output in Windows, so we need to use a generic line
38+
// separator
39+
public static final String GENERIC_LINE_SEPARATOR = "\\r?\\n";
3840
private static final Logger log = LoggersFactory.getLogger(Operations.class.getName());
3941

4042
private Operations() {

0 commit comments

Comments
 (0)