Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Feb 11, 2018
1 parent 51a07e2 commit 9b62480
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion command/audit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *AuditListCommand) Run(args []string) int {
return 0
}

switch Format() {
switch Format(c.UI) {
case "table":
if c.flagDetailed {
c.UI.Output(tableOutput(c.detailedAudits(audits), nil))
Expand Down
2 changes: 1 addition & 1 deletion command/auth_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *AuthListCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
if c.flagDetailed {
c.UI.Output(tableOutput(c.detailedMounts(auths), nil))
Expand Down
14 changes: 2 additions & 12 deletions command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ type BaseCommand struct {
flagTLSSkipVerify bool
flagWrapTTL time.Duration

flagFormat string
flagField string
flagNoColor bool
flagFormat string
flagField string

tokenHelper token.TokenHelper

Expand Down Expand Up @@ -243,15 +242,6 @@ func (c *BaseCommand) flagSet(bit FlagSetBit) *FlagSets {
"The TTL is specified as a numeric string with suffix like \"30s\" " +
"or \"5m\".",
})

f.BoolVar(&BoolVar{
Name: "no-color",
Target: &c.flagNoColor,
Default: false,
Hidden: true,
EnvVar: EnvVaultCLINoColor,
Usage: "Print the output without ANSI color escape sequences.",
})
}

if bit&(FlagSetOutputField|FlagSetOutputFormat) != 0 {
Expand Down
4 changes: 2 additions & 2 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (c *DeprecatedCommand) Help() string {

// Run wraps the embedded Run command and prints a warning about deprecation.
func (c *DeprecatedCommand) Run(args []string) int {
if Format() == "table" {
if Format(c.UI) == "table" {
c.warn()
}
return c.Command.Run(args)
Expand Down Expand Up @@ -564,7 +564,7 @@ func initCommands(ui, serverCmdUi cli.Ui) {

// Deprecated commands
//
// TODO: Remove in 0.9.0
// TODO: Remove not before 0.11.0
DeprecatedCommands = map[string]cli.CommandFactory{
"audit-disable": func() (cli.Command, error) {
return &DeprecatedCommand{
Expand Down
17 changes: 13 additions & 4 deletions command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func OutputData(ui cli.Ui, data interface{}) int {
}

func outputWithFormat(ui cli.Ui, secret *api.Secret, data interface{}) int {
formatter, ok := Formatters[Format()]
format := Format(ui)
formatter, ok := Formatters[format]
if !ok {
ui.Error(fmt.Sprintf("Invalid output format: %s", Format()))
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
return 1
}

Expand All @@ -67,14 +68,22 @@ var Formatters = map[string]Formatter{
"yml": YamlFormatter{},
}

func Format() string {
func format() string {
format := os.Getenv(EnvVaultFormat)
if format == "" {
format = "table"
}
return format
}

func Format(ui cli.Ui) string {
switch ui.(type) {
case *VaultUI:
return ui.(*VaultUI).format
}
return format()
}

// An output formatter for json output of an object
type JsonFormatter struct{}

Expand Down Expand Up @@ -240,7 +249,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error {

// OutputSealStatus will print *api.SealStatusResponse in the CLI according to the format provided
func OutputSealStatus(ui cli.Ui, client *api.Client, status *api.SealStatusResponse) int {
switch Format() {
switch Format(ui) {
case "table":
default:
return OutputData(ui, status)
Expand Down
2 changes: 1 addition & 1 deletion command/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (c *LoginCommand) Run(args []string) int {
}

// Print some yay! text, but only in table mode.
if Format() == "table" {
if Format(c.UI) == "table" {
c.UI.Output(wrapAtLength(
"Success! You are now authenticated. The token information displayed "+
"below is already stored in the token helper. You do NOT need to run "+
Expand Down
14 changes: 7 additions & 7 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type VaultUI struct {
cli.Ui
isTerminal bool
format string
}

func (u *VaultUI) Output(m string) {
Expand Down Expand Up @@ -48,10 +49,6 @@ func setupEnv(args []string) []string {
break
}

if arg == "-no-color" {
os.Setenv(EnvVaultCLINoColor, "true")
}

// Parse a given flag here, which overrides the env var
if strings.HasPrefix(arg, "-format=") {
format = strings.TrimPrefix(arg, "-format=")
Expand All @@ -62,9 +59,10 @@ func setupEnv(args []string) []string {
}
}

envVaultFormat := os.Getenv(EnvVaultFormat)
// If we did not parse a value, fetch the env var
if format == "" && os.Getenv(EnvVaultFormat) != "" {
format = os.Getenv(EnvVaultFormat)
if format == "" && envVaultFormat != "" {
format = envVaultFormat
}
// Lowercase for consistency
format = strings.ToLower(format)
Expand All @@ -86,7 +84,7 @@ func Run(args []string) int {
color = false
}

format := Format()
format := format()

isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))

Expand All @@ -96,12 +94,14 @@ func Run(args []string) int {
ErrorWriter: os.Stderr,
},
isTerminal: isTerminal,
format: format,
}
serverCmdUi := &VaultUI{
Ui: &cli.BasicUi{
Writer: os.Stdout,
},
isTerminal: isTerminal,
format: format,
}

if _, ok := Formatters[format]; !ok {
Expand Down
6 changes: 3 additions & 3 deletions command/operator_generate_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (c *OperatorGenerateRootCommand) init(client *api.Client, otp, pgpKey strin
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
return c.printStatus(status)
default:
Expand Down Expand Up @@ -434,7 +434,7 @@ func (c *OperatorGenerateRootCommand) provide(client *api.Client, key string, dr
c.UI.Error(fmt.Sprintf("Error posting unseal key: %s", err))
return 2
}
switch Format() {
switch Format(c.UI) {
case "table":
return c.printStatus(status)
default:
Expand Down Expand Up @@ -467,7 +467,7 @@ func (c *OperatorGenerateRootCommand) status(client *api.Client, drToken bool) i
c.UI.Error(fmt.Sprintf("Error getting root generation status: %s", err))
return 2
}
switch Format() {
switch Format(c.UI) {
case "table":
return c.printStatus(status)
default:
Expand Down
2 changes: 1 addition & 1 deletion command/operator_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (c *OperatorInitCommand) init(client *api.Client, req *api.InitRequest) int
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
default:
return OutputData(c.UI, newMachineInit(req, resp))
Expand Down
2 changes: 1 addition & 1 deletion command/operator_key_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *OperatorKeyStatusCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
c.UI.Output(printKeyStatus(status))
return 0
Expand Down
4 changes: 2 additions & 2 deletions command/operator_rekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (c *OperatorRekeyCommand) printStatus(status *api.RekeyStatusResponse) int
out = append(out, fmt.Sprintf("Backup | %t", status.Backup))
}

switch Format() {
switch Format(c.UI) {
case "table":
c.UI.Output(tableOutput(out, nil))
return 0
Expand All @@ -589,7 +589,7 @@ func (c *OperatorRekeyCommand) printStatus(status *api.RekeyStatusResponse) int
}

func (c *OperatorRekeyCommand) printUnsealKeys(status *api.RekeyStatusResponse, resp *api.RekeyUpdateResponse) int {
switch Format() {
switch Format(c.UI) {
case "table":
default:
return OutputData(c.UI, resp)
Expand Down
2 changes: 1 addition & 1 deletion command/policy_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *PolicyListCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
for _, p := range policies {
c.UI.Output(p)
Expand Down
2 changes: 1 addition & 1 deletion command/policy_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *PolicyReadCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
c.UI.Output(strings.TrimSpace(rules))
return 0
Expand Down
2 changes: 1 addition & 1 deletion command/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *OperatorRotateCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
c.UI.Output("Success! Rotated key")
c.UI.Output("")
Expand Down
2 changes: 1 addition & 1 deletion command/secrets_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *SecretsListCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
if c.flagDetailed {
c.UI.Output(tableOutput(c.detailedMounts(mounts), nil))
Expand Down
2 changes: 1 addition & 1 deletion command/token_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *TokenCapabilitiesCommand) Run(args []string) int {
return 2
}

switch Format() {
switch Format(c.UI) {
case "table":
sort.Strings(capabilities)
c.UI.Output(strings.Join(capabilities, ", "))
Expand Down
2 changes: 2 additions & 0 deletions command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func PrintRaw(ui cli.Ui, str string) int {
// type, this falls back to os.Stdout.
func getWriterFromUI(ui cli.Ui) io.Writer {
switch t := ui.(type) {
case *VaultUI:
return getWriterFromUI(t.Ui)
case *cli.BasicUi:
return t.Writer
case *cli.ColoredUi:
Expand Down
2 changes: 1 addition & 1 deletion command/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *WriteCommand) Run(args []string) int {
}
if secret == nil {
// Don't output anything unless using the "table" format
if Format() == "table" {
if Format(c.UI) == "table" {
c.UI.Info(fmt.Sprintf("Success! Data written to: %s", path))
}
return 0
Expand Down

0 comments on commit 9b62480

Please sign in to comment.