Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Enhancements #3897

Merged
merged 36 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c14210e
Use Colored UI if stdout is a tty
calvn Feb 1, 2018
58be122
Add format options to operator unseal
calvn Feb 1, 2018
a1fcb87
Add format test on operator unseal
calvn Feb 1, 2018
e149184
Add -no-color output flag, and use BasicUi if no-color flag is provided
calvn Feb 1, 2018
45627d6
Move seal status formatting logic to OutputSealStatus
calvn Feb 1, 2018
ae12135
Apply no-color to warnings from DeprecatedCommands as well
calvn Feb 2, 2018
fa462a4
Add OutputWithFormat to support arbitrary data, add format option to …
calvn Feb 6, 2018
14e70dd
Add ability to output arbitrary list data on TableFormatter
calvn Feb 6, 2018
caae214
Clear up switch logic on format
calvn Feb 6, 2018
e0f96d6
Add format option for list-related commands
calvn Feb 6, 2018
c8cfcb5
Add format option to rest of commands that returns a client API response
calvn Feb 7, 2018
d2ba2fe
Remove initOutputYAML and initOutputJSON, and use OutputWithFormat in…
calvn Feb 7, 2018
8f2a229
Remove outputAsYAML and outputAsJSON, and use OutputWithFormat instead
calvn Feb 7, 2018
204180b
Remove -no-color flag, use env var exclusively to toggle colored output
calvn Feb 7, 2018
2cd9e8e
Fix compile
calvn Feb 7, 2018
c9573ea
Remove -no-color flag in main.go
calvn Feb 7, 2018
7ecec64
Add missing FlagSetOutputFormat
calvn Feb 7, 2018
189f8c9
Fix generate-root/decode test
calvn Feb 8, 2018
60184e9
Merge branch 'master-oss' into cli-enhancements
calvn Feb 8, 2018
a7225da
Migrate init functions to main.go
jefferai Feb 8, 2018
2b3f18d
Add no-color flag back as hidden
jefferai Feb 8, 2018
63d4957
Handle non-supported data types for TableFormatter.OutputList
calvn Feb 9, 2018
53d4534
Merge branch 'cli-enhancements' of github.com:hashicorp/vault into cl…
calvn Feb 9, 2018
4ff62dd
Pull formatting much further up to remove the need to use c.flagForma…
jefferai Feb 9, 2018
3ff53bc
Minor updates
jefferai Feb 9, 2018
a81c42e
Remove unnecessary check
jefferai Feb 9, 2018
79992f3
Fix SSH output and some tests
jefferai Feb 9, 2018
07cf36e
Fix tests
jefferai Feb 9, 2018
7982def
Make race detector not run on generate root since it kills Travis the…
jefferai Feb 9, 2018
9ca29d3
Update docs
calvn Feb 9, 2018
df07a3d
Merge branch 'cli-enhancements' of github.com:hashicorp/vault into cl…
calvn Feb 9, 2018
1583e23
Update docs
calvn Feb 10, 2018
51a07e2
Merge branch 'master-oss' into cli-enhancements
jefferai Feb 11, 2018
9b62480
Address review feedback
jefferai Feb 11, 2018
d0e35e1
Handle --format as well as -format
jefferai Feb 12, 2018
e4ae7ba
Merge branch 'master' into cli-enhancements
calvn Feb 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions api/sys_generate_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func (c *Sys) generateRootUpdateCommon(path, shard, nonce string) (*GenerateRoot
}

