Skip to content

Commit 7bd7346

Browse files
authored
Fix rt curl ServerID environment variable usage (#2882)
1 parent f8b6f63 commit 7bd7346

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

artifactory/cli.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,19 @@ func curlCmd(c *cli.Context) error {
19621962
if err != nil {
19631963
return err
19641964
}
1965+
1966+
// Check if --server-id is explicitly passed in arguments
1967+
flagIndex, _, _, err := coreutils.FindFlag("--server-id", cliutils.ExtractCommand(c))
1968+
if err != nil {
1969+
return err
1970+
}
1971+
// If --server-id is NOT present, then we check for JFROG_CLI_SERVER_ID env variable
1972+
if flagIndex == -1 {
1973+
if artDetails, err := cliutils.CreateArtifactoryDetailsByFlags(c); err == nil && artDetails.ArtifactoryUrl != "" {
1974+
rtCurlCommand.SetServerDetails(artDetails)
1975+
rtCurlCommand.SetUrl(artDetails.ArtifactoryUrl)
1976+
}
1977+
}
19651978
return commands.Exec(rtCurlCommand)
19661979
}
19671980

artifactory_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5543,15 +5543,41 @@ func TestArtifactoryCurl(t *testing.T) {
55435543
_, err := createServerConfigAndReturnPassphrase(t)
55445544
defer deleteServerConfig(t)
55455545
assert.NoError(t, err)
5546-
// Check curl command with config default server
5547-
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version")
5548-
assert.NoError(t, err)
5549-
// Check curl command with '--server-id' flag
5550-
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId)
5551-
assert.NoError(t, err)
5552-
// Check curl command with invalid server id - should get an error.
5553-
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_"+tests.ServerId)
5554-
assert.Error(t, err)
5546+
5547+
baseArgs := []string{"curl", "-XGET", "/api/system/version"}
5548+
5549+
testRuns := []struct {
5550+
testName string
5551+
serverIDEnvValue string
5552+
expectedErr bool
5553+
serverID string
5554+
}{
5555+
{"defaultConfig", "", false, ""},
5556+
{"serverIdFlag", "", false, tests.ServerId},
5557+
{"invalidServerId", "", true, "not_configured_name_" + tests.ServerId},
5558+
{"envVarSet", tests.ServerId, false, ""},
5559+
{"envVarWithFlag", tests.ServerId, false, tests.ServerId},
5560+
{"priorityFlagOverEnv", "wrong_server_id", false, tests.ServerId},
5561+
{"priorityEnvOverDefault", tests.ServerId, false, ""},
5562+
}
5563+
5564+
for _, test := range testRuns {
5565+
t.Run(test.testName, func(t *testing.T) {
5566+
setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.ServerID, test.serverIDEnvValue)
5567+
5568+
args := append([]string{}, baseArgs...)
5569+
if test.serverID != "" {
5570+
args = append(args, "--server-id="+test.serverID)
5571+
}
5572+
err = artifactoryCli.WithoutCredentials().Exec(args...)
5573+
if test.expectedErr {
5574+
assert.Error(t, err)
5575+
} else {
5576+
assert.NoError(t, err)
5577+
}
5578+
setEnvCallBack()
5579+
})
5580+
}
55555581

55565582
cleanArtifactoryTest()
55575583
}

0 commit comments

Comments
 (0)