Skip to content

Commit

Permalink
Add Show Command
Browse files Browse the repository at this point in the history
Add show command to print out decrypted parameters.
Update README.md for show command and bump to 1.4.0.
Refactor packaging to use one 'param' package.
  • Loading branch information
WillJCJ committed Jul 14, 2018
1 parent 1794bd2 commit 018d522
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 107 deletions.
55 changes: 40 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@

## Introduction

A cli tool for talking to AWS Parameter Store. Copy parameters directly to your clipboard.
`param` is a cli tool for talking to AWS Parameter Store. Copy parameters
directly to your clipboard.

`param` uses [Cobra](https://github.com/spf13/cobra), a library providing a simple
interface to create powerful CLI interfaces similar to git & go tools.
`param` uses [Cobra](https://github.com/spf13/cobra), a library providing a
simple interface to create powerful CLI interfaces similar to git & go tools.

`param` works with Linux and MacOS. It also supports `bash` and `zsh` completion.
`param` works with Linux and MacOS.
It also supports `bash` and `zsh` completion.

## Install

You can install `param` by downloading the latest binary from the [Releases](https://github.com/WillJCJ/param/releases)
page or by compiling from source with `go`.
You can install `param` by downloading the latest binary from the
[Releases](https://github.com/WillJCJ/param/releases) page or by compiling
from source with `go`.

```console
go get github.com/willjcj/param
Expand All @@ -34,20 +37,21 @@ already on your `PATH`.

### Curl

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

#### Linux

```console
curl -LO https://github.com/WillJCJ/param/releases/download/1.3.0/param-linux-amd64 && \
curl -LO https://github.com/WillJCJ/param/releases/download/1.4.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.3.0/param-darwin-amd64 && \
curl -LO https://github.com/WillJCJ/param/releases/download/1.4.0/param-darwin-amd64 && \
chmod +x param-darwin-amd64 && \
sudo mv param-darwin-amd64 /usr/local/bin/param
```
Expand All @@ -71,6 +75,11 @@ $ param copy parameter_name -v
password123
```

#### Auto-completion

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

### List

Get a sorted list of parameters in SSM with optional prefix(es):
Expand All @@ -89,9 +98,25 @@ prefix1.prod.password
prefix2.key
```

### Show

If you'd like to print out the decrypted parameter without copying it the
clipboard, you can use:

```console
$ param show parameter_name
password123
```

#### Auto-completion

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

## Shell Completion

`param completion (bash|zsh)` outputs shell completion code for the specified shell (`bash` or `zsh`).
`param completion (bash|zsh)` outputs shell completion code for the specified
shell (`bash` or `zsh`).
The shell code must be evaluated to provide interactive completion of
param commands.
This can be done by sourcing it from `~/.bashrc` or `~/.zshrc`
Expand Down Expand Up @@ -149,16 +174,16 @@ a new tag is pushed to GitHub.
- New Commands
- Set command - Command to set parameters
- Delete command - Command to delete parameters
- Show command - Like `copy -v` without copying to clipboard
- Improvements
- Add a flag to specify parameter type. Currently only works with `SecureString`s.
- Add a flag to specify parameter type.
Currently only works with `SecureString`s.
- Shell Completion
- `zsh` completion doesn't seem to work.
- Update or delete the bash completion cache after creating/deleting parameters
- Update or delete the bash completion cache after creating/deleting
parameters
- Add bash completion for other subcommands and flags.
- Write Tests
- Improve Documentation
- More Examples
- Write about how to set up AWS variables/profile.
- Refactor
- Common SSM service instead of duplicated in all commands.
(And what capabilities are required)
17 changes: 2 additions & 15 deletions cmd/copy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cmd

import (
"fmt"

"github.com/atotto/clipboard"
"github.com/spf13/cobra"
"github.com/willjcj/param/pkg/paramget"
"github.com/willjcj/param/pkg/param"
)

var verbose bool
Expand All @@ -16,21 +13,11 @@ var copyCmd = &cobra.Command{
Long: "Copy the specified SSM Parameter from Paramter Store to your clipboard.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
copyToClipboard(args[0])
param.Copy(args[0], verbose)
},
}

func init() {
RootCmd.AddCommand(copyCmd)
copyCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Also print parameter value to the stdout.")
}

func copyToClipboard(name string) {
value := paramget.GetDecryptedParameter(name)

clipboard.WriteAll(value)

if verbose {
fmt.Println(value)
}
}
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/willjcj/param/pkg/paramlist"
"github.com/willjcj/param/pkg/param"
)

var prefixes string
Expand All @@ -28,7 +28,7 @@ func init() {

func listParameters() {
prefixSlice := strings.Split(prefixes, ",")
for _, param := range paramlist.DescribeParameters(prefixSlice) {
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_copy | param_show)
__param_list
return
;;
Expand Down
20 changes: 20 additions & 0 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

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

var showCmd = &cobra.Command{
Use: "show parameter_name",
Short: "Show a decrypted parameter in the console.",
Long: "Show the specified decrypted SSM Parameter from Paramter Store in your console.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
param.Show(args[0])
},
}

func init() {
RootCmd.AddCommand(showCmd)
}
1 change: 1 addition & 0 deletions docs/param.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Param is a cli tool to improve interacting with AWS 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 show](param_show.md) - Show a decrypted parameter in the console.

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

Show a decrypted parameter in the console.

### Synopsis

Show the specified decrypted SSM Parameter from Paramter Store in your console.

```
param show parameter_name [flags]
```

### Options

```
-h, --help help for show
```

### 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 14-Jul-2018
17 changes: 17 additions & 0 deletions pkg/param/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package param

import (
"fmt"

"github.com/atotto/clipboard"
)

func Copy(name string, verbose bool) {
value := getDecryptedParameter(name)

clipboard.WriteAll(value)

if verbose {
fmt.Println(value)
}
}
17 changes: 17 additions & 0 deletions pkg/param/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package param

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

func getDecryptedParameter(name string) string {
output, err := service.GetParameter(&ssm.GetParameterInput{
Name: aws.String(name),
WithDecryption: aws.Bool(true),
})
if err != nil {
exitErrorf("Unable to describe parameters, %v", err)
}
return *output.Parameter.Value
}
34 changes: 1 addition & 33 deletions pkg/paramlist/paramlist.go → pkg/param/list.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package paramlist
package param

import (
"fmt"
"os"
"sort"

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

var service = createSSMService()

func createSSMService() *ssm.SSM {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-1")},
)

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

// Create SSM service client
return ssm.New(sess)
}

func DescribeParameters(prefixes []string) []string {
paramNames := []string{}
if len(prefixes) <= 0 {
Expand Down Expand Up @@ -70,17 +52,3 @@ func getParamNames(prefix string) []string {
func getAllParamNames() []string {
return getParamNames(" ")
}

func appendIfMissing(slice []string, s string) []string {
for _, ele := range slice {
if ele == s {
return slice
}
}
return append(slice, s)
}

func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
11 changes: 11 additions & 0 deletions pkg/param/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package param

import (
"fmt"
)

func Show(name string) {
value := getDecryptedParameter(name)

fmt.Println(value)
}
22 changes: 22 additions & 0 deletions pkg/param/ssm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package param

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

var service = createSSMService()

func createSSMService() *ssm.SSM {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-1")},
)

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

// Create SSM service client
return ssm.New(sess)
}
20 changes: 20 additions & 0 deletions pkg/param/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package param

import (
"fmt"
"os"
)

func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

func appendIfMissing(slice []string, s string) []string {
for _, ele := range slice {
if ele == s {
return slice
}
}
return append(slice, s)
}
Loading

0 comments on commit 018d522

Please sign in to comment.