Skip to content

Commit

Permalink
Add Set Command
Browse files Browse the repository at this point in the history
Add command to set parameters with option to overwrite existing ones.
Update README.md.
Bump version to 1.5.0.
  • Loading branch information
WillJCJ committed Jul 14, 2018
1 parent 018d522 commit 701b60f
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 46 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ already on your `PATH`.

### Curl

Run the below command to download the 1.4.0 binary and add it to
Run the below command to download the 1.5.0 binary and add it to
`/usr/local/bin`.

#### Linux

```console
curl -LO https://github.com/WillJCJ/param/releases/download/1.4.0/param-linux-amd64 && \
curl -LO https://github.com/WillJCJ/param/releases/download/1.5.0/param-linux-amd64 && \
chmod +x param-linux-amd64 && \
sudo mv param-linux-amd64 /usr/local/bin/param
```

#### MacOS

```console
curl -LO https://github.com/WillJCJ/param/releases/download/1.4.0/param-darwin-amd64 && \
curl -LO https://github.com/WillJCJ/param/releases/download/1.5.0/param-darwin-amd64 && \
chmod +x param-darwin-amd64 && \
sudo mv param-darwin-amd64 /usr/local/bin/param
```
Expand All @@ -60,6 +60,10 @@ sudo mv param-darwin-amd64 /usr/local/bin/param

Make sure your terminal session has the correct AWS credentials.

Below is a brief overview for each command.
Full docs for each command can be found at [`/docs`](docs/param.md).


### Copy

