diff --git a/cmd/artifact_specifier.go b/cmd/artifact_specifier.go new file mode 100644 index 0000000..085de8d --- /dev/null +++ b/cmd/artifact_specifier.go @@ -0,0 +1,44 @@ +/* + * 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 ( + "strconv" + "strings" +) + +// parseImportFileSpecifier parses an import argument of the form: +// +// [:] +// +// It parses from the right to avoid breaking paths that may contain ':' +// characters (e.g. Windows absolute paths like C:\...). +func parseImportFileSpecifier(spec string) (path string, mainArtifact bool) { + mainArtifact = true + + lastColon := strings.LastIndex(spec, ":") + if lastColon == -1 { + return spec, mainArtifact + } + + tail := spec[lastColon+1:] + if b, err := strconv.ParseBool(tail); err == nil { + return spec[:lastColon], b + } + + return spec, mainArtifact +} diff --git a/cmd/artifact_specifier_test.go b/cmd/artifact_specifier_test.go new file mode 100644 index 0000000..f887623 --- /dev/null +++ b/cmd/artifact_specifier_test.go @@ -0,0 +1,41 @@ +/* + * 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 TestParseImportFileSpecifier_SuffixBool(t *testing.T) { + in := "./specs/openapi.yaml:false" + path, main := parseImportFileSpecifier(in) + if path != "./specs/openapi.yaml" { + t.Fatalf("path mismatch: got %q", path) + } + if main != false { + t.Fatalf("mainArtifact mismatch: got %v", main) + } +} + +func TestParseImportFileSpecifier_NoSuffix_Unchanged(t *testing.T) { + in := "./specs/openapi.yaml" + path, main := parseImportFileSpecifier(in) + if path != in { + t.Fatalf("path mismatch: got %q", path) + } + if main != true { + t.Fatalf("mainArtifact mismatch: got %v", main) + } +} diff --git a/cmd/import.go b/cmd/import.go index 99bbcc6..96a7de7 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -18,7 +18,6 @@ package cmd import ( "fmt" "os" - "strconv" "strings" "github.com/microcks/microcks-cli/pkg/config" @@ -126,21 +125,10 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command sepSpecificationFiles := strings.Split(specificationFiles, ",") results := make([]artifactImportResult, 0, len(sepSpecificationFiles)) for _, f := range sepSpecificationFiles { - mainArtifact := true - 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]) - } - } + path, mainArtifact := parseImportFileSpecifier(f) // Try uploading this artifact. - msg, err := mc.UploadArtifact(f, mainArtifact) + msg, err := mc.UploadArtifact(path, mainArtifact) if err != nil { return err } @@ -173,13 +161,11 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command } // Normalize file path to match the watcher fsnotify events format. - if strings.HasPrefix(f, "./") { - f = strings.TrimPrefix(f, "./") - } + path = strings.TrimPrefix(path, "./") // Upsert entry. watchCfg.UpsertEntry(config.WatchEntry{ - FilePath: f, + FilePath: path, Context: []string{globalClientOpts.Context}, MainArtifact: mainArtifact, }) diff --git a/cmd/importURL.go b/cmd/importURL.go index 343145a..19de871 100644 --- a/cmd/importURL.go +++ b/cmd/importURL.go @@ -17,6 +17,7 @@ package cmd import ( + "fmt" "strconv" "strings"