mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 10:12:35 +00:00
command: refactor to share output formating code
This commit is contained in:
59
command/format.go
Normal file
59
command/format.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/vault/api"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
|
"github.com/ryanuber/columnize"
|
||||||
|
)
|
||||||
|
|
||||||
|
func OutputSecret(ui cli.Ui, format string, secret *api.Secret) int {
|
||||||
|
switch format {
|
||||||
|
case "json":
|
||||||
|
return outputFormatJSON(ui, secret)
|
||||||
|
case "table":
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
return outputFormatTable(ui, secret, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func outputFormatJSON(ui cli.Ui, s *api.Secret) int {
|
||||||
|
b, err := json.Marshal(s)
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf(
|
||||||
|
"Error formatting secret: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
json.Indent(&out, b, "", "\t")
|
||||||
|
ui.Output(out.String())
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func outputFormatTable(ui cli.Ui, s *api.Secret, whitespace bool) int {
|
||||||
|
config := columnize.DefaultConfig()
|
||||||
|
config.Delim = "♨"
|
||||||
|
config.Glue = "\t"
|
||||||
|
config.Prefix = ""
|
||||||
|
|
||||||
|
input := make([]string, 0, 5)
|
||||||
|
input = append(input, fmt.Sprintf("Key %s Value", config.Delim))
|
||||||
|
|
||||||
|
if s.LeaseID != "" && s.LeaseDuration > 0 {
|
||||||
|
input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID))
|
||||||
|
input = append(input, fmt.Sprintf(
|
||||||
|
"lease_duration %s %d", config.Delim, s.LeaseDuration))
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range s.Data {
|
||||||
|
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Output(columnize.Format(input, config))
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -1,13 +1,8 @@
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/api"
|
|
||||||
"github.com/ryanuber/columnize"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReadCommand is a Command that reads data from the Vault.
|
// ReadCommand is a Command that reads data from the Vault.
|
||||||
@@ -51,55 +46,7 @@ func (c *ReadCommand) Run(args []string) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.output(format, secret)
|
return OutputSecret(c.Ui, format, secret)
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ReadCommand) output(format string, secret *api.Secret) int {
|
|
||||||
switch format {
|
|
||||||
case "json":
|
|
||||||
return c.formatJSON(secret)
|
|
||||||
case "table":
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
return c.formatTable(secret, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ReadCommand) formatJSON(s *api.Secret) int {
|
|
||||||
b, err := json.Marshal(s)
|
|
||||||
if err != nil {
|
|
||||||
c.Ui.Error(fmt.Sprintf(
|
|
||||||
"Error formatting secret: %s", err))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
|
||||||
json.Indent(&out, b, "", "\t")
|
|
||||||
c.Ui.Output(out.String())
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ReadCommand) formatTable(s *api.Secret, whitespace bool) int {
|
|
||||||
config := columnize.DefaultConfig()
|
|
||||||
config.Delim = "♨"
|
|
||||||
config.Glue = "\t"
|
|
||||||
config.Prefix = ""
|
|
||||||
|
|
||||||
input := make([]string, 0, 5)
|
|
||||||
input = append(input, fmt.Sprintf("Key %s Value", config.Delim))
|
|
||||||
|
|
||||||
if s.LeaseID != "" && s.LeaseDuration > 0 {
|
|
||||||
input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID))
|
|
||||||
input = append(input, fmt.Sprintf(
|
|
||||||
"lease_duration %s %d", config.Delim, s.LeaseDuration))
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range s.Data {
|
|
||||||
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Ui.Output(columnize.Format(input, config))
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ReadCommand) Synopsis() string {
|
func (c *ReadCommand) Synopsis() string {
|
||||||
|
|||||||
@@ -55,10 +55,7 @@ func (c *RenewCommand) Run(args []string) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the ReadCommand in order to format our output
|
return OutputSecret(c.Ui, format, secret)
|
||||||
var read ReadCommand
|
|
||||||
read.Meta = c.Meta
|
|
||||||
return read.output(format, secret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RenewCommand) Synopsis() string {
|
func (c *RenewCommand) Synopsis() string {
|
||||||
|
|||||||
@@ -54,10 +54,7 @@ func (c *TokenRenewCommand) Run(args []string) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the ReadCommand in order to format our output
|
return OutputSecret(c.Ui, format, secret)
|
||||||
var read ReadCommand
|
|
||||||
read.Meta = c.Meta
|
|
||||||
return read.output(format, secret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TokenRenewCommand) Synopsis() string {
|
func (c *TokenRenewCommand) Synopsis() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user