Skip to content

Commit

Permalink
✨ (flags): Replace newlines with commas before parsing flags
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Feb 22, 2023
1 parent 7e58da4 commit 049e664
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/flag_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import (

var rawValues map[string]string

const (
ValueFlag = "value"
ValueFlagShort = "v"
)

func init() {
Command.Flags().StringToStringVarP(&rawValues, "value", "v", rawValues, "Define a template variable. Can be used more than once.")
Command.Flags().StringToStringVarP(&rawValues, ValueFlag, ValueFlagShort, rawValues, "Define a template variable. Can be used more than once.")
err := Command.RegisterFlagCompletionFunc("value", valueCompletion)
if err != nil {
panic(err)
Expand Down
30 changes: 30 additions & 0 deletions internal/util/flags.go
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)
}
55 changes: 55 additions & 0 deletions internal/util/flags_test.go
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)
}
})
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"github.com/clevyr/yampl/cmd"
"github.com/clevyr/yampl/internal/util"
"os"
)

func main() {
os.Args = util.FixStringToStringNewlines(os.Args)
if err := cmd.Command.Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit 049e664

Please sign in to comment.