diff --git a/.vscode/launch.json b/.vscode/launch.json index a126b15..fcbcd01 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,8 @@ "type": "delve", "request": "launch", "program": "${workspaceFolder}/src", - "args": ["link", "test_module", "-d"], + "args1": ["link", "test_module", "-d"], + "args": ["info"], "dlvCwd": "${workspaceFolder}/src" } ] diff --git a/src/config/moduleConfiguration.go b/src/config/moduleConfiguration.go index e1a69e8..248264c 100644 --- a/src/config/moduleConfiguration.go +++ b/src/config/moduleConfiguration.go @@ -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]) } diff --git a/src/helpers/osinfo.go b/src/helpers/osinfo.go new file mode 100644 index 0000000..352dc6c --- /dev/null +++ b/src/helpers/osinfo.go @@ -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 +} diff --git a/src/test_module/.alma-config.json b/src/test_module/.alma-config.json index 5af3422..f79f783 100644 --- a/src/test_module/.alma-config.json +++ b/src/test_module/.alma-config.json @@ -5,5 +5,13 @@ "test_folder": "test_folder", "test_folder2": "test_folder3" } + }, + "win": { + }, + "linux": { + }, + "linux-fedora-x64": { + }, + "linux-fedora": { } }