Copy a parameter to your clipboard:
Expand Down Expand Up @@ -98,6 +102,26 @@ prefix1.prod.password
prefix2.key
```

### Set

Set a parameter with type `SecureString`:

```console
$ param set parameter_name password123
```

If the parameter already exists, you must specify the `-f` flag
to overwrite it:

```console
$ param set parameter_name password456 -f
```

#### Auto-completion

With shell completion enabled, you can press tab to auto-complete the parameter
names.

### Show

If you'd like to print out the decrypted parameter without copying it the
Expand Down Expand Up @@ -160,9 +184,9 @@ export PARAM_NO_CACHE=1

## Docs

Docs for each command can be found at [`/docs`](docs/param.md)
Docs for each command can be found at [`/docs`](docs/param.md).

They are automatically generated by Cobra.
They are generated by Cobra by running `go run build/generate_docs.go`.

## CI

Expand All @@ -172,16 +196,18 @@ a new tag is pushed to GitHub.
## TODO

- New Commands
- Set command - Command to set parameters
- Delete command - Command to delete parameters
- Maybe with a `--yes` flag to confim.
- Improvements
- Add a flag to specify parameter type.
Currently only works with `SecureString`s.
- Better logging and a verbose option to see what calls are made to AWS.
- Shell Completion
- `zsh` completion doesn't seem to work.
- Update or delete the bash completion cache after creating/deleting
parameters
- Add bash completion for other subcommands and flags.
- Command to reset the cache.
- Write Tests
- Improve Documentation
- More Examples
Expand Down
21 changes: 0 additions & 21 deletions cmd/add.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
var verbose bool

var copyCmd = &cobra.Command{
Use: "copy parameter_name",
Use: "copy name",
Short: "Copy a parameter to clipboard.",
Long: "Copy the specified SSM Parameter from Paramter Store to your clipboard.",
Args: cobra.ExactArgs(1),
Expand Down
11 changes: 2 additions & 9 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
Expand All @@ -16,7 +15,8 @@ var listCmd = &cobra.Command{
Long: `List all parameters from parameter store with an optional prefix.
Results are sorted in alphabetical order.`,
Run: func(cmd *cobra.Command, args []string) {
listParameters()
prefixSlice := strings.Split(prefixes, ",")
param.List(prefixSlice)
},
}

Expand All @@ -25,10 +25,3 @@ func init() {

listCmd.Flags().StringVarP(&prefixes, "prefix", "p", "", "Prefixes to fileter by")
}

func listParameters() {
prefixSlice := strings.Split(prefixes, ",")
for _, param := range param.DescribeParameters(prefixSlice) {
fmt.Println(param)
}
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const (
__custom_func() {
case ${last_command} in
param_copy | param_show)
param_copy | param_set | param_show)
__param_list
return
;;
Expand Down
24 changes: 24 additions & 0 deletions cmd/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/willjcj/param/pkg/param"
)

var force bool

var setCmd = &cobra.Command{
Use: "set name value",
Short: "Set a paramter in Parameter Store.",
Long: "Add a SecureString paramter to Parameter Store.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
param.Set(args[0], args[1], force)
},
}

func init() {
RootCmd.AddCommand(setCmd)
setCmd.Flags().BoolVarP(&force, "force", "f", false, "Overwrite the parameter if it exists.")

}
4 changes: 2 additions & 2 deletions docs/param.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Param is a cli tool to improve interacting with AWS Parameter Store.

### SEE ALSO

* [param add](param_add.md) - Add a paramter to Parameter Store.
* [param completion](param_completion.md) - Generates shell completion scripts
* [param copy](param_copy.md) - Copy a parameter to clipboard.
* [param list](param_list.md) - List parameters in Parameter Store.
* [param set](param_set.md) - Set a paramter in Parameter Store.
* [param show](param_show.md) - Show a decrypted parameter in the console.

###### Auto generated by spf13/cobra on 14-Jul-2018
###### Auto generated by spf13/cobra on 15-Jul-2018
2 changes: 1 addition & 1 deletion docs/param_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ param completion (bash|zsh) [flags]

* [param](param.md) - Tools to improve Parameter Store on the command line.

###### Auto generated by spf13/cobra on 14-Jul-2018
###### Auto generated by spf13/cobra on 15-Jul-2018
4 changes: 2 additions & 2 deletions docs/param_copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Copy a parameter to clipboard.
Copy the specified SSM Parameter from Paramter Store to your clipboard.

```
param copy parameter_name [flags]
param copy name [flags]
```

### Options
Expand All @@ -27,4 +27,4 @@ param copy parameter_name [flags]

* [param](param.md) - Tools to improve Parameter Store on the command line.

###### Auto generated by spf13/cobra on 14-Jul-2018
###### Auto generated by spf13/cobra on 15-Jul-2018
2 changes: 1 addition & 1 deletion docs/param_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ param list [flags]

* [param](param.md) - Tools to improve Parameter Store on the command line.

###### Auto generated by spf13/cobra on 14-Jul-2018
###### Auto generated by spf13/cobra on 15-Jul-2018
30 changes: 30 additions & 0 deletions docs/param_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## param set

Set a paramter in Parameter Store.

### Synopsis

Add a SecureString paramter to Parameter Store.

```
param set name value [flags]
```

### Options

```
-f, --force Overwrite the parameter if it exists.
-h, --help help for set
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.param.yaml)
```

### SEE ALSO

* [param](param.md) - Tools to improve Parameter Store on the command line.

###### Auto generated by spf13/cobra on 15-Jul-2018
2 changes: 1 addition & 1 deletion docs/param_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ param show parameter_name [flags]

* [param](param.md) - Tools to improve Parameter Store on the command line.

###### Auto generated by spf13/cobra on 14-Jul-2018
###### Auto generated by spf13/cobra on 15-Jul-2018
10 changes: 9 additions & 1 deletion pkg/param/list.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package param

import (
"fmt"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
)

func DescribeParameters(prefixes []string) []string {
func List(prefixes []string) {
for _, param := range describeParameters(prefixes) {
fmt.Println(param)
}
}

func describeParameters(prefixes []string) []string {
paramNames := []string{}
if len(prefixes) <= 0 {
paramNames = getAllParamNames()
Expand All @@ -32,6 +39,7 @@ func getParamNames(prefix string) []string {
Key: aws.String("Name"),
Values: []*string{aws.String(prefix)},
}}

paramNames := []string{}
err := service.DescribeParametersPages(&ssm.DescribeParametersInput{
Filters: filters},
Expand Down
23 changes: 23 additions & 0 deletions pkg/param/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package param

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
)

func Set(name string, value string, force bool) {
putSecureStringParameter(name, value, force)
}

func putSecureStringParameter(name string, value string, overwrite bool) {
_, err := service.PutParameter(&ssm.PutParameterInput{
Name: aws.String(name),
Type: aws.String("SecureString"),
Value: aws.String(value),
Overwrite: aws.Bool(overwrite),
})

if err != nil {
exitErrorf("Unable to set parameter, %v", err)
}
}

0 comments on commit 701b60f

Please sign in to comment.