Skip to content

Commit

Permalink
Merge pull request #1 from springload/feature/dotenv
Browse files Browse the repository at this point in the history
Add dotenv command to generate .env
  • Loading branch information
jesserockz authored Oct 1, 2018
2 parents a2386bb + a5f0645 commit e68f13d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.10-alpine as build
FROM golang:1.11-alpine as build

RUN apk update && apk add git
RUN go get -u github.com/golang/dep/cmd/dep
Expand Down
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SSM Parent
----------

This is a parent process for Docker with one addition: it can read from AWS SSM Parameter store.
This is mostly a parent process for Docker with one addition: it can read from AWS SSM Parameter store.

The way it works is that ssm-parent can be used as an entrypoint for Docker. Firstly, it retrieves all specified parameters, then injects them to the environment,
and finally runs the command.
Expand Down Expand Up @@ -45,7 +45,6 @@ The result will be merged as this:
That should be pretty self-explanatory.

```
$ssm-parent help <aws:hosting>
SSM-Parent is a docker entrypoint.
It gets specified parameters (possibly secret) from AWS SSM Parameter Store,
Expand All @@ -55,11 +54,13 @@ Usage:
ssm-parent [command]
Available Commands:
dotenv Writes dotenv file
help Help about any command
print Prints the specified parameters.
run Runs the specified command
Flags:
-e, --expand Expand arguments and values using shell-style syntax
-h, --help help for ssm-parent
-n, --name stringArray Name of the SSM parameter to retrieve. Can be specified multiple times.
-p, --path stringArray Path to a SSM parameter. Can be specified multiple times.
Expand Down Expand Up @@ -96,6 +97,16 @@ envsubst < /etc/Caddyfile.env > /etc/Caddyfile
exec $@
```

### .env file generation

Sometimes you just want a .env file, and now it is also possible.

Just specify all the same parameters, but use `dotenv` command instead with a filename to generate `.env` file.
```
./ssm-parent dotenv -r -p /project/environment dotenv.env
2018/10/01 16:37:59 info Wrote the .env file filename=dotenv.env
```

### How to build

This project uses https://github.com/golang/dep as a dependency manager. Go v.1.10.1 was used.
Expand Down
48 changes: 48 additions & 0 deletions cmd/dotenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"github.com/apex/log"
"github.com/imdario/mergo"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/springload/ssm-parent/ssm"
)

// dotenvCmd represents the dotenv command
var dotenvCmd = &cobra.Command{
Use: "dotenv <filename>",
Short: "Writes dotenv file",
Long: `Gathers parameters from SSM Parameter store, writes .env file and exits`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
megamap := make(map[string]string)
parameters, err := ssm.GetParameters(names, paths, expand, strict, recursive)
if err != nil {
log.WithError(err).Fatal("Can't get parameters")
}
for _, parameter := range parameters {
err = mergo.Merge(&megamap, &parameter, mergo.WithOverride)
if err != nil {
log.WithError(err).Fatal("Can't merge maps")
}
}
for key, value := range megamap {
if expand {
megamap[key] = ssm.ExpandValue(value)
}
}

err = godotenv.Write(megamap, args[0])
if err != nil {
log.WithError(err).Fatal("Can't write the dotenv file")
} else {
log.WithFields(log.Fields{"filename": args[0]}).Info("Wrote the .env file")

}
},
}

func init() {
rootCmd.AddCommand(dotenvCmd)

}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func init() {
rootCmd.PersistentFlags().StringArrayVarP(&names, "name", "n", []string{}, "Name of the SSM parameter to retrieve. Can be specified multiple times.")
rootCmd.PersistentFlags().BoolVarP(&recursive, "recursive", "r", false, "Walk through the provided SSM paths recursively.")
rootCmd.PersistentFlags().BoolVarP(&strict, "strict", "s", false, "Strict mode. Fail if found less parameters than number of names.")
rootCmd.PersistentFlags().BoolVarP(&expand, "expand", "e", false, "Expand arguments and values using /bin/sh")
rootCmd.PersistentFlags().BoolVarP(&expand, "expand", "e", false, "Expand arguments and values using shell-style syntax")
}

0 comments on commit e68f13d

Please sign in to comment.