diff --git a/cmd/import.go b/cmd/import.go index 28d931c..1f27043 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -30,6 +30,16 @@ import ( "github.com/spf13/cobra" ) +func parseImportFileArg(file string) (string, bool) { + mainArtifact := true + if separator := strings.LastIndex(file, ":"); separator >= 0 { + if parsedMainArtifact, err := strconv.ParseBool(file[separator+1:]); err == nil { + return file[:separator], parsedMainArtifact + } + } + return file, mainArtifact +} + func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { var watch bool var outputFormat string @@ -127,19 +137,9 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command sepSpecificationFiles := strings.Split(specificationFiles, ",") results := make([]artifactImportResult, 0, len(sepSpecificationFiles)) for _, f := range sepSpecificationFiles { - mainArtifact := true + f, mainArtifact := parseImportFileArg(f) var err error - // Check if mainArtifact flag is provided. - if strings.Contains(f, ":") { - pathAndMainArtifact := strings.Split(f, ":") - f = pathAndMainArtifact[0] - mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1]) - if err != nil { - return errors.Wrapf(errors.KindUsage, "cannot parse %q as artifact primary flag", pathAndMainArtifact[1]) - } - } - // Try uploading this artifact. msg, err := mc.UploadArtifact(f, mainArtifact) if err != nil { diff --git a/cmd/import_test.go b/cmd/import_test.go new file mode 100644 index 0000000..2e44d91 --- /dev/null +++ b/cmd/import_test.go @@ -0,0 +1,83 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import "testing" + +func TestParseImportFileArg(t *testing.T) { + tests := []struct { + name string + input string + wantFile string + wantMainArtifact bool + }{ + { + name: "Windows absolute path", + input: `C:\Temp\api.yaml`, + wantFile: `C:\Temp\api.yaml`, + wantMainArtifact: true, + }, + { + name: "Windows absolute path with false suffix", + input: `C:\Temp\api.yaml:false`, + wantFile: `C:\Temp\api.yaml`, + wantMainArtifact: false, + }, + { + name: "relative path defaults to main artifact", + input: "./api.yaml", + wantFile: "./api.yaml", + wantMainArtifact: true, + }, + { + name: "relative path with true suffix", + input: "./api.yaml:true", + wantFile: "./api.yaml", + wantMainArtifact: true, + }, + { + name: "relative path with false suffix", + input: "./api.yaml:false", + wantFile: "./api.yaml", + wantMainArtifact: false, + }, + { + name: "colon-containing filename", + input: "path:with:colon.yaml", + wantFile: "path:with:colon.yaml", + wantMainArtifact: true, + }, + { + name: "colon-containing filename with false suffix", + input: "path:with:colon.yaml:false", + wantFile: "path:with:colon.yaml", + wantMainArtifact: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotFile, gotMainArtifact := parseImportFileArg(tt.input) + if gotFile != tt.wantFile { + t.Errorf("file = %q, want %q", gotFile, tt.wantFile) + } + if gotMainArtifact != tt.wantMainArtifact { + t.Errorf("mainArtifact = %t, want %t", gotMainArtifact, tt.wantMainArtifact) + } + }) + } +}