-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (flags): Replace newlines with commas before parsing flags
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/clevyr/yampl/cmd" | ||
"strings" | ||
) | ||
|
||
func FixStringToStringNewlines(s []string) []string { | ||
var replaceNext bool | ||
for i, arg := range s { | ||
switch { | ||
case arg == "---": | ||
return s | ||
case hasValueFlag(arg) || replaceNext: | ||
replaceNext = false | ||
if strings.ContainsRune(arg, '=') { | ||
if strings.ContainsRune(arg, '\n') { | ||
s[i] = strings.ReplaceAll(arg, "\n", ",") | ||
} | ||
} else { | ||
replaceNext = true | ||
} | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func hasValueFlag(s string) bool { | ||
return strings.HasPrefix(s, "-"+cmd.ValueFlagShort) || strings.HasPrefix(s, "--"+cmd.ValueFlag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package util | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFixStringToStringNewlines(t *testing.T) { | ||
type args struct { | ||
s []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{"no value flag", args{[]string{"yampl"}}, []string{"yampl"}}, | ||
{"no newline", args{[]string{"yampl", "--value=a=a"}}, []string{"yampl", "--value=a=a"}}, | ||
{"newline with equal", args{[]string{"yampl", "--value=a=a\nb=b"}}, []string{"yampl", "--value=a=a,b=b"}}, | ||
{"newline with space", args{[]string{"yampl", "--value", "a=a\nb=b"}}, []string{"yampl", "--value", "a=a,b=b"}}, | ||
{"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"}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FixStringToStringNewlines(tt.args.s); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("FixStringToStringNewlines() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_hasValueFlag(t *testing.T) { | ||
type args struct { | ||
s string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{"no flag", args{"yampl"}, false}, | ||
{"normal", args{"--value"}, true}, | ||
{"normal with value", args{"--value=test"}, true}, | ||
{"shorthand", args{"-v"}, true}, | ||
{"shorthand with value", args{"-v=test"}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := hasValueFlag(tt.args.s); got != tt.want { | ||
t.Errorf("hasValueFlag() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters