Skip to content

Commit

Permalink
Fix addkey error messages and improve usability
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesimons committed Jun 18, 2018
1 parent a391e0c commit b732a66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 7 additions & 5 deletions cmd/envelope/add_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/url"

errors "github.com/hashicorp/errwrap"
"github.com/ansel1/merry"
"github.com/mikesimons/envelope"
"gopkg.in/urfave/cli.v1"
)
Expand All @@ -13,8 +13,10 @@ import (
// e.g $ envelope add-key test kms://arn
func addKeyCommand() cli.Command {
return cli.Command{
Name: "add-key",
Usage: "Add a key to the keyring",
Name: "addkey",
Usage: "Add a key to the keyring.",
Description: `Master key DSN is of the form <provider>://<dsn>. The only supported provider is currently awskms.
Example: envelope add-key mytestkey awskms://arn:aws:kms:us-east-1:111111111111:key/abcdef01-2345-6789-abcd-ef0123456789`,
ArgsUsage: "<alias> <master key dsn>",
Flags: []cli.Flag{
cli.StringFlag{
Expand All @@ -24,7 +26,7 @@ func addKeyCommand() cli.Command {
},
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
cli.ShowCommandHelp(c, "add-key")
cli.ShowCommandHelp(c, "addkey")
fmt.Println("")
return cli.NewExitError("Error: Not enough arguments", 1)
}
Expand Down Expand Up @@ -61,7 +63,7 @@ func parseEncryptionContext(input string) (map[string]string, error) {

values, err := url.ParseQuery(input)
if err != nil {
return nil, errors.Wrapf("Unable to parse encryption context", err)
return nil, merry.Wrap(err).WithMessage("Unable to parse encryption context")
}

output := make(map[string]string)
Expand Down
3 changes: 2 additions & 1 deletion cmd/envelope/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/ansel1/merry"
cli "gopkg.in/urfave/cli.v1"
)

func asFormat(as string, input string) string {
Expand Down Expand Up @@ -66,7 +67,7 @@ func processErrors(err error) error {
}
lines = append(lines, strings.Join(extra, ", "))

return fmt.Errorf(strings.Join(lines, "\n"))
return cli.NewExitError(strings.Join(lines, "\n"), 1)
}

func getInputReader(input string) (io.Reader, error) {
Expand Down
7 changes: 6 additions & 1 deletion keysvc/keysvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keysvc
import (
"fmt"
"net/url"
"strings"

"github.com/ansel1/merry"
"github.com/mikesimons/envelope/keysvc/awskms"
Expand All @@ -26,7 +27,11 @@ func GetKeyService(name string) (KeyServiceProvider, error) {
return fn()
}

return nil, fmt.Errorf("Unknown key service: %s", name)
var known []string
for k := range services {
known = append(known, k)
}
return nil, merry.Errorf("Unknown key service. Must be one of: %s", strings.Join(known, ", ")).WithValue("svc", name)
}

func GenerateDatakey(alias string, masterKey string, context map[string]string) (*Key, error) {
Expand Down

0 comments on commit b732a66

Please sign in to comment.