command/token-create: provide more useful output. Fixes #337

This commit is contained in:
Armon Dadgar 2015-06-17 16:59:50 -07:00
parent ffeb6ea76c
commit 1f963ec1bb
3 changed files with 24 additions and 2 deletions

View File

@ -53,6 +53,16 @@ func outputFormatTable(ui cli.Ui, s *api.Secret, whitespace bool) int {
"lease_renewable %s %s", config.Delim, strconv.FormatBool(s.Renewable)))
}
if s.Auth != nil {
input = append(input, fmt.Sprintf("token %s %s", config.Delim, s.Auth.ClientToken))
input = append(input, fmt.Sprintf("token_duration %s %d", config.Delim, s.Auth.LeaseDuration))
input = append(input, fmt.Sprintf("token_renewable %s %v", config.Delim, s.Auth.Renewable))
input = append(input, fmt.Sprintf("token_policies %s %v", config.Delim, s.Auth.Policies))
for k, v := range s.Auth.Metadata {
input = append(input, fmt.Sprintf("token_meta_%s %s %#v", k, config.Delim, v))
}
}
for k, v := range s.Data {
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v))
}

View File

@ -15,12 +15,14 @@ type TokenCreateCommand struct {
}
func (c *TokenCreateCommand) Run(args []string) int {
var format string
var displayName, lease string
var orphan bool
var metadata map[string]string
var numUses int
var policies []string
flags := c.Meta.FlagSet("mount", FlagSetDefault)
flags.StringVar(&format, "format", "table", "")
flags.StringVar(&displayName, "display-name", "", "")
flags.StringVar(&lease, "lease", "", "")
flags.BoolVar(&orphan, "orphan", false, "")
@ -61,8 +63,7 @@ func (c *TokenCreateCommand) Run(args []string) int {
return 2
}
c.Ui.Output(secret.Auth.ClientToken)
return 0
return OutputSecret(c.Ui, format, secret)
}
func (c *TokenCreateCommand) Synopsis() string {
@ -121,6 +122,10 @@ Token Options:
-use-limit=5 The number of times this token can be used until
it is automatically revoked.
-format=table The format for output. By default it is a whitespace-
delimited table. This can also be json.
`
return strings.TrimSpace(helpText)
}

View File

@ -1,6 +1,7 @@
package command
import (
"strings"
"testing"
"github.com/hashicorp/vault/http"
@ -27,4 +28,10 @@ func TestTokenCreate(t *testing.T) {
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
// Ensure we get lease info
output := ui.OutputWriter.String()
if !strings.Contains(output, "token_duration") {
t.Fatalf("bad: %#v", output)
}
}