Skip to content

Commit

Permalink
feat: Cli input (#8)
Browse files Browse the repository at this point in the history
* feat: Allow cli input for variables

* chore: example update
  • Loading branch information
jeff-roche committed Jun 17, 2022
1 parent 879c5b4 commit f3cc23a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion example.biome.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/lib/setters/cli_setter.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions src/lib/setters/setter_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package setters

import (
"fmt"
"os"
)

func GetEnvironmentSetter(key string, node interface{}) (EnvironmentSetter, error) {
Expand Down Expand Up @@ -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)
}

0 comments on commit f3cc23a

Please sign in to comment.