1717
1818import 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 ;
2023import com .github .packageurl .MalformedPackageURLException ;
2124import com .github .packageurl .PackageURL ;
2225import com .redhat .exhort .Api ;
@@ -68,7 +71,7 @@ public String getMainModuleVersion() {
6871 public GoModulesProvider (Path manifest ) {
6972 super (Type .GOLANG , manifest );
7073 this .goExecutable = Operations .getExecutable ("go" , "version" );
71- this .goEnvironmentVariableForPurl = getQualifiers (true );
74+ this .goEnvironmentVariableForPurl = getQualifiers ();
7275 this .mainModuleVersion = getDefaultMainModuleVersion ();
7376 }
7477
@@ -137,7 +140,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
137140 boolean matchManifestVersions =
138141 Environment .getBoolean (Provider .PROP_MATCH_MANIFEST_VERSIONS , false );
139142 if (matchManifestVersions ) {
140- String [] goModGraphLines = goModulesResult .split (System . lineSeparator () );
143+ String [] goModGraphLines = goModulesResult .split (Operations . GENERIC_LINE_SEPARATOR );
141144 performManifestVersionsCheck (goModGraphLines , manifestPath );
142145 }
143146 if (!buildTree ) {
@@ -151,7 +154,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
151154 private void performManifestVersionsCheck (String [] goModGraphLines , Path manifestPath ) {
152155 try {
153156 String goModLines = Files .readString (manifestPath );
154- String [] lines = goModLines .split (System . lineSeparator () );
157+ String [] lines = goModLines .split (Operations . GENERIC_LINE_SEPARATOR );
155158 String root = getParentVertex (goModGraphLines [0 ]);
156159 List <String > comparisonLines =
157160 Arrays .stream (goModGraphLines )
@@ -270,7 +273,8 @@ private Sbom buildSbomFromGraph(
270273 // iterate over go mod graph line by line and create map , with each entry to contain module as
271274 // a key , and
272275 // value of list of that module' dependencies.
273- List <String > linesList = Arrays .asList (goModulesResult .split (System .lineSeparator ()));
276+ List <String > linesList =
277+ Arrays .asList (goModulesResult .split (Operations .GENERIC_LINE_SEPARATOR ));
274278
275279 int startingIndex = 0 ;
276280 for (String line : linesList ) {
@@ -324,7 +328,7 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
324328 String finalVersionsForAllModules =
325329 Operations .runProcessGetOutput (manifestPath .getParent (), "go" , "list" , "-m" , "all" );
326330 Map <String , String > finalModulesVersions =
327- Arrays .stream (finalVersionsForAllModules .split (System . lineSeparator () ))
331+ Arrays .stream (finalVersionsForAllModules .split (Operations . GENERIC_LINE_SEPARATOR ))
328332 .filter (string -> string .trim ().split (" " ).length == 2 )
329333 .collect (
330334 Collectors .toMap (
@@ -378,26 +382,38 @@ private static List<String> collectAllDirectDependencies(List<String> targetLine
378382 .collect (Collectors .toList ());
379383 }
380384
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 ));
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 ));
389394 }
390395
391- return new TreeMap <>( Map . of ( "type" , "module" )) ;
396+ return qualifiers ;
392397 }
393398
394- private static String getEnvironmentVariable (String goEnvironmentVariables , String envName ) {
395- int i = goEnvironmentVariables .indexOf (String .format ("%s=" , envName ));
396- int beginIndex = i + String .format ("%s=" , envName ).length ();
397- int endOfLineIndex =
398- goEnvironmentVariables .substring (beginIndex ).indexOf (System .lineSeparator ());
399- String envValue = goEnvironmentVariables .substring (beginIndex ).substring (0 , endOfLineIndex );
400- return envValue .replaceAll ("\" " , "" );
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 ());
407+ }
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 ;
401417 }
402418
403419 private String buildGoModulesDependencies (Path manifestPath ) {
@@ -417,7 +433,7 @@ private String buildGoModulesDependencies(Path manifestPath) {
417433 }
418434
419435 private Sbom buildSbomFromList (String golangDeps , List <PackageURL > ignoredDeps ) {
420- String [] allModulesFlat = golangDeps .split (System . lineSeparator () );
436+ String [] allModulesFlat = golangDeps .split (Operations . GENERIC_LINE_SEPARATOR );
421437 String parentVertex = getParentVertex (allModulesFlat [0 ]);
422438 PackageURL root = toPurl (parentVertex , "@" , this .goEnvironmentVariableForPurl );
423439 // Get only direct dependencies of root package/module, and that's it.
0 commit comments