From 0f8f036350f7c8b134f824ffe5c40349bfe1f8a8 Mon Sep 17 00:00:00 2001 From: Andrey Kalmykov Date: Wed, 25 Dec 2024 01:43:58 +0600 Subject: [PATCH] atmos.yaml --- pkg/config/config.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index df60ff8d53..6660ebf8ce 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "os" "path/filepath" "runtime" @@ -14,6 +15,7 @@ import ( "github.com/spf13/viper" "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/utils" u "github.com/cloudposse/atmos/pkg/utils" "github.com/cloudposse/atmos/pkg/version" ) @@ -99,6 +101,8 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks // system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows) // home dir (~/.atmos) // current directory + // ENV var ATMOS_CLI_CONFIG_PATH + // ENV var ATMOS_REMOTE_CONFIG_URL from GITHUB // ENV vars // Command-line arguments @@ -199,6 +203,18 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks } } + // Process remote config from a GITHUB URL from the path in ENV var `ATMOS_REMOTE_CONFIG_URL` + configFilePath6 := os.Getenv("ATMOS_REMOTE_CONFIG_URL") + if len(configFilePath6) > 0 { + found, err = processRemoteConfigFile(atmosConfig, configFilePath6, v) + if err != nil { + return atmosConfig, err + } + if found { + configFound = true + } + } + if !configFound { // If `atmos.yaml` not found, use the default config // Set `ATMOS_LOGS_LEVEL` ENV var to "Debug" to see the message about Atmos using the default CLI config @@ -382,3 +398,34 @@ func processConfigFile( return true, nil } + +// processRemoteConfigFile attempts to download and merge a remote atmos.yaml +// from a URL. It currently only supports GitHub URLs. +func processRemoteConfigFile( + atmosConfig schema.AtmosConfiguration, + rawURL string, + v *viper.Viper, +) (bool, error) { + parsedURL, err := url.Parse(rawURL) + if err != nil { + u.LogWarning(atmosConfig, fmt.Sprintf("Failed to parse remote config URL '%s': %s", rawURL, err.Error())) + return false, nil + } + + if parsedURL.Scheme != "https" || parsedURL.Host != "github.com" { + return false, nil + } + + data, err := utils.DownloadFileFromGitHub(rawURL) + if err != nil { + u.LogWarning(atmosConfig, fmt.Sprintf("Failed to download remote config from GitHub '%s': %s", rawURL, err.Error())) + return false, nil + } + + err = v.MergeConfig(bytes.NewReader(data)) + if err != nil { + return false, fmt.Errorf("failed to merge remote config from GitHub '%s': %w", rawURL, err) + } + + return true, nil +}