type GenerateRootStatusResponse struct {
Nonce string
Started bool
Progress int
Required int
Complete bool
Nonce string `json:"nonce"`
Started bool `json:"started"`
Progress int `json:"progress"`
Required int `json:"required"`
Complete bool `json:"complete"`
EncodedToken string `json:"encoded_token"`
EncodedRootToken string `json:"encoded_root_token"`
PGPFingerprint string `json:"pgp_fingerprint"`
Expand Down
26 changes: 13 additions & 13 deletions api/sys_rekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,27 @@ type RekeyInitRequest struct {
}

type RekeyStatusResponse struct {
Nonce string
Started bool
T int
N int
Progress int
Required int
Nonce string `json:"nonce"`
Started bool `json:"started"`
T int `json:"t"`
N int `json:"n"`
Progress int `json:"progress"`
Required int `json:"required"`
PGPFingerprints []string `json:"pgp_fingerprints"`
Backup bool
Backup bool `json:"backup"`
}

type RekeyUpdateResponse struct {
Nonce string
Complete bool
Keys []string
Nonce string `json:"nonce"`
Complete bool `json:"complete"`
Keys []string `json:"keys"`
KeysB64 []string `json:"keys_base64"`
PGPFingerprints []string `json:"pgp_fingerprints"`
Backup bool
Backup bool `json:"backup"`
}

type RekeyRetrieveResponse struct {
Nonce string
Keys map[string][]string
Nonce string `json:"nonce"`
Keys map[string][]string `json:"keys"`
KeysB64 map[string][]string `json:"keys_base64"`
}
4 changes: 4 additions & 0 deletions command/audit_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (c *AuditDisableCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
4 changes: 4 additions & 0 deletions command/audit_enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (c *AuditEnableCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
if len(args) < 1 {
c.UI.Error("Missing TYPE!")
Expand Down
20 changes: 13 additions & 7 deletions command/audit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Usage: vault audit list [options]
}

func (c *AuditListCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP)
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)

f := set.NewFlagSet("Command Options")

Expand Down Expand Up @@ -76,6 +76,10 @@ func (c *AuditListCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
Expand All @@ -99,13 +103,15 @@ func (c *AuditListCommand) Run(args []string) int {
return 0
}

if c.flagDetailed {
c.UI.Output(tableOutput(c.detailedAudits(audits), nil))
return 0
switch c.flagFormat {
case "table":
if c.flagDetailed {
return OutputWithFormat(c.UI, c.flagFormat, c.detailedAudits(audits))
}
return OutputWithFormat(c.UI, c.flagFormat, c.simpleAudits(audits))
default:
return OutputWithFormat(c.UI, c.flagFormat, audits)
}

c.UI.Output(tableOutput(c.simpleAudits(audits), nil))
return 0
}

func (c *AuditListCommand) simpleAudits(audits map[string]*api.Audit) []string {
Expand Down
4 changes: 4 additions & 0 deletions command/auth_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (c *AuthDisableCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
4 changes: 4 additions & 0 deletions command/auth_enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (c *AuthEnableCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
4 changes: 4 additions & 0 deletions command/auth_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *AuthHelpCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
22 changes: 14 additions & 8 deletions command/auth_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Usage: vault auth list [options]
}

func (c *AuthListCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP)
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)

f := set.NewFlagSet("Command Options")

Expand All @@ -55,7 +55,7 @@ func (c *AuthListCommand) Flags() *FlagSets {
Target: &c.flagDetailed,
Default: false,
Usage: "Print detailed information such as configuration and replication " +
"status about each auth method.",
"status about each auth method. This option is only applicable to table formatted output.",
})

return set
Expand All @@ -77,6 +77,10 @@ func (c *AuthListCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
Expand All @@ -95,13 +99,15 @@ func (c *AuthListCommand) Run(args []string) int {
return 2
}

if c.flagDetailed {
c.UI.Output(tableOutput(c.detailedMounts(auths), nil))
return 0
switch c.flagFormat {
case "table":
if c.flagDetailed {
return OutputWithFormat(c.UI, c.flagFormat, c.detailedMounts(auths))
}
return OutputWithFormat(c.UI, c.flagFormat, c.simpleMounts(auths))
default:
return OutputWithFormat(c.UI, c.flagFormat, auths)
}

c.UI.Output(tableOutput(c.simpleMounts(auths), nil))
return 0
}

func (c *AuthListCommand) simpleMounts(auths map[string]*api.AuthMount) []string {
Expand Down
4 changes: 4 additions & 0 deletions command/auth_tune.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (c *AuthTuneCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
28 changes: 25 additions & 3 deletions command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type BaseCommand struct {
flagTLSSkipVerify bool
flagWrapTTL time.Duration

flagFormat string
flagField string
flagFormat string
flagField string
flagNoColor bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this and the var below if we are just using the envvar

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to allow it in a flag but not advertise it. Now that you mention autocorrect below, I wonder if it's better to just remove it rather than have people get it autocorrected and then complain that it's not documented. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code will be a lot simpler if we drop the flag entirely. People are more likely to "never want colored output" that "not want colored output for this specific command", and even in the latter case, they can specify the envvar first.


tokenHelper token.TokenHelper

Expand Down Expand Up @@ -143,6 +144,7 @@ const (
FlagSetHTTP
FlagSetOutputField
FlagSetOutputFormat
FlagSetOutputNoColor
)

// flagSet creates the flags for this command. The result is cached on the
Expand All @@ -151,6 +153,11 @@ func (c *BaseCommand) flagSet(bit FlagSetBit) *FlagSets {
c.flagsOnce.Do(func() {
set := NewFlagSets(c.UI)

// These flag sets will apply to all leaf subcommands.
// TODO: Optional, but FlagSetHTTP can be safely removed from the individual
// Flags() subcommands.
bit = bit | FlagSetHTTP | FlagSetOutputNoColor

if bit&FlagSetHTTP != 0 {
f := set.NewFlagSet("HTTP Options")

Expand Down Expand Up @@ -238,7 +245,7 @@ func (c *BaseCommand) flagSet(bit FlagSetBit) *FlagSets {
})
}

if bit&(FlagSetOutputField|FlagSetOutputFormat) != 0 {
if bit&(FlagSetOutputField|FlagSetOutputFormat|FlagSetOutputNoColor) != 0 {
f := set.NewFlagSet("Output Options")

if bit&FlagSetOutputField != 0 {
Expand All @@ -265,6 +272,16 @@ func (c *BaseCommand) flagSet(bit FlagSetBit) *FlagSets {
"are \"table\", \"json\", or \"yaml\".",
})
}

if bit&FlagSetOutputNoColor != 0 {
f.BoolVar(&BoolVar{
Name: "no-color",
Target: &c.flagNoColor,
Default: false,
EnvVar: "VAULT_OUTPUT_NO_COLOR",
Usage: "Print the output without ANSI color escape sequences.",
})
}
}

c.flags = set
Expand Down Expand Up @@ -316,6 +333,11 @@ func (f *FlagSets) Parse(args []string) error {
return f.mainSet.Parse(args)
}

// Parsed reports whether the command-line flags have been parsed.
func (f *FlagSets) Parsed() bool {
return f.mainSet.Parsed()
}

// Args returns the remaining args after parsing.
func (f *FlagSets) Args() []string {
return f.mainSet.Args()
Expand Down
45 changes: 32 additions & 13 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/vault/physical"
"github.com/hashicorp/vault/version"
"github.com/mitchellh/cli"
"golang.org/x/crypto/ssh/terminal"

"github.com/hashicorp/vault/builtin/logical/aws"
"github.com/hashicorp/vault/builtin/logical/cassandra"
Expand Down Expand Up @@ -88,6 +89,13 @@ func (c *DeprecatedCommand) Run(args []string) int {
}

func (c *DeprecatedCommand) warn() {
// If we detect the env var for no-color, then get the BasicUi.
// Ideally we would want DeprecatedCommands to support flags as well,
// and do parsing here, but this will do for now.
if os.Getenv("VAULT_OUTPUT_NO_COLOR") != "" {
c.UI = getBasicUI(c.UI)
}

c.UI.Warn(wrapAtLength(fmt.Sprintf(
"WARNING! The \"vault %s\" command is deprecated. Please use \"vault %s\" "+
"instead. This command will be removed in Vault 0.11 (or later).",
Expand All @@ -101,21 +109,32 @@ var Commands map[string]cli.CommandFactory
var DeprecatedCommands map[string]cli.CommandFactory

func init() {
ui := &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: &cli.BasicUi{
Writer: os.Stdout,
ErrorWriter: os.Stderr,
},
var ui cli.Ui
var serverCmdUi cli.Ui

ui = &cli.BasicUi{
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}

serverCmdUi := &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: &cli.BasicUi{
Writer: os.Stdout,
},
serverCmdUi = &cli.BasicUi{
Writer: os.Stdout,
}

// Attempt to detect if stdout is a tty. If so,
// wrap BasicUi's in ColoredUi.
if terminal.IsTerminal(int(os.Stdout.Fd())) {
ui = &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: ui,
}

serverCmdUi = &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: serverCmdUi,
}
}

loginHandlers := map[string]LoginHandler{
Expand Down
4 changes: 4 additions & 0 deletions command/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (c *DeleteCommand) Run(args []string) int {
return 1
}

if c.flagNoColor {
c.UI = getBasicUI(c.UI)
}

args = f.Args()
switch {
case len(args) < 1:
Expand Down
Loading