Skip to content

Commit

Permalink
always use prompt for secret
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-github committed May 3, 2020
1 parent 0d2177e commit bded947
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flags

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -32,15 +31,14 @@ type Flags struct {

var EXTRA = `
Commands:
save-secret <service> [secret [username]]
save-secret <service> [username]
service Service URL or name for secret.
secret Secret to be saved for the service.
username Username to associate with the service.
Save a secret into the secret store. Requires wtf.secretStore
to be configured. See individual modules for information on what
service, secret, and username means for their configuration. Not
all modules use secrets, and not all secrets require a username.
If secret or username is not provided, will prompt for them.
username Username to associate with the service. Optional.
Save a secret into the secret store. The secret will be prompted for.
Requires wtf.secretStore to be configured. See individual modules for
information on what service, secret, and username means for their
configuration. Not all modules use secrets, and not all secrets require
a username.
`

// NewFlags creates an instance of Flags
Expand Down Expand Up @@ -85,38 +83,29 @@ func (flags *Flags) RenderIf(version, date string, config *config.Config) {

service = args[0]

if len(args) > 2 {
fmt.Fprintf(os.Stderr, "save-secret: too many arguments, see `%s --help`\n", os.Args[0])
os.Exit(1)
}

if len(args) > 1 {
secret = args[1]
} else {
b, err := readline.Password("Secret (required): ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
secret = string(b)
username = args[1]
}

b, err := readline.Password("Secret: ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
secret = string(b)
secret = strings.TrimSpace(secret)

if secret == "" {
fmt.Fprintf(os.Stderr, "save-secret: secret required, see `%s --help`\n", os.Args[0])
os.Exit(1)
}

if len(args) > 2 {
username = args[2]
} else {
fmt.Printf("Username (optional): ")
reader := bufio.NewReader(os.Stdin)
var err error
username, err = reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
username = strings.TrimSpace(username)

err := cfg.StoreSecret(config, &cfg.Secret{
err = cfg.StoreSecret(config, &cfg.Secret{
Service: service,
Secret: secret,
Username: username,
Expand Down

0 comments on commit bded947

Please sign in to comment.