Skip to content

Commit

Permalink
fix(cli): resource path with quotes and spaces
Browse files Browse the repository at this point in the history
Closes #2417
  • Loading branch information
squakez authored and astefanutti committed Jun 21, 2021
1 parent 409b355 commit e58a782
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
39 changes: 22 additions & 17 deletions pkg/cmd/run_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const (
ConfigOptionTypeFile configOptionType = "file"
)

var validConfigRegexp = regexp.MustCompile(`^(configmap|secret|file)\:([\w\.\-\_\:\/@]+)$`)
var validConfigSecretRegexp = regexp.MustCompile(`^(configmap|secret)\:([\w\.\-\_\:\/@]+)$`)
var validFileRegexp = regexp.MustCompile(`^file\:([\w\.\-\_\:\/@" ]+)$`)
var validResourceRegexp = regexp.MustCompile(`^([\w\.\-\_\:]+)(\/([\w\.\-\_\:]+))?(\@([\w\.\-\_\:\/]+))?$`)

func newRunConfigOption(configType configOptionType, value string) *RunConfigOption {
Expand Down Expand Up @@ -130,7 +131,7 @@ func ParseResourceOption(item string) (*RunConfigOption, error) {
// then replace with parseOption() func directly
option, err := parseOption(item)
if err != nil {
if strings.HasPrefix(err.Error(), "could not match configuration") {
if strings.HasPrefix(err.Error(), "could not match config, secret or file configuration") {
fmt.Printf("Warn: --resource %s has been deprecated. You should use --resource file:%s instead.\n", item, item)
return parseOption("file:" + item)
}
Expand All @@ -145,24 +146,28 @@ func ParseConfigOption(item string) (*RunConfigOption, error) {
}

func parseOption(item string) (*RunConfigOption, error) {
if !validConfigRegexp.MatchString(item) {
return nil, fmt.Errorf("could not match configuration %s, must match %v regular expression", item, validConfigRegexp)
}
// Parse the regexp groups
groups := validConfigRegexp.FindStringSubmatch(item)
var cot configOptionType
switch groups[1] {
case "configmap":
cot = ConfigOptionTypeConfigmap
case "secret":
cot = ConfigOptionTypeSecret
case "file":
var value string
if validConfigSecretRegexp.MatchString(item) {
// parse as secret/configmap
groups := validConfigSecretRegexp.FindStringSubmatch(item)
switch groups[1] {
case "configmap":
cot = ConfigOptionTypeConfigmap
case "secret":
cot = ConfigOptionTypeSecret
}
value = groups[2]
} else if validFileRegexp.MatchString(item) {
//parse as file
groups := validFileRegexp.FindStringSubmatch(item)
cot = ConfigOptionTypeFile
default:
// Should never reach this
return nil, fmt.Errorf("invalid config option type %s", groups[1])
value = groups[1]
} else {
return nil, fmt.Errorf("could not match config, secret or file configuration as %s", item)
}
configurationOption := newRunConfigOption(cot, groups[2])

configurationOption := newRunConfigOption(cot, value)
if err := configurationOption.Validate(); err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/run_help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestParseConfigOptionAllParams(t *testing.T) {
sec4 := "secret:sec"
file1 := "file:/path/to/my-file.txt@/tmp/file.txt"
file2 := "file:/path/to/my-file.txt"
file3 := "file:/path to/my-file.txt"

parsedCm1, err := ParseConfigOption(cm1)
assert.Nil(t, err)
Expand Down Expand Up @@ -132,6 +133,13 @@ func TestParseConfigOptionAllParams(t *testing.T) {
assert.Equal(t, "/path/to/my-file.txt", parsedFile2.Name())
assert.Equal(t, "", parsedFile2.Key())
assert.Equal(t, "", parsedFile2.DestinationPath())

parsedFile3, err := ParseConfigOption(file3)
assert.Nil(t, err)
assert.Equal(t, "file", parsedFile3.Type())
assert.Equal(t, "/path to/my-file.txt", parsedFile3.Name())
assert.Equal(t, "", parsedFile3.Key())
assert.Equal(t, "", parsedFile3.DestinationPath())
}

func TestFilterFileLocation(t *testing.T) {
Expand Down

0 comments on commit e58a782

Please sign in to comment.