From 94510e60938f56d31a1fbf85524eda18f8cb8513 Mon Sep 17 00:00:00 2001 From: Pete Cornish Date: Fri, 12 Apr 2024 16:29:08 +0100 Subject: [PATCH] feat: supports specification of environment variables in config files. --- cmd/up.go | 50 ++++++++++++++++++++++++++++++++++++-------------- docs/config.md | 4 ++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/cmd/up.go b/cmd/up.go index 5c1de03..dbde1e0 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -21,7 +21,9 @@ import ( "gatehill.io/imposter/engine" "gatehill.io/imposter/fileutil" "gatehill.io/imposter/plugin" + "gatehill.io/imposter/stringutil" "github.com/spf13/cobra" + "github.com/spf13/viper" "os" "os/signal" "path/filepath" @@ -55,7 +57,7 @@ var upCmd = &cobra.Command{ If CONFIG_DIR is not specified, the current working directory is used.`, Args: cobra.RangeArgs(0, 1), Run: func(cmd *cobra.Command, args []string) { - injectExplicitEnvironment() + injectExplicitEnvironment(upFlags.environment) var configDir string if len(args) == 0 { @@ -103,25 +105,13 @@ If CONFIG_DIR is not specified, the current working directory is used.`, Deduplicate: upFlags.deduplicate, EnablePlugins: upFlags.enablePlugins, EnableFileCache: upFlags.enableFileCache, - Environment: upFlags.environment, + Environment: buildStartEnvironment(upFlags.environment), DirMounts: upFlags.dirMounts, } start(&lib, startOptions, configDir, upFlags.restartOnChange) }, } -func injectExplicitEnvironment() { - for _, env := range upFlags.environment { - envParts := strings.Split(env, "=") - if len(envParts) > 1 { - _ = os.Setenv(envParts[0], envParts[1]) - } - } - if upFlags.recursiveConfigScan { - _ = os.Setenv("IMPOSTER_CONFIG_SCAN_RECURSIVE", "true") - } -} - func init() { upCmd.Flags().StringVarP(&upFlags.engineType, "engine-type", "t", "", "Imposter engine type (valid: docker,jvm - default \"docker\")") upCmd.Flags().StringVarP(&upFlags.engineVersion, "version", "v", "", "Imposter engine version (default \"latest\")") @@ -140,6 +130,38 @@ func init() { rootCmd.AddCommand(upCmd) } +func injectExplicitEnvironment(cliEnvArgs []string) { + for _, env := range cliEnvArgs { + envParts := strings.Split(env, "=") + if len(envParts) > 1 { + _ = os.Setenv(envParts[0], envParts[1]) + } + } + if upFlags.recursiveConfigScan { + _ = os.Setenv("IMPOSTER_CONFIG_SCAN_RECURSIVE", "true") + } +} + +func buildStartEnvironment(cliEnvArgs []string) []string { + env := append([]string{}, cliEnvArgs...) + + // include environment variables from CLI config file, under the 'env' key, such as: + // ```yaml + // env: + // IMPOSTER_FOO: bar + // IMPOSTER_BAZ: qux + // ``` + for k, v := range viper.GetStringMapString("env") { + // environment variables passed as command-line arguments take precedence + // over those in the config file + if !stringutil.ContainsPrefix(env, k+"=") { + env = append(env, k+"="+v) + } + } + + return env +} + func start(lib *engine.EngineLibrary, startOptions engine.StartOptions, configDir string, restartOnChange bool) { provider := (*lib).GetProvider(startOptions.Version) mockEngine := provider.Build(configDir, startOptions) diff --git a/docs/config.md b/docs/config.md index 1b78b66..1e8e7fc 100644 --- a/docs/config.md +++ b/docs/config.md @@ -98,6 +98,10 @@ default: plugins: - store-dynamodb - store-redis + +# Map of environment variables to +env: + IMPOSTER_EXAMPLE: "some-value" ``` ## Environment variables