diff --git a/cmd/importURL.go b/cmd/importURL.go index cb1640f..b111908 100644 --- a/cmd/importURL.go +++ b/cmd/importURL.go @@ -97,25 +97,9 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm } sepSpecificationFiles := strings.Split(specificationFiles, ",") for _, f := range sepSpecificationFiles { - mainArtifact := true - secret := "" - - // Check if URL starts with https or http - if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") { - urlAndMainAtrifactAndSecretName := strings.Split(f, ":") - n := len(urlAndMainAtrifactAndSecretName) - f = urlAndMainAtrifactAndSecretName[0] + ":" + urlAndMainAtrifactAndSecretName[1] - if n > 2 { - val, err := strconv.ParseBool(urlAndMainAtrifactAndSecretName[2]) - if err != nil { - fmt.Println(err) - } - mainArtifact = val - } - if n > 3 { - secret = urlAndMainAtrifactAndSecretName[3] - } - } + var mainArtifact bool + var secret string + f, mainArtifact, secret = parseImportURLArg(f) // Try downloading the artifcat msg, err := mc.DownloadArtifact(f, mainArtifact, secret) @@ -130,3 +114,46 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm return importURLCmd } + +func parseImportURLArg(f string) (string, bool, string) { + mainArtifact := true + secret := "" + + // Check if URL starts with https or http + if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") { + parts := strings.Split(f, ":") + n := len(parts) + + hasSecret := false + hasMain := false + + if n >= 3 { + // Check if parts[n-2] is a boolean. If it is, then parts[n-1] is the secret, + // and parts[n-2] is mainArtifact. + if val, err := strconv.ParseBool(parts[n-2]); err == nil { + mainArtifact = val + secret = parts[n-1] + hasSecret = true + hasMain = true + } + } + + if !hasSecret && n >= 2 { + // Check if parts[n-1] is a boolean. If it is, then parts[n-1] is mainArtifact. + if val, err := strconv.ParseBool(parts[n-1]); err == nil { + mainArtifact = val + hasMain = true + } + } + + // Reconstruct the URL + if hasSecret { + f = strings.Join(parts[:n-2], ":") + } else if hasMain { + f = strings.Join(parts[:n-1], ":") + } else { + f = strings.Join(parts, ":") + } + } + return f, mainArtifact, secret +} diff --git a/cmd/importURL_test.go b/cmd/importURL_test.go new file mode 100644 index 0000000..a323faa --- /dev/null +++ b/cmd/importURL_test.go @@ -0,0 +1,106 @@ +/* + * 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" + + "github.com/stretchr/testify/assert" +) + +func TestParseImportURLArg(t *testing.T) { + tests := []struct { + name string + input string + expectedURL string + expectedMainArtifact bool + expectedSecret string + }{ + { + name: "Standard URL without suffixes", + input: "https://example.com/openapi.yaml", + expectedURL: "https://example.com/openapi.yaml", + expectedMainArtifact: true, + expectedSecret: "", + }, + { + name: "Standard URL with mainArtifact suffix true", + input: "https://example.com/spec1.yaml:true", + expectedURL: "https://example.com/spec1.yaml", + expectedMainArtifact: true, + expectedSecret: "", + }, + { + name: "Standard URL with mainArtifact suffix false", + input: "https://example.com/spec1.yaml:false", + expectedURL: "https://example.com/spec1.yaml", + expectedMainArtifact: false, + expectedSecret: "", + }, + { + name: "Standard URL with mainArtifact and secret", + input: "https://example.com/spec1.yaml:true:my-secret", + expectedURL: "https://example.com/spec1.yaml", + expectedMainArtifact: true, + expectedSecret: "my-secret", + }, + { + name: "URL with port and no suffixes", + input: "http://localhost:8585/spec.yaml", + expectedURL: "http://localhost:8585/spec.yaml", + expectedMainArtifact: true, + expectedSecret: "", + }, + { + name: "URL with port and mainArtifact suffix true", + input: "http://localhost:8585/spec.yaml:true", + expectedURL: "http://localhost:8585/spec.yaml", + expectedMainArtifact: true, + expectedSecret: "", + }, + { + name: "URL with port and mainArtifact suffix false", + input: "http://localhost:8585/spec.yaml:false", + expectedURL: "http://localhost:8585/spec.yaml", + expectedMainArtifact: false, + expectedSecret: "", + }, + { + name: "URL with port, mainArtifact and secret", + input: "http://localhost:8585/spec.yaml:true:my-secret-token", + expectedURL: "http://localhost:8585/spec.yaml", + expectedMainArtifact: true, + expectedSecret: "my-secret-token", + }, + { + name: "URL with port, mainArtifact false and secret", + input: "http://localhost:8585/spec.yaml:false:my-secret-token", + expectedURL: "http://localhost:8585/spec.yaml", + expectedMainArtifact: false, + expectedSecret: "my-secret-token", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + url, mainArtifact, secret := parseImportURLArg(tt.input) + assert.Equal(t, tt.expectedURL, url) + assert.Equal(t, tt.expectedMainArtifact, mainArtifact) + assert.Equal(t, tt.expectedSecret, secret) + }) + } +}