Skip to content

Commit 68aee7c

Browse files
authored
fix-curl-cmd-arg-parsing-issue (#1485)
1 parent 576ae49 commit 68aee7c

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

common/commands/curl.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package commands
22

33
import (
44
"fmt"
5-
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
65
"io"
76
"os/exec"
87
"strings"
98

9+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
10+
1011
gofrogcmd "github.com/jfrog/gofrog/io"
1112
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1213
"github.com/jfrog/jfrog-client-go/utils/errorutils"
@@ -145,14 +146,28 @@ func (curlCmd *CurlCommand) GetServerDetails() (*config.ServerDetails, error) {
145146
// Use this method ONLY after removing all JFrog-CLI flags, i.e. flags in the form: '--my-flag=value' are not allowed.
146147
// An argument is any provided candidate which is not a flag or a flag value.
147148
func (curlCmd *CurlCommand) findUriValueAndIndex() (int, string) {
149+
cntArgs := 0
150+
for range curlCmd.arguments {
151+
cntArgs++
152+
}
153+
148154
skipThisArg := false
149155
for index, arg := range curlCmd.arguments {
156+
if skipThisArg && !strings.HasPrefix(arg, "-") && index == cntArgs-1 {
157+
return index, arg
158+
}
159+
150160
// Check if shouldn't check current arg.
151-
if skipThisArg {
161+
if skipThisArg && !strings.HasPrefix(arg, "-") {
152162
skipThisArg = false
153163
continue
154164
}
155165

166+
// If we were expecting a value but found a flag instead, reset skipThisArg
167+
if skipThisArg && strings.HasPrefix(arg, "-") {
168+
skipThisArg = false
169+
}
170+
156171
// If starts with '--', meaning a flag which its value is at next slot.
157172
if strings.HasPrefix(arg, "--") {
158173
skipThisArg = true

common/commands/curl_test.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ func TestFindNextArg(t *testing.T) {
1010
{"-X", "GET", "arg1", "--foo", "bar"},
1111
{"-X", "GET", "--server-idea", "foo", "/api/arg2"},
1212
{"-XGET", "--foo", "bar", "--foo-bar", "meow", "arg3"},
13+
{"-o", "helm.tar.gz", "-L", "-vvv", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
14+
{"-o", "helm.tar.gz", "-vvv", "-L", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
15+
{"-o", "helm.tar.gz", "-L", "-s", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
16+
{"-L", "-o", "helm.tar.gz", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
17+
{"-o", "helm.tar.gz", "-L", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
18+
{"-L", "-vvv", "-o", "helm.tar.gz", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
19+
{"-L", "-o", "helm.tar.gz", "-vvv", "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
20+
{"-vvv", "-s", "-L", "api/test"},
21+
{"-LsS", "api/test2"},
22+
{"-ofile.txt", "-L", "api/test3"},
1323
}
1424

1525
expected := []struct {
@@ -19,6 +29,24 @@ func TestFindNextArg(t *testing.T) {
1929
{2, "arg1"},
2030
{4, "/api/arg2"},
2131
{5, "arg3"},
32+
{4, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
33+
{4, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
34+
{4, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
35+
{3, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
36+
{3, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
37+
{4, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
38+
{4, "helm-sh/helm-v3.19.0-linux-amd64.tar.gz"},
39+
{3, "api/test"},
40+
{1, "api/test2"},
41+
{2, "api/test3"},
42+
{-1, ""},
43+
{4, "api/endpoint"},
44+
{0, "api/endpoint"},
45+
{2, "api/endpoint"},
46+
{2, "api/endpoint"},
47+
{-1, ""},
48+
{-1, ""},
49+
{-1, ""},
2250
}
2351

2452
for index, test := range args {
@@ -68,15 +96,15 @@ func TestBuildCommandUrl(t *testing.T) {
6896
uriValue string
6997
expectErr bool
7098
}{
71-
{"test1", []string{"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, 2, "http://artifactory:8081/artifactory/api/build/test1", false},
72-
{"test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, 2, "http://artifactory:8081/artifactory/api/build/test2", false},
73-
{"test3", []string{"-XGET", "--/api/build/test3", "--server-id="}, 1, "http://artifactory:8081/artifactory/api/build/test3", true},
74-
{"test4", []string{"-XGET", "-Test4", "--server-id", "bar"}, 1, "http://artifactory:8081/artifactory/api/build/test4", true},
75-
{"test5", []string{"-X", "GET", "api/build/test5", "--server-id", "test5", "--foo", "bar"}, 2, "http://artifactory:8081/artifactory/api/build/test5", false},
99+
{"test1", []string{"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, 2, "https://artifactory:8081/artifactory/api/build/test1", false},
100+
{"test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, 2, "https://artifactory:8081/artifactory/api/build/test2", false},
101+
{"test3", []string{"-XGET", "--/api/build/test3", "--server-id="}, 1, "https://artifactory:8081/artifactory/api/build/test3", true},
102+
{"test4", []string{"-XGET", "-Test4", "--server-id", "bar"}, 3, "https://artifactory:8081/artifactory/bar", false},
103+
{"test5", []string{"-X", "GET", "api/build/test5", "--server-id", "test5", "--foo", "bar"}, 2, "https://artifactory:8081/artifactory/api/build/test5", false},
76104
}
77105

78106
command := &CurlCommand{}
79-
urlPrefix := "http://artifactory:8081/artifactory/"
107+
urlPrefix := "https://artifactory:8081/artifactory/"
80108
for _, test := range tests {
81109
command.arguments = test.arguments
82110
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)