Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #36 from adfinis-sygroup/feature/support_comment_i…
Browse files Browse the repository at this point in the history
…n_show_output

Prefix comments in the output `vc show` by #
  • Loading branch information
winpat authored Jun 29, 2017
2 parents c519afb + 53bd48a commit 182b849
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
58 changes: 41 additions & 17 deletions src/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"sort"
"strings"

"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -35,31 +36,54 @@ func (c *ShowCommand) Run(args []string) int {
return 1
}

// Get length of the largest key in order to calculate the
// "whitespace padded" representation of `show`
MaxKeyLen := 0
for k, _ := range secret.Data {
if KeyLen := len(k); KeyLen > MaxKeyLen {
MaxKeyLen = KeyLen
var MaxKeyLen = 0
var keys []string
var has_comments = false

for key, _ := range secret.Data {
// Get length of the largest key in order to calculate the
// "whitespace padded" representation of `show`
if KeyLen := len(key); KeyLen > MaxKeyLen {
MaxKeyLen = KeyLen + 4
}
}

// Add an additional X whitespaces between "key:" and "value"
MaxKeyLen += 4
if strings.HasSuffix(key, "_comment") {
// Check whether a secret contains comments
has_comments = true
} else {
keys = append(keys, key)
}
}

// Sort secrets lexicographically
var keys []string
for k := range secret.Data {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
c.Ui.Output(fmt.Sprintf("%-"+fmt.Sprint(MaxKeyLen)+"v %v",
k+":", // Secret identifier
secret.Data[k])) // Secret value
// Only pad K/V pairs when a secret containts no comments
kv_output_format := "%-" + fmt.Sprint(MaxKeyLen) + "v %v\n"
if has_comments || len(secret.Data) == 1 {
kv_output_format = "%v %v\n"
}

output := ""

for _, key := range keys {
if value, exists := secret.Data[key+"_comment"].(string); exists {

if multilineComments := strings.Split(value, "\n"); len(multilineComments) > 1 {
for _, comment := range multilineComments {
output += "#" + comment + "\n"
}
} else {
output += "#" + value + "\n"
}
}
output += fmt.Sprintf(kv_output_format,
key+":",
secret.Data[key])
}

c.Ui.Output(output)

return 0
}

Expand Down
2 changes: 1 addition & 1 deletion src/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestShow(t *testing.T) {
t.Fatalf("expected error:\n%s\n\nto include: %q", actual, expectedErr)
}

expectedOutput := "key: value"
expectedOutput := "key: value"
if actual := ui.OutputWriter.String(); !strings.Contains(actual, expectedOutput) {
t.Fatalf("expected output:\n%s\n\nto include: %q", actual, expectedOutput)
}
Expand Down

0 comments on commit 182b849

Please sign in to comment.