Skip to content

Commit 71d9bc2

Browse files
committed
fix : parsing-for-port-url
Signed-off-by: aniket866 <iamaniketkumarmaner@gmail.com>
1 parent f883c02 commit 71d9bc2

2 files changed

Lines changed: 118 additions & 20 deletions

File tree

cmd/importURL.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,10 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
9797
}
9898
sepSpecificationFiles := strings.Split(specificationFiles, ",")
9999
for _, f := range sepSpecificationFiles {
100-
mainArtifact := true
101-
secret := ""
102-
103-
// Check if URL starts with https or http
104-
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
105-
urlAndMainAtrifactAndSecretName := strings.Split(f, ":")
106-
n := len(urlAndMainAtrifactAndSecretName)
107-
f = urlAndMainAtrifactAndSecretName[0] + ":" + urlAndMainAtrifactAndSecretName[1]
108-
if n > 2 {
109-
val, err := strconv.ParseBool(urlAndMainAtrifactAndSecretName[2])
110-
if err != nil {
111-
fmt.Println(err)
112-
}
113-
mainArtifact = val
114-
}
115-
if n > 3 {
116-
secret = urlAndMainAtrifactAndSecretName[3]
117-
}
118-
}
100+
parsedURL, mainArtifact, secret := parseImportURLArg(f)
119101

120102
// Try downloading the artifcat
121-
msg, err := mc.DownloadArtifact(f, mainArtifact, secret)
103+
msg, err := mc.DownloadArtifact(parsedURL, mainArtifact, secret)
122104
if err != nil {
123105
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
124106
os.Exit(1)
@@ -130,3 +112,28 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
130112

131113
return importURLCmd
132114
}
115+
116+
func parseImportURLArg(f string) (string, bool, string) {
117+
mainArtifact := true
118+
secret := ""
119+
120+
// Check if URL starts with https or http and safely extract flags from the end
121+
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
122+
parts := strings.Split(f, ":")
123+
n := len(parts)
124+
// If the last parts represent mainArtifact or secret name
125+
if n > 2 {
126+
if val, err := strconv.ParseBool(parts[n-1]); err == nil {
127+
mainArtifact = val
128+
f = strings.Join(parts[:n-1], ":")
129+
} else if n > 3 {
130+
if val, err := strconv.ParseBool(parts[n-2]); err == nil {
131+
mainArtifact = val
132+
secret = parts[n-1]
133+
f = strings.Join(parts[:n-2], ":")
134+
}
135+
}
136+
}
137+
}
138+
return f, mainArtifact, secret
139+
}

cmd/importURL_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestParseImportURLArg(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
input string
28+
expectedURL string
29+
expectedMainArtifact bool
30+
expectedSecret string
31+
}{
32+
{
33+
name: "Simple URL, defaults",
34+
input: "http://localhost/spec.yaml",
35+
expectedURL: "http://localhost/spec.yaml",
36+
expectedMainArtifact: true,
37+
expectedSecret: "",
38+
},
39+
{
40+
name: "URL with port, defaults",
41+
input: "http://localhost:8585/spec.yaml",
42+
expectedURL: "http://localhost:8585/spec.yaml",
43+
expectedMainArtifact: true,
44+
expectedSecret: "",
45+
},
46+
{
47+
name: "URL with port and mainArtifact true",
48+
input: "http://localhost:8585/spec.yaml:true",
49+
expectedURL: "http://localhost:8585/spec.yaml",
50+
expectedMainArtifact: true,
51+
expectedSecret: "",
52+
},
53+
{
54+
name: "URL with port and mainArtifact false",
55+
input: "http://localhost:8585/spec.yaml:false",
56+
expectedURL: "http://localhost:8585/spec.yaml",
57+
expectedMainArtifact: false,
58+
expectedSecret: "",
59+
},
60+
{
61+
name: "URL with port, mainArtifact false, and secret",
62+
input: "http://localhost:8585/spec.yaml:false:mysecret",
63+
expectedURL: "http://localhost:8585/spec.yaml",
64+
expectedMainArtifact: false,
65+
expectedSecret: "mysecret",
66+
},
67+
{
68+
name: "URL with port, mainArtifact true, and secret",
69+
input: "http://localhost:8585/spec.yaml:true:mysecret",
70+
expectedURL: "http://localhost:8585/spec.yaml",
71+
expectedMainArtifact: true,
72+
expectedSecret: "mysecret",
73+
},
74+
{
75+
name: "Simple URL, mainArtifact false, and secret",
76+
input: "http://localhost/spec.yaml:false:mysecret",
77+
expectedURL: "http://localhost/spec.yaml",
78+
expectedMainArtifact: false,
79+
expectedSecret: "mysecret",
80+
},
81+
}
82+
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
url, mainArtifact, secret := parseImportURLArg(tt.input)
86+
assert.Equal(t, tt.expectedURL, url)
87+
assert.Equal(t, tt.expectedMainArtifact, mainArtifact)
88+
assert.Equal(t, tt.expectedSecret, secret)
89+
})
90+
}
91+
}

0 commit comments

Comments
 (0)