diff --git a/README.md b/README.md index e6406b9..e2b58b9 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ $ onstaging ./bin/ci/deploy-service.sh ## Future Plans - Allow inhereting from other biomes in the same file -- Allow setting an environment variable from stdin +- :white_check_mark: Allow setting an environment variable from stdin - :white_check_mark: Implement goreleaser for binary building - :white_check_mark: Use semantic versioning - :white_check_mark: Add a version command diff --git a/example.biome.yaml b/example.biome.yaml index c5c2c8d..a40482e 100644 --- a/example.biome.yaml +++ b/example.biome.yaml @@ -4,7 +4,8 @@ load_env: example_file.env # Load additional envs from a dotenv file environment: # Additional environment vars to load in ENVIRONMENT: staging MY_USEFUL_ENV: "A value I need" - MY_OTHER_ENV: "Another value I need" + CLI_ENV_VAR: # Get the variable from the CLI + from_cli: true MY_AWS_SECRET_ENV: secret_arn: "{{ARN}}" # Secrets manager ARN secret_json_key: "my_super_secret_key" # JSON key in the secret diff --git a/src/lib/setters/cli_setter.go b/src/lib/setters/cli_setter.go new file mode 100644 index 0000000..67b4db4 --- /dev/null +++ b/src/lib/setters/cli_setter.go @@ -0,0 +1,38 @@ +package setters + +import ( + "bufio" + "fmt" + "io" + "os" + "strings" +) + +const CLI_ENVIRONMENT_SETTER_KEY = "from_cli" + +// CLIEnvironmentSetter will pull an env var from the provided io.Reader (stdin) +type CLIEnvironmentSetter struct { + Key string + Value string +} + +func NewCLIEnvironmentSetter(key string, rd io.Reader) (*CLIEnvironmentSetter, error) { + // Get the value from the io.Reader + fmt.Printf("%s: ", key) + reader := bufio.NewReader(rd) + val, err := reader.ReadString('\n') + if err != nil { + return nil, err + } + val = strings.Replace(val, "\n", "", -1) // Remove any newline characters + + // Save and return the setter + return &CLIEnvironmentSetter{ + Key: key, + Value: val, + }, nil +} + +func (s CLIEnvironmentSetter) SetEnv() error { + return os.Setenv(s.Key, s.Value) +} diff --git a/src/lib/setters/setter_helpers.go b/src/lib/setters/setter_helpers.go index 9cdab32..1c241e6 100644 --- a/src/lib/setters/setter_helpers.go +++ b/src/lib/setters/setter_helpers.go @@ -2,6 +2,7 @@ package setters import ( "fmt" + "os" ) func GetEnvironmentSetter(key string, node interface{}) (EnvironmentSetter, error) { @@ -32,5 +33,14 @@ func getComplexSetter(key string, node map[string]interface{}) (EnvironmentSette return NewDragomanEnvironmentSetter(key, val.(string)) } + // CLI Input + if val, exists := node[CLI_ENVIRONMENT_SETTER_KEY]; exists { + if val.(bool) { + return NewCLIEnvironmentSetter(key, os.Stdin) + } else { + return nil, fmt.Errorf("invalid value for %s: %v", CLI_ENVIRONMENT_SETTER_KEY, val) + } + } + return nil, fmt.Errorf("unkown environment config for variable '%s'", key) }