Skip to content

Commit be25bf7

Browse files
Roshan-Rjlebon
authored andcommitted
refactor(kola): refactor manifest parsing into helper
Move stream and osversion extraction to getStreamAndOsVersionFromManifest() to improve readability and maintainability of ParseDenyListYaml.
1 parent 7fd488c commit be25bf7

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

mantle/kola/harness.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,41 @@ type InitConfigData struct {
376376
ConfigVariant string `json:"coreos-assembler.config-variant"`
377377
}
378378

379+
func getStreamAndOsVersionFromManifest() (string, string, error) {
380+
// Look for the right manifest, taking into account the variant
381+
var manifest ManifestData
382+
var pathToManifest string
383+
pathToInitConfig := filepath.Join(Options.CosaWorkdir, "src/config.json")
384+
initConfigFile, err := os.ReadFile(pathToInitConfig)
385+
if os.IsNotExist(err) {
386+
// No variant config found. Let's read the default manifest
387+
pathToManifest = filepath.Join(Options.CosaWorkdir, "src/config/manifest.yaml")
388+
} else if err != nil {
389+
// Unexpected error
390+
return "", "", err
391+
} else {
392+
// Figure out the variant and read the corresponding manifests
393+
var initConfig InitConfigData
394+
err = json.Unmarshal(initConfigFile, &initConfig)
395+
if err != nil {
396+
return "", "", err
397+
}
398+
pathToManifest = filepath.Join(Options.CosaWorkdir, fmt.Sprintf("src/config/manifest-%s.yaml", initConfig.ConfigVariant))
399+
}
400+
manifestFile, err := os.ReadFile(pathToManifest)
401+
if err != nil {
402+
return "", "", err
403+
}
404+
err = yaml.Unmarshal(manifestFile, &manifest)
405+
if err != nil {
406+
return "", "", err
407+
}
408+
409+
stream := manifest.Variables.Stream
410+
osversion := manifest.Variables.OsVersion
411+
return stream, osversion, nil
412+
}
413+
379414
func ParseDenyListYaml(pltfrm string) error {
380415
var objs []DenyListObj
381416

@@ -401,37 +436,10 @@ func ParseDenyListYaml(pltfrm string) error {
401436

402437
// Get the stream and osversion variables from the manifest since DenylistStream is not specified
403438
if len(DenylistStream) == 0 {
404-
// Look for the right manifest, taking into account the variant
405-
var manifest ManifestData
406-
var pathToManifest string
407-
pathToInitConfig := filepath.Join(Options.CosaWorkdir, "src/config.json")
408-
initConfigFile, err := os.ReadFile(pathToInitConfig)
409-
if os.IsNotExist(err) {
410-
// No variant config found. Let's read the default manifest
411-
pathToManifest = filepath.Join(Options.CosaWorkdir, "src/config/manifest.yaml")
412-
} else if err != nil {
413-
// Unexpected error
414-
return err
415-
} else {
416-
// Figure out the variant and read the corresponding manifests
417-
var initConfig InitConfigData
418-
err = json.Unmarshal(initConfigFile, &initConfig)
419-
if err != nil {
420-
return err
421-
}
422-
pathToManifest = filepath.Join(Options.CosaWorkdir, fmt.Sprintf("src/config/manifest-%s.yaml", initConfig.ConfigVariant))
423-
}
424-
manifestFile, err := os.ReadFile(pathToManifest)
425-
if err != nil {
426-
return err
427-
}
428-
err = yaml.Unmarshal(manifestFile, &manifest)
439+
stream, osversion, err = getStreamAndOsVersionFromManifest()
429440
if err != nil {
430441
return err
431442
}
432-
433-
stream = manifest.Variables.Stream
434-
osversion = manifest.Variables.OsVersion
435443
} else {
436444
stream = DenylistStream
437445
}

0 commit comments

Comments
 (0)