Skip to content

Commit

Permalink
chore(command): Add separate Quoter interface to fix Stringers not be…
Browse files Browse the repository at this point in the history
…ing quoted
  • Loading branch information
gabe565 committed Mar 8, 2024
1 parent f10d18b commit a814592
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
8 changes: 7 additions & 1 deletion internal/command/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Builder struct {
cmd []any
}

type Quoter interface {
Quote() string
}

func (j *Builder) Push(p ...any) *Builder {
j.cmd = append(j.cmd, p...)
return j
Expand All @@ -33,8 +37,10 @@ func (j Builder) String() string {
switch v := v.(type) {
case string:
buf.WriteString(shellescape.Quote(v))
case Quoter:
buf.WriteString(v.Quote())
case fmt.Stringer:
buf.WriteString(v.String())
buf.WriteString(shellescape.Quote(v.String()))
default:
panic(fmt.Errorf("unknown value in command: %#v", v))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestBuilder_String(t *testing.T) {
{"simple", fields{[]any{"echo", "hello", "world"}}, "echo hello world", false},
{"pipe", fields{[]any{"echo", "hello", "world", Pipe, "cat"}}, "echo hello world | cat", false},
{"escape", fields{[]any{"echo", "hello world"}}, "echo 'hello world'", false},
{"env", fields{[]any{Env{"MESSAGE", "hello world"}, "env"}}, Env{"MESSAGE", "hello world"}.String() + " env", false},
{"env", fields{[]any{Env{"MESSAGE", "hello world"}, "env"}}, Env{"MESSAGE", "hello world"}.Quote() + " env", false},
{"panic", fields{[]any{0}}, "", true},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type Env struct {
Value string
}

func (e Env) String() string {
func (e Env) Quote() string {
return e.Key + "=" + shellescape.Quote(e.Value)
}
4 changes: 2 additions & 2 deletions internal/command/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestEnv_String(t *testing.T) {
func TestEnv_Quote(t *testing.T) {
type fields struct {
Key string
Value string
Expand All @@ -25,7 +25,7 @@ func TestEnv_String(t *testing.T) {
Key: tt.fields.Key,
Value: tt.fields.Value,
}
got := e.String()
got := e.Quote()
assert.Equal(t, tt.want, got)
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package command

type Raw string

func (r Raw) String() string {
func (r Raw) Quote() string {
return string(r)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/command/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Split string

func (s Split) String() string {
func (s Split) Quote() string {
var inSingleString bool
var inDoubleString bool
var wasEscaped bool
Expand Down
4 changes: 2 additions & 2 deletions internal/command/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSplit_String(t *testing.T) {
func TestSplit_Quote(t *testing.T) {
tests := []struct {
name string
s Split
Expand All @@ -23,7 +23,7 @@ func TestSplit_String(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.s.String(), "String()")
assert.Equalf(t, tt.want, tt.s.Quote(), "String()")
})
}
}

0 comments on commit a814592

Please sign in to comment.