Skip to content

Commit

Permalink
feat: supports specification of environment variables in config files.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Apr 12, 2024
1 parent e9e91ab commit 94510e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
50 changes: 36 additions & 14 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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\")")
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ default:
plugins:
- store-dynamodb
- store-redis

# Map of environment variables to
env:
IMPOSTER_EXAMPLE: "some-value"
```
## Environment variables
Expand Down

0 comments on commit 94510e6

Please sign in to comment.