diff --git a/cmd/rhoas/main.go b/cmd/rhoas/main.go index 4d4896458..977b2e1c1 100644 --- a/cmd/rhoas/main.go +++ b/cmd/rhoas/main.go @@ -33,14 +33,12 @@ func main() { buildVersion := build.Version cmdFactory := defaultfactory.New(localizer) - err = initConfig(cmdFactory) - if err != nil { + if err = initConfig(cmdFactory); err != nil { cmdFactory.Logger.Errorf(localizer.MustLocalize("main.config.error", localize.NewEntry("Error", err))) os.Exit(1) } - err = initProfiles(cmdFactory) - if err != nil { + if err = initProfiles(cmdFactory); err != nil { cmdFactory.Logger.Errorf(localizer.MustLocalize("main.context.error", localize.NewEntry("Error", err))) os.Exit(1) } diff --git a/pkg/cmd/context/context.go b/pkg/cmd/context/context.go index 099b525aa..9609e1965 100644 --- a/pkg/cmd/context/context.go +++ b/pkg/cmd/context/context.go @@ -4,8 +4,10 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/context/create" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/delete" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/list" - "github.com/redhat-developer/app-services-cli/pkg/cmd/context/status" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/use" + kafkaUse "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/use" + registryUse "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/use" + "github.com/redhat-developer/app-services-cli/pkg/cmd/status" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" "github.com/spf13/cobra" ) @@ -20,12 +22,25 @@ func NewContextCmd(f *factory.Factory) *cobra.Command { Args: cobra.NoArgs, } + // The implementation of `rhoas kafka use` command has been aliased here as `rhoas context kafka-use` + kafkaUseCmd := kafkaUse.NewUseCommand(f) + kafkaUseCmd.Use = "kafka-use" + + // The implementation of `rhoas service-registry use` command has been aliased here as `rhoas context service-registry-use` + registryUseCmd := registryUse.NewUseCommand(f) + registryUseCmd.Use = "service-registry-use" + cmd.AddCommand( use.NewUseCommand(f), status.NewStatusCommand(f), list.NewListCommand(f), create.NewCreateCommand(f), delete.NewDeleteCommand(f), + kafkaUseCmd, + registryUseCmd, + + // `rhoas status` cmd has been re-used as `rhoas context status` + status.NewStatusCommand(f), ) return cmd } diff --git a/pkg/cmd/context/contextcmdutil/flagset.go b/pkg/cmd/context/contextcmdutil/flagset.go new file mode 100644 index 000000000..b2002653a --- /dev/null +++ b/pkg/cmd/context/contextcmdutil/flagset.go @@ -0,0 +1,35 @@ +package contextcmdutil + +import ( + "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" + "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/spf13/cobra" +) + +type FlagSet struct { + cmd *cobra.Command + factory *factory.Factory + *flagutil.FlagSet +} + +// NewFlagSet returns a new flag set with common context flags +func NewFlagSet(cmd *cobra.Command, f *factory.Factory) *FlagSet { + return &FlagSet{ + cmd: cmd, + factory: f, + FlagSet: flagutil.NewFlagSet(cmd, f.Localizer), + } +} + +// AddContextName adds a flag for setting the name of the context +func (fs *FlagSet) AddContextName(name *string) { + flagName := "name" + + fs.StringVar( + name, + flagName, + "", + fs.factory.Localizer.MustLocalize("context.common.flag.name"), + ) + +} diff --git a/pkg/cmd/context/create/create.go b/pkg/cmd/context/create/create.go index 40e2f396a..7127a5122 100644 --- a/pkg/cmd/context/create/create.go +++ b/pkg/cmd/context/create/create.go @@ -64,9 +64,9 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command { }, } - flags := flagutil.NewFlagSet(cmd, opts.localizer) + flags := contextcmdutil.NewFlagSet(cmd, f) - flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.AddContextName(&opts.name) flags.BoolVar(&opts.autoUse, "use", true, opts.localizer.MustLocalize("context.create.flag.use")) flags.StringVar(&opts.kafkaID, "kafka-id", "", opts.localizer.MustLocalize("context.create.flag.kafkaID")) flags.StringVar(&opts.registryID, "registry-id", "", opts.localizer.MustLocalize("context.create.flag.registryID")) @@ -95,7 +95,7 @@ func runCreate(opts *options) error { profiles := svcContext.Contexts if profiles == nil { - profiles = map[string]servicecontext.ServiceConfig{} + profiles = make(map[string]servicecontext.ServiceConfig) } if opts.interactive { diff --git a/pkg/cmd/context/delete/delete.go b/pkg/cmd/context/delete/delete.go index 3a8575aa0..c866591ed 100644 --- a/pkg/cmd/context/delete/delete.go +++ b/pkg/cmd/context/delete/delete.go @@ -3,6 +3,7 @@ package delete import ( "context" + "github.com/redhat-developer/app-services-cli/pkg/cmd/context/contextcmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" @@ -52,9 +53,9 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { }, } - flags := flagutil.NewFlagSet(cmd, opts.localizer) + flags := contextcmdutil.NewFlagSet(cmd, f) - flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.AddContextName(&opts.name) return cmd } @@ -66,23 +67,24 @@ func runDelete(opts *options) error { return err } + profileHandler := &profileutil.ContextHandler{ + Context: svcContext, + Localizer: opts.localizer, + } + if opts.name == "" { - if svcContext.CurrentContext == "" { - return opts.localizer.MustLocalizeError("context.common.error.notSet") + + currCtx, newErr := profileHandler.GetCurrentContext() + if newErr != nil { + return newErr } - opts.name = svcContext.CurrentContext + opts.name = currCtx svcContext.CurrentContext = "" } - profileHandler := &profileutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - _, err = profileHandler.GetContext(opts.name) - if err != nil { + if _, err = profileHandler.GetContext(opts.name); err != nil { return err } diff --git a/pkg/cmd/context/list/list.go b/pkg/cmd/context/list/list.go index 66bd0298a..6e252bf89 100644 --- a/pkg/cmd/context/list/list.go +++ b/pkg/cmd/context/list/list.go @@ -57,7 +57,7 @@ func runList(opts *options) error { profiles := svcContext.Contexts if profiles == nil { - profiles = map[string]servicecontext.ServiceConfig{} + profiles = make(map[string]servicecontext.ServiceConfig) } currentCtx := svcContext.CurrentContext diff --git a/pkg/cmd/context/status/status.go b/pkg/cmd/context/status/status.go deleted file mode 100644 index e3234cf02..000000000 --- a/pkg/cmd/context/status/status.go +++ /dev/null @@ -1,94 +0,0 @@ -package status - -import ( - "context" - - "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" - "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" - "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" - "github.com/redhat-developer/app-services-cli/pkg/core/localize" - "github.com/redhat-developer/app-services-cli/pkg/core/logging" - "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" - "github.com/redhat-developer/app-services-cli/pkg/shared/factory" - "github.com/spf13/cobra" - - "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" -) - -type options struct { - IO *iostreams.IOStreams - Logger logging.Logger - Connection factory.ConnectionFunc - localizer localize.Localizer - Context context.Context - ServiceContext servicecontext.IContext - - name string - outputFormat string -} - -// NewStatusCommand creates a new command to display status of a context -func NewStatusCommand(f *factory.Factory) *cobra.Command { - - opts := &options{ - Connection: f.Connection, - IO: f.IOStreams, - Logger: f.Logger, - localizer: f.Localizer, - ServiceContext: f.ServiceContext, - } - - cmd := &cobra.Command{ - Use: "status", - Short: f.Localizer.MustLocalize("context.status.cmd.shortDescription"), - Long: f.Localizer.MustLocalize("context.status.cmd.shortDescription"), - Example: f.Localizer.MustLocalize("context.status.cmd.example"), - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - return runStatus(opts) - }, - } - - flags := flagutil.NewFlagSet(cmd, opts.localizer) - - flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) - flags.AddOutput(&opts.outputFormat) - - return cmd -} - -func runStatus(opts *options) error { - - var currentCtx *servicecontext.ServiceConfig - var err error - - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &profileutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - if opts.name != "" { - currentCtx, err = profileHandler.GetContext(opts.name) - if err != nil { - return err - } - } else { - currentCtx, err = profileHandler.GetContext(svcContext.CurrentContext) - if err != nil { - return err - } - } - - stdout := opts.IO.Out - err = dump.Formatted(stdout, opts.outputFormat, currentCtx) - if err != nil { - return err - } - - return nil -} diff --git a/pkg/cmd/context/use/use.go b/pkg/cmd/context/use/use.go index e1e4a0da5..6a07bd7e5 100644 --- a/pkg/cmd/context/use/use.go +++ b/pkg/cmd/context/use/use.go @@ -4,6 +4,7 @@ import ( "context" "github.com/AlecAivazis/survey/v2" + "github.com/redhat-developer/app-services-cli/pkg/cmd/context/contextcmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" @@ -53,9 +54,9 @@ func NewUseCommand(f *factory.Factory) *cobra.Command { }, } - flags := flagutil.NewFlagSet(cmd, opts.localizer) + flags := contextcmdutil.NewFlagSet(cmd, f) - flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.AddContextName(&opts.name) return cmd } @@ -101,7 +102,7 @@ func runInteractivePrompt(opts *options, context *servicecontext.Context) (strin profiles := context.Contexts if profiles == nil { - profiles = map[string]servicecontext.ServiceConfig{} + profiles = make(map[string]servicecontext.ServiceConfig) } profileNames := make([]string, 0, len(profiles)) diff --git a/pkg/cmd/kafka/describe/describe.go b/pkg/cmd/kafka/describe/describe.go index c17344a03..96e996020 100644 --- a/pkg/cmd/kafka/describe/describe.go +++ b/pkg/cmd/kafka/describe/describe.go @@ -33,7 +33,7 @@ type options struct { } // NewDescribeCommand describes a Kafka instance, either by passing an `--id flag` -// or by using the kafka instance set in the config, if any +// or by using the kafka instance set in the current context, if any func NewDescribeCommand(f *factory.Factory) *cobra.Command { opts := &options{ Config: f.Config, diff --git a/pkg/cmd/kafka/use/use.go b/pkg/cmd/kafka/use/use.go index fc62f43e1..d2c819c09 100644 --- a/pkg/cmd/kafka/use/use.go +++ b/pkg/cmd/kafka/use/use.go @@ -11,9 +11,11 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" "github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" "github.com/spf13/cobra" @@ -24,22 +26,24 @@ type options struct { name string interactive bool - IO *iostreams.IOStreams - Config config.IConfig - Connection factory.ConnectionFunc - Logger logging.Logger - localizer localize.Localizer - Context context.Context + IO *iostreams.IOStreams + Config config.IConfig + Connection factory.ConnectionFunc + Logger logging.Logger + localizer localize.Localizer + Context context.Context + ServiceContext servicecontext.IContext } func NewUseCommand(f *factory.Factory) *cobra.Command { opts := &options{ - Config: f.Config, - Connection: f.Connection, - Logger: f.Logger, - IO: f.IOStreams, - localizer: f.Localizer, - Context: f.Context, + Config: f.Config, + Connection: f.Connection, + Logger: f.Logger, + IO: f.IOStreams, + localizer: f.Localizer, + Context: f.Context, + ServiceContext: f.ServiceContext, } cmd := &cobra.Command{ @@ -89,7 +93,26 @@ func runUse(opts *options) error { } } - cfg, err := opts.Config.Load() + svcContext, err := opts.ServiceContext.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: svcContext, + Localizer: opts.localizer, + } + + if _, newErr := profileHandler.GetCurrentContext(); newErr != nil { + return newErr + } + + currCtx, err := profileHandler.GetCurrentContext() + if err != nil { + return err + } + + svcConfig, err := profileHandler.GetContext(currCtx) if err != nil { return err } @@ -114,14 +137,11 @@ func runUse(opts *options) error { } } - // build Kafka config object from the response - var kafkaConfig = config.KafkaConfig{ - ClusterID: res.GetId(), - } - nameTmplEntry := localize.NewEntry("Name", res.GetName()) - cfg.Services.Kafka = &kafkaConfig - if err := opts.Config.Save(cfg); err != nil { + svcConfig.KafkaID = res.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + + if err := opts.ServiceContext.Save(svcContext); err != nil { saveErrMsg := opts.localizer.MustLocalize("kafka.use.error.saveError", nameTmplEntry) return fmt.Errorf("%v: %w", saveErrMsg, err) } diff --git a/pkg/cmd/registry/describe/describe.go b/pkg/cmd/registry/describe/describe.go index 626b01b0c..1dde96f39 100644 --- a/pkg/cmd/registry/describe/describe.go +++ b/pkg/cmd/registry/describe/describe.go @@ -32,7 +32,7 @@ type options struct { } // NewDescribeCommand describes a service instance, either by passing an `--id flag` -// or by using the service instance set in the config, if any +// or by using the service instance set in the current context, if any func NewDescribeCommand(f *factory.Factory) *cobra.Command { opts := &options{ Config: f.Config, diff --git a/pkg/cmd/registry/use/use.go b/pkg/cmd/registry/use/use.go index 92409c250..461e597f1 100644 --- a/pkg/cmd/registry/use/use.go +++ b/pkg/cmd/registry/use/use.go @@ -9,8 +9,10 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/redhat-developer/app-services-cli/pkg/shared/serviceregistryutil" srsmgmtv1 "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client" @@ -22,22 +24,24 @@ type options struct { name string interactive bool - IO *iostreams.IOStreams - Config config.IConfig - Connection factory.ConnectionFunc - Logger logging.Logger - localizer localize.Localizer - Context context.Context + IO *iostreams.IOStreams + Config config.IConfig + Connection factory.ConnectionFunc + Logger logging.Logger + localizer localize.Localizer + Context context.Context + ServiceContext servicecontext.IContext } func NewUseCommand(f *factory.Factory) *cobra.Command { opts := &options{ - Config: f.Config, - Connection: f.Connection, - Logger: f.Logger, - IO: f.IOStreams, - localizer: f.Localizer, - Context: f.Context, + Config: f.Config, + Connection: f.Connection, + Logger: f.Logger, + IO: f.IOStreams, + localizer: f.Localizer, + Context: f.Context, + ServiceContext: f.ServiceContext, } cmd := &cobra.Command{ @@ -81,7 +85,22 @@ func runUse(opts *options) error { } } - cfg, err := opts.Config.Load() + svcContext, err := opts.ServiceContext.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: svcContext, + Localizer: opts.localizer, + } + + currCtx, err := profileHandler.GetCurrentContext() + if err != nil { + return err + } + + svcConfig, err := profileHandler.GetContext(currCtx) if err != nil { return err } @@ -106,14 +125,11 @@ func runUse(opts *options) error { } } - registryConfig := &config.ServiceRegistryConfig{ - InstanceID: registry.GetId(), - Name: *registry.Name, - } - nameTmplEntry := localize.NewEntry("Name", registry.GetName()) - cfg.Services.ServiceRegistry = registryConfig - if err := opts.Config.Save(cfg); err != nil { + svcConfig.ServiceRegistryID = registry.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + + if err := opts.ServiceContext.Save(svcContext); err != nil { saveErrMsg := opts.localizer.MustLocalize("registry.use.error.saveError", nameTmplEntry) return fmt.Errorf("%v: %w", saveErrMsg, err) } diff --git a/pkg/cmd/status/status.go b/pkg/cmd/status/status.go index 9ba4aad33..e08191573 100644 --- a/pkg/cmd/status/status.go +++ b/pkg/cmd/status/status.go @@ -9,34 +9,39 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/redhat-developer/app-services-cli/pkg/shared/servicespec" "github.com/spf13/cobra" ) type options struct { - IO *iostreams.IOStreams - Config config.IConfig - Logger logging.Logger - Connection factory.ConnectionFunc - localizer localize.Localizer - Context context.Context + IO *iostreams.IOStreams + Config config.IConfig + Logger logging.Logger + Connection factory.ConnectionFunc + localizer localize.Localizer + Context context.Context + ServiceContext servicecontext.IContext outputFormat string + name string services []string } func NewStatusCommand(f *factory.Factory) *cobra.Command { opts := &options{ - IO: f.IOStreams, - Config: f.Config, - Connection: f.Connection, - Logger: f.Logger, - services: servicespec.AllServiceLabels, - localizer: f.Localizer, - Context: f.Context, + IO: f.IOStreams, + Config: f.Config, + Connection: f.Connection, + Logger: f.Logger, + services: servicespec.AllServiceLabels, + localizer: f.Localizer, + Context: f.Context, + ServiceContext: f.ServiceContext, } cmd := &cobra.Command{ @@ -66,6 +71,9 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command { }, } + flags := flagutil.NewFlagSet(cmd, opts.localizer) + + flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "", opts.localizer.MustLocalize("status.flag.output.description")) flagutil.EnableOutputFlagCompletion(cmd) @@ -83,11 +91,36 @@ func runStatus(opts *options) error { opts.Logger.Debug(opts.localizer.MustLocalize("status.log.debug.requestingStatusOfServices"), opts.services) } + svcContext, err := opts.ServiceContext.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: svcContext, + Localizer: opts.localizer, + } + + var svcConfig *servicecontext.ServiceConfig + + if opts.name != "" { + svcConfig, err = profileHandler.GetContext(opts.name) + if err != nil { + return err + } + } else { + svcConfig, err = profileHandler.GetContext(svcContext.CurrentContext) + if err != nil { + return err + } + } + statusClient := newStatusClient(&clientConfig{ - config: opts.Config, - connection: conn, - Logger: opts.Logger, - localizer: opts.localizer, + config: opts.Config, + connection: conn, + Logger: opts.Logger, + localizer: opts.localizer, + serviceConfig: svcConfig, }) status, ok, err := statusClient.BuildStatus(opts.Context, opts.services) diff --git a/pkg/cmd/status/statusBuilder.go b/pkg/cmd/status/statusBuilder.go index 4e098c0e5..e5b00063e 100644 --- a/pkg/cmd/status/statusBuilder.go +++ b/pkg/cmd/status/statusBuilder.go @@ -17,6 +17,7 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/openconfig/goyang/pkg/indent" @@ -45,46 +46,45 @@ type registryStatus struct { } type clientConfig struct { - config config.IConfig - Logger logging.Logger - connection connection.Connection - localizer localize.Localizer + config config.IConfig + Logger logging.Logger + connection connection.Connection + localizer localize.Localizer + serviceConfig *servicecontext.ServiceConfig } type statusClient struct { - config config.IConfig - Logger logging.Logger - conn connection.Connection - localizer localize.Localizer + config config.IConfig + Logger logging.Logger + conn connection.Connection + localizer localize.Localizer + serviceConfig *servicecontext.ServiceConfig } // newStatusClient returns a new client to fetch service statuses // and build it into a service status config object func newStatusClient(cfg *clientConfig) *statusClient { return &statusClient{ - config: cfg.config, - Logger: cfg.Logger, - conn: cfg.connection, - localizer: cfg.localizer, + config: cfg.config, + Logger: cfg.Logger, + conn: cfg.connection, + localizer: cfg.localizer, + serviceConfig: cfg.serviceConfig, } } // BuildStatus gets the status of all services currently set in the user config func (c *statusClient) BuildStatus(ctx context.Context, services []string) (status *serviceStatus, ok bool, err error) { - cfg, err := c.config.Load() - if err != nil { - return nil, false, err - } status = &serviceStatus{} if stringInSlice(servicespec.KafkaServiceName, services) { - if instanceID, exists := cfg.GetKafkaIdOk(); exists { + if c.serviceConfig.KafkaID != "" { // nolint:govet - kafkaStatus, err := c.getKafkaStatus(ctx, instanceID) + kafkaStatus, err := c.getKafkaStatus(ctx, c.serviceConfig.KafkaID) if err != nil { if kafkamgmtv1errors.IsAPIError(err, kafkamgmtv1errors.ERROR_7) { - err = kafkautil.NotFoundByIDError(instanceID) + err = kafkautil.NotFoundByIDError(c.serviceConfig.KafkaID) c.Logger.Error(err) c.Logger.Info(c.localizer.MustLocalize("status.log.info.rhoasKafkaUse")) } @@ -98,12 +98,11 @@ func (c *statusClient) BuildStatus(ctx context.Context, services []string) (stat } if stringInSlice(servicespec.ServiceRegistryServiceName, services) { - registryCfg := cfg.Services.ServiceRegistry - if registryCfg != nil && registryCfg.InstanceID != "" { + if c.serviceConfig.ServiceRegistryID != "" { // nolint:govet - registry, newErr := c.getRegistryStatus(ctx, registryCfg.InstanceID) + registry, newErr := c.getRegistryStatus(ctx, c.serviceConfig.ServiceRegistryID) if newErr != nil { - return status, ok, err + return status, ok, newErr } status.Registry = registry ok = true diff --git a/pkg/core/localize/locales/en/cmd/context.en.toml b/pkg/core/localize/locales/en/cmd/context.en.toml index 53c1a73fa..09fc7ed9f 100644 --- a/pkg/core/localize/locales/en/cmd/context.en.toml +++ b/pkg/core/localize/locales/en/cmd/context.en.toml @@ -5,10 +5,10 @@ one='Group, share and manage your rhoas services' one=''' rhoas context commands allow developers to: -* Group services into contexts that can be used with a number of CLI commands. -* Manage different service contexts by switching, listing and removing service contexts -* Share context with others to use the same set of services -* Generating configuration for connecting to the services from various platforms and tools + * Group services into contexts that can be used with a number of CLI commands. + * Manage different service contexts by switching, listing and removing service contexts + * Share context with others to use the same set of services + * Generating configuration for connecting to the services from various platforms and tools ''' [context.cmd.example] diff --git a/pkg/shared/profileutil/util.go b/pkg/shared/profileutil/util.go index 6aa470616..585dd54a0 100644 --- a/pkg/shared/profileutil/util.go +++ b/pkg/shared/profileutil/util.go @@ -20,7 +20,7 @@ type ContextHandler struct { // GetCurrentKafkaInstance returns the Kafka instance set in the currently selected context func (c *ContextHandler) GetCurrentKafkaInstance(api kafkamgmtclient.DefaultApi) (*kafkamgmtclient.KafkaRequest, error) { - currentCtx, err := c.getCurrentContext() + currentCtx, err := c.GetCurrentContext() if err != nil { return nil, err } @@ -36,7 +36,7 @@ func (c *ContextHandler) GetCurrentKafkaInstance(api kafkamgmtclient.DefaultApi) kafkaInstance, _, err := api.GetKafkaById(context.Background(), s.KafkaID).Execute() if kafkamgmtv1errors.IsAPIError(err, kafkamgmtv1errors.ERROR_7) { - return nil, c.Localizer.MustLocalizeError("context.common.error.registry.notFound") + return nil, c.Localizer.MustLocalizeError("context.common.error.kafka.notFound") } return &kafkaInstance, err @@ -45,7 +45,7 @@ func (c *ContextHandler) GetCurrentKafkaInstance(api kafkamgmtclient.DefaultApi) // GetCurrentRegistryInstance returns the Service Registry instance set in the currently selected context func (c *ContextHandler) GetCurrentRegistryInstance(api registrymgmtclient.RegistriesApi) (*registrymgmtclient.Registry, error) { - currentCtx, err := c.getCurrentContext() + currentCtx, err := c.GetCurrentContext() if err != nil { return nil, err } @@ -79,8 +79,8 @@ func (c *ContextHandler) GetContext(ctxName string) (*servicecontext.ServiceConf return &currCtx, nil } -// getCurrentContext returns the name of the currently selected context -func (c *ContextHandler) getCurrentContext() (string, error) { +// GetCurrentContext returns the name of the currently selected context +func (c *ContextHandler) GetCurrentContext() (string, error) { if c.Context.CurrentContext == "" { return "", c.Localizer.MustLocalizeError("context.common.error.notSet")