Check platform for module config

This commit is contained in:
2024-07-20 00:39:51 +02:00
parent 3ae38d8629
commit fcaddcf60c
4 changed files with 76 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"alma/helpers"
"cmp"
"encoding/json"
"io"
@@ -36,8 +37,9 @@ func LoadModuleConfiguration(moduleConfigPath string) *ModuleConfiguration {
platformKeys := make([]string, 0, len(moduleConfigurationRoot))
for key := range moduleConfigurationRoot {
if key != "default" {
if key != "default" && helpers.IsOnPlatform(key) {
platformKeys = append(platformKeys, key)
println("Adding platform '" + key + "'")
}
}
@@ -51,7 +53,7 @@ func LoadModuleConfiguration(moduleConfigPath string) *ModuleConfiguration {
}
for _, platformKey := range platformKeys {
// TODO: apply only the platform-specific configuration
println("Merging platform '" + platformKey + "'")
moduleConfiguration.Merge(moduleConfigurationRoot[platformKey])
}

62
src/helpers/osinfo.go Normal file
View File

@@ -0,0 +1,62 @@
package helpers
import (
"bufio"
"os"
"runtime"
"strings"
)
const OsIdentifierDefault = "default"
const OsIdentifierLinux = "linux"
const OsIdentifierMac = "macos"
const OsIdentifierWindows = "windows"
const osReleasePathLinux = "/etc/os-release"
func GetOsIdentifier() string {
baseIdentifier := getBaseOsIdentifier()
return baseIdentifier + "-" + getNormalizedArch()
}
func IsOnPlatform(platform string) bool {
return strings.Index(GetOsIdentifier(), platform) == 0
}
func getBaseOsIdentifier() string {
switch runtime.GOOS {
case "windows":
return OsIdentifierWindows
case "darwin":
return OsIdentifierMac
case "linux":
file, err := os.Open(osReleasePathLinux)
if err != nil {
return OsIdentifierLinux
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.ToLower(scanner.Text())
if strings.HasPrefix(line, "id=") {
return OsIdentifierLinux + "-" + strings.Trim(line[3:], "\"")
}
}
}
return runtime.GOOS
}
func getNormalizedArch() string {
switch runtime.GOARCH {
case "amd64":
return "x64"
case "386":
return "x86"
}
return runtime.GOARCH
}

View File

@@ -5,5 +5,13 @@
"test_folder": "test_folder",
"test_folder2": "test_folder3"
}
},
"win": {
},
"linux": {
},
"linux-fedora-x64": {
},
"linux-fedora": {
}
}