diff --git a/cmd/config.go b/cmd/config.go index 603209a59..a30c82d78 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -38,6 +38,14 @@ var configAnnotations = map[string]string{ "": "The path to your config (use \"-\" for STDIN)", } +func GetConfigAPIHost(cfg *settings.Config) string { + if cfg.Host != defaultHost { + return cfg.Host + } else { + return cfg.ConfigAPIHost + } +} + func newConfigCommand(config *settings.Config) *cobra.Command { opts := configOptions{ cfg: config, @@ -68,7 +76,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Check that the config file is well formed.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.rest = rest.NewFromConfig(config.ConfigAPIHost, config) + opts.rest = rest.NewFromConfig(GetConfigAPIHost(opts.cfg), config) }, RunE: func(cmd *cobra.Command, _ []string) error { return validateConfig(opts, cmd.Flags()) @@ -90,7 +98,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Validate config and display expanded configuration.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.rest = rest.NewFromConfig(config.ConfigAPIHost, config) + opts.rest = rest.NewFromConfig(GetConfigAPIHost(opts.cfg), config) }, RunE: func(cmd *cobra.Command, _ []string) error { return processConfig(opts, cmd.Flags()) diff --git a/cmd/config_test.go b/cmd/config_test.go index d0ed9016d..1d0967e79 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -4,11 +4,15 @@ import ( "fmt" "os/exec" "path/filepath" + "testing" "github.com/CircleCI-Public/circleci-cli/clitest" + "github.com/CircleCI-Public/circleci-cli/cmd" + "github.com/CircleCI-Public/circleci-cli/settings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" + "github.com/stretchr/testify/assert" "gotest.tools/v3/golden" ) @@ -246,3 +250,17 @@ var _ = Describe("Config", func() { }) }) }) + +func TestGetConfigAPIHost(t *testing.T) { + t.Run("tests that we correctly get the config api host when the host is not the default one", func(t *testing.T) { + // if the host isn't equal to `https://circleci.com` then this is likely a server instance and + // wont have the api.X.com subdomain so we should instead just respect the host for config commands + host := cmd.GetConfigAPIHost(&settings.Config{Host: "test"}) + assert.Equal(t, host, "test") + + // If the host passed in is the same as the defaultHost 'https://circleci.com' - then we know this is cloud + // and as such should use the `api.circleci.com` subdomain + host = cmd.GetConfigAPIHost(&settings.Config{Host: "https://circleci.com", ConfigAPIHost: "https://api.circleci.com"}) + assert.Equal(t, host, "https://api.circleci.com") + }) +}