From 21eb039be728bfc522dd92f03723496886bb1e13 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Wed, 22 Feb 2023 15:27:54 -0600 Subject: [PATCH] :bug: (flags): Fix error when value has consecutive newlines --- internal/util/flags.go | 3 ++- internal/util/flags_test.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/util/flags.go b/internal/util/flags.go index 2cff702..590ffca 100644 --- a/internal/util/flags.go +++ b/internal/util/flags.go @@ -2,6 +2,7 @@ package util import ( "github.com/clevyr/yampl/cmd" + "regexp" "strings" ) @@ -15,7 +16,7 @@ func FixStringToStringNewlines(s []string) []string { replaceNext = false if strings.ContainsRune(arg, '=') { if strings.ContainsRune(arg, '\n') { - arg = strings.ReplaceAll(arg, "\n", ",") + arg = regexp.MustCompile("\n+").ReplaceAllString(arg, ",") arg = strings.Trim(arg, ",") arg = strings.Replace(arg, "=,", "=", 1) s[i] = arg diff --git a/internal/util/flags_test.go b/internal/util/flags_test.go index 4632c63..f1ba1c8 100644 --- a/internal/util/flags_test.go +++ b/internal/util/flags_test.go @@ -21,6 +21,7 @@ func TestFixStringToStringNewlines(t *testing.T) { {"newline in file", args{[]string{"yampl", "test\nfile.yaml"}}, []string{"yampl", "test\nfile.yaml"}}, {"newline after end of options", args{[]string{"yampl", "-v=a=a", "---", "-v\nfile.yaml"}}, []string{"yampl", "-v=a=a", "---", "-v\nfile.yaml"}}, {"trim newline", args{[]string{"yampl", "-v=\na=a\n"}}, []string{"yampl", "-v=a=a"}}, + {"collapse newlines", args{[]string{"yampl", "-v=a=a\n\nb=b"}}, []string{"yampl", "-v=a=a,b=b"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {