From b29ae1d2d2224445e67beae55dfd84b45d66b871 Mon Sep 17 00:00:00 2001 From: Patrick Winter Date: Thu, 29 Jun 2017 15:26:42 +0200 Subject: [PATCH 1/2] prefix comments in the output `vc show` by # --- src/show.go | 56 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/show.go b/src/show.go index ed49452..72cebfd 100644 --- a/src/show.go +++ b/src/show.go @@ -3,6 +3,7 @@ package main import ( "fmt" "sort" + "strings" "github.com/mitchellh/cli" ) @@ -35,29 +36,50 @@ 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]) } return 0 From 53bd48a89eceb3aa5d7e65f6c1316dc800b64694 Mon Sep 17 00:00:00 2001 From: Patrick Winter Date: Thu, 29 Jun 2017 15:36:57 +0200 Subject: [PATCH 2/2] fixed unit tests --- src/show.go | 2 ++ src/show_test.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/show.go b/src/show.go index 72cebfd..035994f 100644 --- a/src/show.go +++ b/src/show.go @@ -82,6 +82,8 @@ func (c *ShowCommand) Run(args []string) int { secret.Data[key]) } + c.Ui.Output(output) + return 0 } diff --git a/src/show_test.go b/src/show_test.go index 5f00189..6d18803 100644 --- a/src/show_test.go +++ b/src/show_test.go @@ -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) }