Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional to disable strip path and enable uppercasing the name #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions aws-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ const (
formatDotenv = "dotenv"
)

type FormattingOpts struct {
Format string
StripPath bool
Uppercase bool
}

func main() {
if os.Getenv("AWS_ENV_PATH") == "" {
log.Println("aws-env running locally, without AWS_ENV_PATH")
return
}

recursivePtr := flag.Bool("recursive", false, "recursively process parameters on path")
stripPath := flag.Bool("strip-path", true, "remove the AWS_ENV_PATH from the environment variable name")
uppercase := flag.Bool("uppercase", false, "print the env variable in all caps")
format := flag.String("format", formatExports, "output format")
flag.Parse()

Expand All @@ -34,7 +42,8 @@ func main() {
sess := CreateSession()
client := CreateClient(sess)

ExportVariables(client, os.Getenv("AWS_ENV_PATH"), *recursivePtr, *format, "")
formattingOpts := FormattingOpts{Format: *format, StripPath: *stripPath, Uppercase: *uppercase}
ExportVariables(client, os.Getenv("AWS_ENV_PATH"), *recursivePtr, formattingOpts, "")
}

func CreateSession() *session.Session {
Expand All @@ -45,7 +54,7 @@ func CreateClient(sess *session.Session) *ssm.SSM {
return ssm.New(sess)
}

func ExportVariables(client *ssm.SSM, path string, recursive bool, format string, nextToken string) {
func ExportVariables(client *ssm.SSM, path string, recursive bool, opts FormattingOpts, nextToken string) {
input := &ssm.GetParametersByPathInput{
Path: &path,
WithDecryption: aws.Bool(true),
Expand All @@ -63,22 +72,31 @@ func ExportVariables(client *ssm.SSM, path string, recursive bool, format string
}

for _, element := range output.Parameters {
OutputParameter(path, element, format)
OutputParameter(path, element, opts)
}

if output.NextToken != nil {
ExportVariables(client, path, recursive, format, *output.NextToken)
ExportVariables(client, path, recursive, opts, *output.NextToken)
}
}

func OutputParameter(path string, parameter *ssm.Parameter, format string) {
func OutputParameter(path string, parameter *ssm.Parameter, opts FormattingOpts) {
name := *parameter.Name
value := *parameter.Value

env := strings.Replace(strings.Trim(name[len(path):], "/"), "/", "_", -1)
if opts.StripPath {
name = name[len(path):]
}

env := strings.Replace(strings.Trim(name, "/"), "/", "_", -1)

if opts.Uppercase {
env = strings.ToUpper(env)
}

value = strings.Replace(value, "\n", "\\n", -1)

switch format {
switch opts.Format {
case formatExports:
fmt.Printf("export %s=$'%s'\n", env, value)
case formatDotenv:
Expand Down