-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace go-shellwords with own shellescape.Split
Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
8 changed files
with
152 additions
and
8 deletions.
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
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
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,60 @@ | ||
package shellescape | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Split splits the input string respecting shell-like quoted segments. | ||
func Split(input string) ([]string, error) { //nolint:cyclop | ||
var segments []string | ||
|
||
currentSegment, ok := builderPool.Get().(*strings.Builder) | ||
if !ok { | ||
currentSegment = &strings.Builder{} | ||
} | ||
defer builderPool.Put(currentSegment) | ||
defer currentSegment.Reset() | ||
|
||
var inDoubleQuotes, inSingleQuotes, isEscaped bool | ||
|
||
for i := 0; i < len(input); i++ { | ||
currentChar := input[i] | ||
|
||
if isEscaped { | ||
currentSegment.WriteByte(currentChar) | ||
isEscaped = false | ||
continue | ||
} | ||
|
||
switch { | ||
case currentChar == '\\' && !inSingleQuotes: | ||
isEscaped = true | ||
case currentChar == '"' && !inSingleQuotes: | ||
inDoubleQuotes = !inDoubleQuotes | ||
case currentChar == '\'' && !inDoubleQuotes: | ||
inSingleQuotes = !inSingleQuotes | ||
case currentChar == ' ' && !inDoubleQuotes && !inSingleQuotes: | ||
// Space outside quotes; delimiter for a new segment | ||
segments = append(segments, currentSegment.String()) | ||
currentSegment.Reset() | ||
default: | ||
currentSegment.WriteByte(currentChar) | ||
} | ||
} | ||
|
||
if inDoubleQuotes || inSingleQuotes { | ||
return nil, fmt.Errorf("split `%q`: %w", input, ErrMismatchedQuotes) | ||
} | ||
|
||
if isEscaped { | ||
return nil, fmt.Errorf("split `%q`: %w", input, ErrTrailingBackslash) | ||
} | ||
|
||
// Add the last segment if present | ||
if currentSegment.Len() > 0 { | ||
segments = append(segments, currentSegment.String()) | ||
} | ||
|
||
return segments, nil | ||
} |
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,75 @@ | ||
package shellescape_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/k0sproject/rig/v2/sh/shellescape" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSplit(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
wantOutput []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Simple Double Quotes Single Value", | ||
input: `"value"`, | ||
wantOutput: []string{"value"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Simple Unquoted Simple Value", | ||
input: "value", | ||
wantOutput: []string{"value"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Two unquoted values", | ||
input: "value1 value2", | ||
wantOutput: []string{"value1", "value2"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "One unquoted and one single quoted value", | ||
input: `value1 'value2 with quotes'`, | ||
wantOutput: []string{"value1", "value2 with quotes"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "One unquoted and one double quoted value", | ||
input: `value1 "value2 with quotes"`, | ||
wantOutput: []string{"value1", "value2 with quotes"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Double quoted value inside single quotes", | ||
input: `value1 'value2 "with" quotes'`, | ||
wantOutput: []string{"value1", `value2 "with" quotes`}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Escaped Single Quote in Single Quotes", | ||
input: `value1 'escaped \'single\' quote'`, | ||
wantOutput: nil, | ||
// expecting an error due to unmatched quotes, first \' is treated as a literal and ' closes the | ||
// quotes. the second \ is treated as an escape for the ' and gets parsed as ' literal. | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotOutput, err := shellescape.Split(tc.input) | ||
if tc.wantErr { | ||
assert.Nil(t, gotOutput, "Split(%q)", tc.input) | ||
assert.Error(t, err, "Split(%q)", tc.input) | ||
} else { | ||
assert.NoError(t, err, "Split(%q)", tc.input) | ||
assert.Equal(t, tc.wantOutput, gotOutput, "Split(%q)", tc.input) | ||
} | ||
}) | ||
} | ||
} |
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