diff --git a/artifactory/cli.go b/artifactory/cli.go index 85f8520c5..0b8fa158c 100644 --- a/artifactory/cli.go +++ b/artifactory/cli.go @@ -1962,6 +1962,19 @@ func curlCmd(c *cli.Context) error { if err != nil { return err } + + // Check if --server-id is explicitly passed in arguments + flagIndex, _, _, err := coreutils.FindFlag("--server-id", cliutils.ExtractCommand(c)) + if err != nil { + return err + } + // If --server-id is NOT present, then we check for JFROG_CLI_SERVER_ID env variable + if flagIndex == -1 { + if artDetails, err := cliutils.CreateArtifactoryDetailsByFlags(c); err == nil && artDetails.ArtifactoryUrl != "" { + rtCurlCommand.SetServerDetails(artDetails) + rtCurlCommand.SetUrl(artDetails.ArtifactoryUrl) + } + } return commands.Exec(rtCurlCommand) } diff --git a/artifactory_test.go b/artifactory_test.go index 1737fe05d..7e35ab696 100644 --- a/artifactory_test.go +++ b/artifactory_test.go @@ -5543,15 +5543,41 @@ func TestArtifactoryCurl(t *testing.T) { _, err := createServerConfigAndReturnPassphrase(t) defer deleteServerConfig(t) assert.NoError(t, err) - // Check curl command with config default server - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version") - assert.NoError(t, err) - // Check curl command with '--server-id' flag - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId) - assert.NoError(t, err) - // Check curl command with invalid server id - should get an error. - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_"+tests.ServerId) - assert.Error(t, err) + + baseArgs := []string{"curl", "-XGET", "/api/system/version"} + + testRuns := []struct { + testName string + serverIDEnvValue string + expectedErr bool + serverID string + }{ + {"defaultConfig", "", false, ""}, + {"serverIdFlag", "", false, tests.ServerId}, + {"invalidServerId", "", true, "not_configured_name_" + tests.ServerId}, + {"envVarSet", tests.ServerId, false, ""}, + {"envVarWithFlag", tests.ServerId, false, tests.ServerId}, + {"priorityFlagOverEnv", "wrong_server_id", false, tests.ServerId}, + {"priorityEnvOverDefault", tests.ServerId, false, ""}, + } + + for _, test := range testRuns { + t.Run(test.testName, func(t *testing.T) { + setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.ServerID, test.serverIDEnvValue) + + args := append([]string{}, baseArgs...) + if test.serverID != "" { + args = append(args, "--server-id="+test.serverID) + } + err = artifactoryCli.WithoutCredentials().Exec(args...) + if test.expectedErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + setEnvCallBack() + }) + } cleanArtifactoryTest() }