diff --git a/cmd/ocm/account/quota/cmd.go b/cmd/ocm/account/quota/cmd.go index ef6a8fc1..28d42833 100644 --- a/cmd/ocm/account/quota/cmd.go +++ b/cmd/ocm/account/quota/cmd.go @@ -23,8 +23,8 @@ import ( "github.com/spf13/cobra" - "github.com/openshift-online/ocm-cli/pkg/config" "github.com/openshift-online/ocm-cli/pkg/dump" + "github.com/openshift-online/ocm-cli/pkg/ocm" amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1" ) @@ -60,29 +60,12 @@ func init() { } func run(cmd *cobra.Command, argv []string) error { - // Load the configuration file: - cfg, err := config.Load() - if err != nil { - return fmt.Errorf("Can't load config file: %v", err) - } - if cfg == nil { - return fmt.Errorf("Not logged in, run the 'login' command") - } - // Check that the configuration has credentials or tokens that haven't have expired: - armed, reason, err := cfg.Armed() + // Create the client for the OCM API: + connection, err := ocm.NewConnection().Build() if err != nil { return err } - if !armed { - return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) - } - - // Create the connection, and remember to close it: - connection, err := cfg.Connection() - if err != nil { - return fmt.Errorf("Can't create connection: %v", err) - } defer connection.Close() orgID := args.org diff --git a/cmd/ocm/account/roles/cmd.go b/cmd/ocm/account/roles/cmd.go index 9a850480..648fa2a0 100644 --- a/cmd/ocm/account/roles/cmd.go +++ b/cmd/ocm/account/roles/cmd.go @@ -22,8 +22,8 @@ import ( "github.com/spf13/cobra" - "github.com/openshift-online/ocm-cli/pkg/config" "github.com/openshift-online/ocm-cli/pkg/dump" + "github.com/openshift-online/ocm-cli/pkg/ocm" amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1" ) @@ -57,29 +57,11 @@ func init() { func run(cmd *cobra.Command, argv []string) error { - // Load the configuration file: - cfg, err := config.Load() - if err != nil { - return fmt.Errorf("Can't load config file: %v", err) - } - if cfg == nil { - return fmt.Errorf("Not logged in, run the 'login' command") - } - - // Check that the configuration has credentials or tokens that haven't have expired: - armed, reason, err := cfg.Armed() + // Create the client for the OCM API: + connection, err := ocm.NewConnection().Build() if err != nil { return err } - if !armed { - return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) - } - - // Create the connection, and remember to close it: - connection, err := cfg.Connection() - if err != nil { - return fmt.Errorf("Can't create connection: %v", err) - } defer connection.Close() // No role name was provided; Print all roles. diff --git a/cmd/ocm/account/status/cmd.go b/cmd/ocm/account/status/cmd.go index 7a97060a..1bedfd4a 100644 --- a/cmd/ocm/account/status/cmd.go +++ b/cmd/ocm/account/status/cmd.go @@ -23,6 +23,7 @@ import ( acc_util "github.com/openshift-online/ocm-cli/pkg/account" "github.com/openshift-online/ocm-cli/pkg/config" + "github.com/openshift-online/ocm-cli/pkg/ocm" amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1" ) @@ -61,20 +62,11 @@ func run(cmd *cobra.Command, argv []string) error { return fmt.Errorf("Not logged in, run the 'login' command") } - // Check that the configuration has credentials or tokens that haven't have expired: - armed, reason, err := cfg.Armed() + // Create the client for the OCM API: + connection, err := ocm.NewConnection().Build() if err != nil { return err } - if !armed { - return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) - } - - // Create the connection, and remember to close it: - connection, err := cfg.Connection() - if err != nil { - return fmt.Errorf("Can't create connection: %v", err) - } defer connection.Close() // Send the request: @@ -87,7 +79,7 @@ func run(cmd *cobra.Command, argv []string) error { // Display user and which server they are logged into currAccount := response.Body() currOrg := currAccount.Organization() - fmt.Printf("User %s on %s in org '%s' %s (external_id: %s)", + fmt.Printf("User %s on %s in org '%s' %s (external_id: %s) ", currAccount.Username(), cfg.URL, currOrg.Name(), currOrg.ID(), currOrg.ExternalID()) // Display roles currently assigned to the user diff --git a/cmd/ocm/account/users/cmd.go b/cmd/ocm/account/users/cmd.go index b7c3137f..e4467208 100644 --- a/cmd/ocm/account/users/cmd.go +++ b/cmd/ocm/account/users/cmd.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" acc_util "github.com/openshift-online/ocm-cli/pkg/account" - "github.com/openshift-online/ocm-cli/pkg/config" + "github.com/openshift-online/ocm-cli/pkg/ocm" amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1" ) @@ -74,29 +74,11 @@ func init() { func run(cmd *cobra.Command, argv []string) error { - // Load the configuration file: - cfg, err := config.Load() - if err != nil { - return fmt.Errorf("Can't load config file: %v", err) - } - if cfg == nil { - return fmt.Errorf("Not logged in, run the 'login' command") - } - - // Check that the configuration has credentials or tokens that haven't have expired: - armed, reason, err := cfg.Armed() + // Create the client for the OCM API: + connection, err := ocm.NewConnection().Build() if err != nil { return err } - if !armed { - return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) - } - - // Create the connection, and remember to close it: - connection, err := cfg.Connection() - if err != nil { - return fmt.Errorf("Can't create connection: %v", err) - } defer connection.Close() // needed variables: diff --git a/cmd/ocm/get/cmd.go b/cmd/ocm/get/cmd.go index 422725b6..8d796130 100644 --- a/cmd/ocm/get/cmd.go +++ b/cmd/ocm/get/cmd.go @@ -25,6 +25,7 @@ import ( "github.com/openshift-online/ocm-cli/pkg/arguments" "github.com/openshift-online/ocm-cli/pkg/config" "github.com/openshift-online/ocm-cli/pkg/dump" + "github.com/openshift-online/ocm-cli/pkg/ocm" "github.com/openshift-online/ocm-cli/pkg/urls" ) @@ -69,20 +70,12 @@ func run(cmd *cobra.Command, argv []string) error { return fmt.Errorf("Not logged in, run the 'login' command") } - // Check that the configuration has credentials or tokens that don't have expired: - armed, reason, err := cfg.Armed() + // Create the client for the OCM API: + connection, err := ocm.NewConnection().Build() if err != nil { return err } - if !armed { - return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) - } - - // Create the connection: - connection, err := cfg.Connection() - if err != nil { - return fmt.Errorf("Can't create connection: %v", err) - } + defer connection.Close() // Create and populate the request: request := connection.Get() diff --git a/cmd/ocm/login/cmd.go b/cmd/ocm/login/cmd.go index f574cc7d..a6681b1c 100644 --- a/cmd/ocm/login/cmd.go +++ b/cmd/ocm/login/cmd.go @@ -25,6 +25,7 @@ import ( "time" "github.com/openshift-online/ocm-cli/pkg/config" + "github.com/openshift-online/ocm-cli/pkg/properties" "github.com/openshift-online/ocm-cli/pkg/urls" sdk "github.com/openshift-online/ocm-sdk-go" "github.com/openshift-online/ocm-sdk-go/authentication" @@ -313,6 +314,10 @@ func run(cmd *cobra.Command, argv []string) error { gatewayURL = fmt.Sprintf("https://%s", regValue.URL) } + if overrideUrl := os.Getenv(properties.URLEnvKey); overrideUrl != "" { + fmt.Fprintf(os.Stderr, "WARNING: the `%s` environment variable is set, but is not used for the login command. The `ocm login` command will only use the explicitly set flag's url, which is set as %s\n", properties.URLEnvKey, gatewayURL) //nolint:lll + } + // Update the configuration with the values given in the command line: cfg.TokenURL = tokenURL cfg.ClientID = clientID diff --git a/pkg/ocm/connection.go b/pkg/ocm/connection.go index 59fd7ff7..ff17899b 100644 --- a/pkg/ocm/connection.go +++ b/pkg/ocm/connection.go @@ -15,10 +15,13 @@ package ocm import ( "fmt" + "os" sdk "github.com/openshift-online/ocm-sdk-go" "github.com/openshift-online/ocm-cli/pkg/config" + "github.com/openshift-online/ocm-cli/pkg/debug" + "github.com/openshift-online/ocm-cli/pkg/properties" ) // ConnectionBuilder contains the information and logic needed to build a connection to OCM. Don't @@ -63,6 +66,15 @@ func (b *ConnectionBuilder) Build() (result *sdk.Connection, err error) { return } + // overwrite the config URL if the environment variable is set + if overrideUrl := os.Getenv(properties.URLEnvKey); overrideUrl != "" { + if debug.Enabled() { + fmt.Fprintf(os.Stderr, "INFO: %s is overridden via environment variable. This functionality is considered tech preview and may cause unexpected issues.\n", properties.URLEnvKey) //nolint:lll + fmt.Fprintf(os.Stderr, " If you experience issues while %s is set, unset the %s environment variable and attempt to log in directly to the desired OCM environment.\n\n", properties.URLEnvKey, properties.URLEnvKey) //nolint:lll + } + b.cfg.URL = overrideUrl + } + result, err = b.cfg.Connection() if err != nil { return diff --git a/pkg/properties/properties.go b/pkg/properties/properties.go index 643510a2..214d5d17 100644 --- a/pkg/properties/properties.go +++ b/pkg/properties/properties.go @@ -16,4 +16,7 @@ limitations under the License. package properties -const KeyringEnvKey = "OCM_KEYRING" +const ( + KeyringEnvKey = "OCM_KEYRING" + URLEnvKey = "OCM_URL" +)