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 ;
4043import java .util .List ;
4144import java .util .Map ;
4245import java .util .Objects ;
43- import java .util .Optional ;
4446import java .util .TreeMap ;
4547import java .util .logging .Logger ;
4648import 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.
0 commit comments