From b5285b59aab2c34881706956405f3298e6adc4e2 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Thu, 9 Nov 2017 12:16:23 -0500 Subject: [PATCH] [FAB-6930] Permit lists with brackets Prior to this change set, you had to specify lists as a comma-separated lists without surrounding brackets as follows: a,b,c This change set allows either of the following two forms: a,b,c [a,b,c] For example: --tls.certfiles a,b or --tls.certfiles [a,b] Change-Id: Iccfe4aa3dc1c0a7f71edccbcca19f2a7ee70db67 Signed-off-by: Keith Smith --- util/util.go | 5 +++++ util/util_test.go | 29 +++++++++-------------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/util/util.go b/util/util.go index 5e1433190..eba61342c 100644 --- a/util/util.go +++ b/util/util.go @@ -641,6 +641,11 @@ func NormalizeStringSlice(slice []string) []string { if len(slice) > 0 { for _, item := range slice { + // Remove surrounding brackets "[]" if specified + if strings.HasPrefix(item, "[") && strings.HasSuffix(item, "]") { + item = item[1 : len(item)-1] + } + // Split elements based on comma and add to normalized slice if strings.Contains(item, ",") { normalizedSlice = append(normalizedSlice, strings.Split(item, ",")...) } else { diff --git a/util/util_test.go b/util/util_test.go index b7a8e5115..1f345db75 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -514,29 +514,18 @@ func TestStructToString(t *testing.T) { assert.NotContains(t, idStr, "barpwd", "Identity password is not masked in the output") } +// Test file list with multiple and single entries both with and without brackets func TestNormalizeFileList(t *testing.T) { - var err error - slice := []string{"file1,file2", "file3,file4"} - - slice, err = NormalizeFileList(slice, "../testdata") + slice := []string{"[file0,file1]", "file2,file3", "file4", "[file5]"} + slice, err := NormalizeFileList(slice, "../testdata") if err != nil { - t.Error("Failed to normalize files list, error: ", err) - } - - if !strings.Contains(slice[0], "file1") { - t.Error("Failed to correctly normalize files list") - } - - if strings.Contains(slice[0], "file2") { - t.Error("Should have failed, first element should not contain 'file2'") - } - - if !strings.Contains(slice[1], "file2") { - t.Error("Failed to correctly normalize files list") + t.Fatalf("Failed to normalize files list, error: %s", err) } - - if !strings.Contains(slice[3], "file4") { - t.Error("Failed to correctly normalize files list") + assert.Equal(t, 6, len(slice), "Invalid slice length") + for i := range slice { + if !strings.HasSuffix(slice[i], fmt.Sprintf("file%d", i)) { + t.Errorf("Failed to normalize files list for element %d; found '%s'", i, slice[i]) + } } }