Skip to content

Commit 3fb94dc

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

2 files changed

Lines changed: 152 additions & 19 deletions

File tree

cmd/importURL.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,9 @@ 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+
var mainArtifact bool
101+
var secret string
102+
f, mainArtifact, secret = parseImportURLArg(f)
119103

120104
// Try downloading the artifcat
121105
msg, err := mc.DownloadArtifact(f, mainArtifact, secret)
@@ -130,3 +114,46 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
130114

131115
return importURLCmd
132116
}
117+
118+
func parseImportURLArg(f string) (string, bool, string) {
119+
mainArtifact := true
120+
secret := ""
121+
122+
// Check if URL starts with https or http
123+
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
124+
parts := strings.Split(f, ":")
125+
n := len(parts)
126+
127+
hasSecret := false
128+
hasMain := false
129+
130+
if n >= 3 {
131+
// Check if parts[n-2] is a boolean. If it is, then parts[n-1] is the secret,
132+
// and parts[n-2] is mainArtifact.
133+
if val, err := strconv.ParseBool(parts[n-2]); err == nil {
134+
mainArtifact = val
135+
secret = parts[n-1]
136+
hasSecret = true
137+
hasMain = true
138+
}
139+
}
140+
141+
if !hasSecret && n >= 2 {
142+
// Check if parts[n-1] is a boolean. If it is, then parts[n-1] is mainArtifact.
143+
if val, err := strconv.ParseBool(parts[n-1]); err == nil {
144+
mainArtifact = val
145+
hasMain = true
146+
}
147+
}
148+
149+
// Reconstruct the URL
150+
if hasSecret {
151+
f = strings.Join(parts[:n-2], ":")
152+
} else if hasMain {
153+
f = strings.Join(parts[:n-1], ":")
154+
} else {
155+
f = strings.Join(parts, ":")
156+
}
157+
}
158+
return f, mainArtifact, secret
159+
}

cmd/importURL_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
17+
package cmd
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestParseImportURLArg(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
input string
29+
expectedURL string
30+
expectedMainArtifact bool
31+
expectedSecret string
32+
}{
33+
{
34+
name: "Standard URL without suffixes",
35+
input: "https://example.com/openapi.yaml",
36+
expectedURL: "https://example.com/openapi.yaml",
37+
expectedMainArtifact: true,
38+
expectedSecret: "",
39+
},
40+
{
41+
name: "Standard URL with mainArtifact suffix true",
42+
input: "https://example.com/spec1.yaml:true",
43+
expectedURL: "https://example.com/spec1.yaml",
44+
expectedMainArtifact: true,
45+
expectedSecret: "",
46+
},
47+
{
48+
name: "Standard URL with mainArtifact suffix false",
49+
input: "https://example.com/spec1.yaml:false",
50+
expectedURL: "https://example.com/spec1.yaml",
51+
expectedMainArtifact: false,
52+
expectedSecret: "",
53+
},
54+
{
55+
name: "Standard URL with mainArtifact and secret",
56+
input: "https://example.com/spec1.yaml:true:my-secret",
57+
expectedURL: "https://example.com/spec1.yaml",
58+
expectedMainArtifact: true,
59+
expectedSecret: "my-secret",
60+
},
61+
{
62+
name: "URL with port and no suffixes",
63+
input: "http://localhost:8585/spec.yaml",
64+
expectedURL: "http://localhost:8585/spec.yaml",
65+
expectedMainArtifact: true,
66+
expectedSecret: "",
67+
},
68+
{
69+
name: "URL with port and mainArtifact suffix true",
70+
input: "http://localhost:8585/spec.yaml:true",
71+
expectedURL: "http://localhost:8585/spec.yaml",
72+
expectedMainArtifact: true,
73+
expectedSecret: "",
74+
},
75+
{
76+
name: "URL with port and mainArtifact suffix false",
77+
input: "http://localhost:8585/spec.yaml:false",
78+
expectedURL: "http://localhost:8585/spec.yaml",
79+
expectedMainArtifact: false,
80+
expectedSecret: "",
81+
},
82+
{
83+
name: "URL with port, mainArtifact and secret",
84+
input: "http://localhost:8585/spec.yaml:true:my-secret-token",
85+
expectedURL: "http://localhost:8585/spec.yaml",
86+
expectedMainArtifact: true,
87+
expectedSecret: "my-secret-token",
88+
},
89+
{
90+
name: "URL with port, mainArtifact false and secret",
91+
input: "http://localhost:8585/spec.yaml:false:my-secret-token",
92+
expectedURL: "http://localhost:8585/spec.yaml",
93+
expectedMainArtifact: false,
94+
expectedSecret: "my-secret-token",
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
url, mainArtifact, secret := parseImportURLArg(tt.input)
101+
assert.Equal(t, tt.expectedURL, url)
102+
assert.Equal(t, tt.expectedMainArtifact, mainArtifact)
103+
assert.Equal(t, tt.expectedSecret, secret)
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)