-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix InstallFlags change detection with kubelet-extra-args
Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
7 changed files
with
292 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package shell | ||
|
||
// this is borrowed as-is from rig v2 until k0sctl is updated to use it | ||
|
||
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 := range len(input) { | ||
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,80 @@ | ||
package shell | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// This is borrowed from rig v2 until k0sctl is updated to use it | ||
|
||
var ( | ||
builderPool = sync.Pool{ | ||
New: func() interface{} { | ||
return &strings.Builder{} | ||
}, | ||
} | ||
|
||
// ErrMismatchedQuotes is returned when the input string has mismatched quotes when unquoting. | ||
ErrMismatchedQuotes = errors.New("mismatched quotes") | ||
|
||
// ErrTrailingBackslash is returned when the input string ends with a trailing backslash. | ||
ErrTrailingBackslash = errors.New("trailing backslash") | ||
) | ||
|
||
// Unquote is a mostly POSIX compliant implementation of unquoting a string the same way a shell would. | ||
// Variables and command substitutions are not handled. | ||
func Unquote(input string) (string, error) { //nolint:cyclop | ||
sb, ok := builderPool.Get().(*strings.Builder) | ||
if !ok { | ||
sb = &strings.Builder{} | ||
} | ||
defer builderPool.Put(sb) | ||
defer sb.Reset() | ||
|
||
var inDoubleQuotes, inSingleQuotes, isEscaped bool | ||
|
||
for i := range len(input) { | ||
currentChar := input[i] | ||
|
||
if isEscaped { | ||
sb.WriteByte(currentChar) | ||
isEscaped = false | ||
continue | ||
} | ||
|
||
switch currentChar { | ||
case '\\': | ||
if !inSingleQuotes { // Escape works in double quotes or outside any quotes | ||
isEscaped = true | ||
} else { | ||
sb.WriteByte(currentChar) // Treat as a regular character within single quotes | ||
} | ||
case '"': | ||
if !inSingleQuotes { // Toggle double quotes only if not in single quotes | ||
inDoubleQuotes = !inDoubleQuotes | ||
} else { | ||
sb.WriteByte(currentChar) // Treat as a regular character within single quotes | ||
} | ||
case '\'': | ||
if !inDoubleQuotes { // Toggle single quotes only if not in double quotes | ||
inSingleQuotes = !inSingleQuotes | ||
} else { | ||
sb.WriteByte(currentChar) // Treat as a regular character within double quotes | ||
} | ||
default: | ||
sb.WriteByte(currentChar) | ||
} | ||
} | ||
|
||
if inDoubleQuotes || inSingleQuotes { | ||
return "", fmt.Errorf("unquote `%q`: %w", input, ErrMismatchedQuotes) | ||
} | ||
|
||
if isEscaped { | ||
return "", fmt.Errorf("unquote `%q`: %w", input, ErrTrailingBackslash) | ||
} | ||
|
||
return sb.String(), 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
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
Oops, something went wrong.