diff --git a/pkg/cmd/context/contextcmdutil/validator.go b/pkg/cmd/context/contextcmdutil/validator.go index 652d92271..c2750df34 100644 --- a/pkg/cmd/context/contextcmdutil/validator.go +++ b/pkg/cmd/context/contextcmdutil/validator.go @@ -5,13 +5,14 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/errors" "github.com/redhat-developer/app-services-cli/pkg/core/localize" + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" ) // Validator is a type for validating service context inputs type Validator struct { - Localizer localize.Localizer - ProfileHandler *contextutil.ContextHandler + Localizer localize.Localizer + SvcContext *servicecontext.Context } const ( @@ -42,7 +43,7 @@ func (v *Validator) ValidateName(val interface{}) error { // ValidateNameIsAvailable validates if the name provided is a unique context name func (v *Validator) ValidateNameIsAvailable(name string) error { - context, _ := v.ProfileHandler.GetContext(name) + context, _ := contextutil.GetContext(v.SvcContext, v.Localizer, name) if context != nil { return v.Localizer.MustLocalizeError("context.create.log.alreadyExists", localize.NewEntry("Name", name)) } diff --git a/pkg/cmd/context/create/create.go b/pkg/cmd/context/create/create.go index 970f50cbe..ed56d41e4 100644 --- a/pkg/cmd/context/create/create.go +++ b/pkg/cmd/context/create/create.go @@ -68,14 +68,9 @@ func runCreate(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - profileValidator := &contextcmdutil.Validator{ - Localizer: opts.localizer, - ProfileHandler: profileHandler, + Localizer: opts.localizer, + SvcContext: svcContext, } svcContextsMap := svcContext.Contexts @@ -94,7 +89,7 @@ func runCreate(opts *options) error { return err } - context, _ := profileHandler.GetContext(opts.name) + context, _ := contextutil.GetContext(svcContext, opts.localizer, opts.name) if context != nil { return opts.localizer.MustLocalizeError("context.create.log.alreadyExists", localize.NewEntry("Name", opts.name)) } diff --git a/pkg/cmd/context/delete/delete.go b/pkg/cmd/context/delete/delete.go index 6508888c8..8284eff25 100644 --- a/pkg/cmd/context/delete/delete.go +++ b/pkg/cmd/context/delete/delete.go @@ -61,11 +61,6 @@ func runDelete(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - currCtx := svcContext.CurrentContext if opts.name == "" || opts.name == currCtx { @@ -81,7 +76,7 @@ func runDelete(opts *options) error { opts.Logger.Info(opts.localizer.MustLocalize("context.delete.log.warning.currentUnset")) } - if _, err = profileHandler.GetContext(opts.name); err != nil { + if _, err = contextutil.GetContext(svcContext, opts.localizer, opts.name); err != nil { return err } diff --git a/pkg/cmd/context/use/use.go b/pkg/cmd/context/use/use.go index 62f332c9a..d03fec232 100644 --- a/pkg/cmd/context/use/use.go +++ b/pkg/cmd/context/use/use.go @@ -75,12 +75,7 @@ func runUse(opts *options) error { } } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - _, err = profileHandler.GetContext(opts.name) + _, err = contextutil.GetContext(svcContext, opts.localizer, opts.name) if err != nil { return err } diff --git a/pkg/cmd/kafka/acl/admin/admin.go b/pkg/cmd/kafka/acl/admin/admin.go index 53d4b4acc..4f57c8f80 100644 --- a/pkg/cmd/kafka/acl/admin/admin.go +++ b/pkg/cmd/kafka/acl/admin/admin.go @@ -61,22 +61,8 @@ func NewAdminACLCommand(f *factory.Factory) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { if opts.kafkaID == "" { - svcContext, err := opts.serviceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/acl/create/create.go b/pkg/cmd/kafka/acl/create/create.go index 72eccc7c2..fceb7e6aa 100644 --- a/pkg/cmd/kafka/acl/create/create.go +++ b/pkg/cmd/kafka/acl/create/create.go @@ -78,6 +78,16 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command { return aclcmdutil.BuildInstructions(errorCollection) } + if opts.InstanceID == "" { + + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) + if err != nil { + return err + } + + opts.InstanceID = kafkaInstance.GetId() + } + return runAdd(opts.InstanceID, opts) }, } @@ -231,29 +241,5 @@ func validateAndSetOpts(opts *aclcmdutil.CrudOptions) error { opts.Principal = serviceAccount } - if opts.InstanceID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.Localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) - if err != nil { - return err - } - - opts.InstanceID = kafkaInstance.GetId() - } - return nil } diff --git a/pkg/cmd/kafka/acl/delete/delete.go b/pkg/cmd/kafka/acl/delete/delete.go index e5105c811..d823a1002 100644 --- a/pkg/cmd/kafka/acl/delete/delete.go +++ b/pkg/cmd/kafka/acl/delete/delete.go @@ -71,6 +71,16 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { return aclcmdutil.BuildInstructions(errorCollection) } + if opts.InstanceID == "" { + + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) + if err != nil { + return err + } + + opts.InstanceID = kafkaInstance.GetId() + } + return runDelete(opts.InstanceID, opts) }, } @@ -277,29 +287,5 @@ func validateAndSetOpts(opts *aclcmdutil.CrudOptions) error { opts.Principal = serviceAccount } - if opts.InstanceID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.Localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) - if err != nil { - return err - } - - opts.InstanceID = kafkaInstance.GetId() - } - return nil } diff --git a/pkg/cmd/kafka/acl/grant/grant.go b/pkg/cmd/kafka/acl/grant/grant.go index 1f550bd76..f47202afb 100644 --- a/pkg/cmd/kafka/acl/grant/grant.go +++ b/pkg/cmd/kafka/acl/grant/grant.go @@ -68,22 +68,7 @@ func NewGrantPermissionsACLCommand(f *factory.Factory) *cobra.Command { if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/acl/list/list.go b/pkg/cmd/kafka/acl/list/list.go index 70fe09cdb..be12cfb46 100644 --- a/pkg/cmd/kafka/acl/list/list.go +++ b/pkg/cmd/kafka/acl/list/list.go @@ -75,22 +75,7 @@ func NewListACLCommand(f *factory.Factory) *cobra.Command { if opts.kafkaID == "" { - svcContext, err := opts.serviceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/consumergroup/delete/delete.go b/pkg/cmd/kafka/consumergroup/delete/delete.go index e2fb981a4..bbced1997 100644 --- a/pkg/cmd/kafka/consumergroup/delete/delete.go +++ b/pkg/cmd/kafka/consumergroup/delete/delete.go @@ -53,22 +53,7 @@ func NewDeleteConsumerGroupCommand(f *factory.Factory) *cobra.Command { return runCmd(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/consumergroup/describe/describe.go b/pkg/cmd/kafka/consumergroup/describe/describe.go index de98a30b1..f561c3b89 100644 --- a/pkg/cmd/kafka/consumergroup/describe/describe.go +++ b/pkg/cmd/kafka/consumergroup/describe/describe.go @@ -70,22 +70,7 @@ func NewDescribeConsumerGroupCommand(f *factory.Factory) *cobra.Command { return runCmd(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/consumergroup/list/list.go b/pkg/cmd/kafka/consumergroup/list/list.go index c0b55de15..bee76e44a 100644 --- a/pkg/cmd/kafka/consumergroup/list/list.go +++ b/pkg/cmd/kafka/consumergroup/list/list.go @@ -78,22 +78,8 @@ func NewListConsumerGroupCommand(f *factory.Factory) *cobra.Command { } if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/consumergroup/resetoffset/reset_offset.go b/pkg/cmd/kafka/consumergroup/resetoffset/reset_offset.go index 95367b0b7..056b22ac0 100644 --- a/pkg/cmd/kafka/consumergroup/resetoffset/reset_offset.go +++ b/pkg/cmd/kafka/consumergroup/resetoffset/reset_offset.go @@ -77,22 +77,7 @@ func NewResetOffsetConsumerGroupCommand(f *factory.Factory) *cobra.Command { return runCmd(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/create/create.go b/pkg/cmd/kafka/create/create.go index 811f64242..38a24ff94 100644 --- a/pkg/cmd/kafka/create/create.go +++ b/pkg/cmd/kafka/create/create.go @@ -143,17 +143,7 @@ func runCreate(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -248,8 +238,8 @@ func runCreate(opts *options) error { if opts.autoUse { opts.Logger.Debug("Auto-use is set, updating the current instance") - svcConfig.KafkaID = response.GetId() - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.KafkaID = response.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err = opts.ServiceContext.Save(svcContext); err != nil { return fmt.Errorf("%v: %w", opts.localizer.MustLocalize("kafka.common.error.couldNotUseKafka"), err) diff --git a/pkg/cmd/kafka/delete/delete.go b/pkg/cmd/kafka/delete/delete.go index deca43d19..544d32c7c 100644 --- a/pkg/cmd/kafka/delete/delete.go +++ b/pkg/cmd/kafka/delete/delete.go @@ -63,22 +63,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { return runDelete(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } @@ -108,17 +93,7 @@ func runDelete(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -174,7 +149,7 @@ func runDelete(opts *options) error { opts.Logger.Info(opts.localizer.MustLocalize("kafka.delete.log.info.deleting", localize.NewEntry("Name", kafkaName))) - currentKafka := svcConfig.KafkaID + currentKafka := currCtx.KafkaID // this is not the current instance, our work here is done if currentKafka != response.GetId() { return nil @@ -182,8 +157,8 @@ func runDelete(opts *options) error { // the Kafka that was deleted is set as the user's current cluster // since it was deleted it should be removed from the context - svcConfig.KafkaID = "" - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.KafkaID = "" + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err := opts.ServiceContext.Save(svcContext); err != nil { return err diff --git a/pkg/cmd/kafka/describe/describe.go b/pkg/cmd/kafka/describe/describe.go index aea82d964..626862ac6 100644 --- a/pkg/cmd/kafka/describe/describe.go +++ b/pkg/cmd/kafka/describe/describe.go @@ -65,22 +65,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { return runDescribe(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/list/list.go b/pkg/cmd/kafka/list/list.go index 3115dd0a2..88cdae7a0 100644 --- a/pkg/cmd/kafka/list/list.go +++ b/pkg/cmd/kafka/list/list.go @@ -134,20 +134,13 @@ func runList(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } - kafkaInstance, _ := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) - - if kafkaInstance != nil { - rows = mapResponseItemsToRows(response.GetItems(), kafkaInstance.GetId()) + if currCtx.KafkaID != "" { + rows = mapResponseItemsToRows(response.GetItems(), currCtx.KafkaID) } else { rows = mapResponseItemsToRows(response.GetItems(), "-") } diff --git a/pkg/cmd/kafka/topic/create/create.go b/pkg/cmd/kafka/topic/create/create.go index 50cab8644..138df281c 100644 --- a/pkg/cmd/kafka/topic/create/create.go +++ b/pkg/cmd/kafka/topic/create/create.go @@ -92,22 +92,7 @@ func NewCreateTopicCommand(f *factory.Factory) *cobra.Command { if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/topic/delete/delete.go b/pkg/cmd/kafka/topic/delete/delete.go index 4b48240b7..79ec217b4 100644 --- a/pkg/cmd/kafka/topic/delete/delete.go +++ b/pkg/cmd/kafka/topic/delete/delete.go @@ -56,22 +56,7 @@ func NewDeleteTopicCommand(f *factory.Factory) *cobra.Command { if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/topic/describe/describe.go b/pkg/cmd/kafka/topic/describe/describe.go index 60628bf5c..f7240e4f7 100644 --- a/pkg/cmd/kafka/topic/describe/describe.go +++ b/pkg/cmd/kafka/topic/describe/describe.go @@ -57,22 +57,8 @@ func NewDescribeTopicCommand(f *factory.Factory) *cobra.Command { } if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/topic/list/list.go b/pkg/cmd/kafka/topic/list/list.go index fc91321a6..1c8ff6df9 100644 --- a/pkg/cmd/kafka/topic/list/list.go +++ b/pkg/cmd/kafka/topic/list/list.go @@ -87,22 +87,8 @@ func NewListTopicCommand(f *factory.Factory) *cobra.Command { } if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/topic/update/update.go b/pkg/cmd/kafka/topic/update/update.go index 08652d53b..67f5f91b5 100644 --- a/pkg/cmd/kafka/topic/update/update.go +++ b/pkg/cmd/kafka/topic/update/update.go @@ -132,22 +132,8 @@ func NewUpdateTopicCommand(f *factory.Factory) *cobra.Command { } if opts.kafkaID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/update/update.go b/pkg/cmd/kafka/update/update.go index 84f2e4d8f..4367cfd37 100644 --- a/pkg/cmd/kafka/update/update.go +++ b/pkg/cmd/kafka/update/update.go @@ -107,22 +107,7 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { return run(&opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - kafkaInstance, err := profileHandler.GetCurrentKafkaInstance(conn.API().KafkaMgmt()) + kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f) if err != nil { return err } diff --git a/pkg/cmd/kafka/use/use.go b/pkg/cmd/kafka/use/use.go index 0be442374..6119904ed 100644 --- a/pkg/cmd/kafka/use/use.go +++ b/pkg/cmd/kafka/use/use.go @@ -95,17 +95,7 @@ func runUse(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -131,8 +121,8 @@ func runUse(opts *options) error { } nameTmplEntry := localize.NewEntry("Name", res.GetName()) - svcConfig.KafkaID = res.GetId() - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.KafkaID = res.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err := opts.ServiceContext.Save(svcContext); err != nil { saveErrMsg := opts.localizer.MustLocalize("kafka.use.error.saveError", nameTmplEntry) diff --git a/pkg/cmd/registry/artifact/crud/create/create.go b/pkg/cmd/registry/artifact/crud/create/create.go index ce7d01947..8fd6a7adc 100644 --- a/pkg/cmd/registry/artifact/crud/create/create.go +++ b/pkg/cmd/registry/artifact/crud/create/create.go @@ -81,22 +81,7 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command { return runCreate(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/crud/delete/delete.go b/pkg/cmd/registry/artifact/crud/delete/delete.go index 2884ee93a..de62dfc9f 100644 --- a/pkg/cmd/registry/artifact/crud/delete/delete.go +++ b/pkg/cmd/registry/artifact/crud/delete/delete.go @@ -57,22 +57,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { return runDelete(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/crud/get/get.go b/pkg/cmd/registry/artifact/crud/get/get.go index f1683e300..af704cba1 100644 --- a/pkg/cmd/registry/artifact/crud/get/get.go +++ b/pkg/cmd/registry/artifact/crud/get/get.go @@ -61,22 +61,7 @@ func NewGetCommand(f *factory.Factory) *cobra.Command { return runGet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/crud/list/list.go b/pkg/cmd/registry/artifact/crud/list/list.go index 2ffd6ad28..1305a16f8 100644 --- a/pkg/cmd/registry/artifact/crud/list/list.go +++ b/pkg/cmd/registry/artifact/crud/list/list.go @@ -83,22 +83,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command { } if opts.registryID == "" { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/crud/update/update.go b/pkg/cmd/registry/artifact/crud/update/update.go index 5393f9659..5ce12497c 100644 --- a/pkg/cmd/registry/artifact/crud/update/update.go +++ b/pkg/cmd/registry/artifact/crud/update/update.go @@ -68,22 +68,7 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { return runUpdate(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/download/download.go b/pkg/cmd/registry/artifact/download/download.go index b9d5cc2b3..dfc095634 100644 --- a/pkg/cmd/registry/artifact/download/download.go +++ b/pkg/cmd/registry/artifact/download/download.go @@ -62,22 +62,7 @@ func NewDownloadCommand(f *factory.Factory) *cobra.Command { return runGet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/metadata/get.go b/pkg/cmd/registry/artifact/metadata/get.go index 1cb28ab46..1e866f428 100644 --- a/pkg/cmd/registry/artifact/metadata/get.go +++ b/pkg/cmd/registry/artifact/metadata/get.go @@ -61,22 +61,7 @@ func NewGetMetadataCommand(f *factory.Factory) *cobra.Command { return runGet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/metadata/set.go b/pkg/cmd/registry/artifact/metadata/set.go index fbca69166..5725be201 100644 --- a/pkg/cmd/registry/artifact/metadata/set.go +++ b/pkg/cmd/registry/artifact/metadata/set.go @@ -68,22 +68,7 @@ func NewSetMetadataCommand(f *factory.Factory) *cobra.Command { return runSet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/migrate/export.go b/pkg/cmd/registry/artifact/migrate/export.go index c72b24814..0f7ae1f34 100644 --- a/pkg/cmd/registry/artifact/migrate/export.go +++ b/pkg/cmd/registry/artifact/migrate/export.go @@ -50,22 +50,7 @@ func NewExportCommand(f *factory.Factory) *cobra.Command { return runExport(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/migrate/import.go b/pkg/cmd/registry/artifact/migrate/import.go index a94dcf94f..97dd92b0a 100644 --- a/pkg/cmd/registry/artifact/migrate/import.go +++ b/pkg/cmd/registry/artifact/migrate/import.go @@ -53,22 +53,7 @@ func NewImportCommand(f *factory.Factory) *cobra.Command { return runImport(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/role/add/add.go b/pkg/cmd/registry/artifact/role/add/add.go index 42de3af72..31ff66d9d 100644 --- a/pkg/cmd/registry/artifact/role/add/add.go +++ b/pkg/cmd/registry/artifact/role/add/add.go @@ -76,22 +76,7 @@ func NewAddCommand(f *factory.Factory) *cobra.Command { return runAdd(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/role/list/list.go b/pkg/cmd/registry/artifact/role/list/list.go index c966c58fa..0e8cac5e9 100644 --- a/pkg/cmd/registry/artifact/role/list/list.go +++ b/pkg/cmd/registry/artifact/role/list/list.go @@ -63,22 +63,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command { return runList(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/role/revoke/revoke.go b/pkg/cmd/registry/artifact/role/revoke/revoke.go index f97112b2e..dad45e8c8 100644 --- a/pkg/cmd/registry/artifact/role/revoke/revoke.go +++ b/pkg/cmd/registry/artifact/role/revoke/revoke.go @@ -64,22 +64,7 @@ func NewRevokeCommand(f *factory.Factory) *cobra.Command { return runRevoke(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/state/state.go b/pkg/cmd/registry/artifact/state/state.go index 779bb508a..47e93243a 100644 --- a/pkg/cmd/registry/artifact/state/state.go +++ b/pkg/cmd/registry/artifact/state/state.go @@ -63,22 +63,7 @@ func NewSetStateCommand(f *factory.Factory) *cobra.Command { return runSet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/artifact/versions/versions.go b/pkg/cmd/registry/artifact/versions/versions.go index 607cad00c..0690340e3 100644 --- a/pkg/cmd/registry/artifact/versions/versions.go +++ b/pkg/cmd/registry/artifact/versions/versions.go @@ -58,22 +58,7 @@ func NewVersionsCommand(f *factory.Factory) *cobra.Command { return runGet(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/create/create.go b/pkg/cmd/registry/create/create.go index 444e9535a..1712f5fdc 100644 --- a/pkg/cmd/registry/create/create.go +++ b/pkg/cmd/registry/create/create.go @@ -104,17 +104,7 @@ func runCreate(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -172,8 +162,8 @@ func runCreate(opts *options) error { if opts.autoUse { opts.Logger.Debug("Auto-use is set, updating the current instance") - svcConfig.ServiceRegistryID = response.GetId() - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.ServiceRegistryID = response.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err := opts.ServiceContext.Save(svcContext); err != nil { return fmt.Errorf("%v: %w", opts.localizer.MustLocalize("registry.cmd.create.error.couldNotUse"), err) diff --git a/pkg/cmd/registry/delete/delete.go b/pkg/cmd/registry/delete/delete.go index d7fa13b63..0072ce7a1 100644 --- a/pkg/cmd/registry/delete/delete.go +++ b/pkg/cmd/registry/delete/delete.go @@ -62,22 +62,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { return runDelete(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } @@ -102,17 +87,7 @@ func runDelete(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -169,7 +144,7 @@ func runDelete(opts *options) error { opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("registry.delete.log.info.deleteSuccess", localize.NewEntry("Name", registryName))) - currentContextRegistry := svcConfig.ServiceRegistryID + currentContextRegistry := currCtx.ServiceRegistryID // this is not the current cluster, our work here is done if currentContextRegistry != opts.id { return nil @@ -178,8 +153,8 @@ func runDelete(opts *options) error { // the service that was deleted is set as the user's current instance // since it was deleted it should be removed from the context - svcConfig.ServiceRegistryID = "" - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.ServiceRegistryID = "" + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err := opts.ServiceContext.Save(svcContext); err != nil { return err diff --git a/pkg/cmd/registry/describe/describe.go b/pkg/cmd/registry/describe/describe.go index 62241d85f..e24d045c7 100644 --- a/pkg/cmd/registry/describe/describe.go +++ b/pkg/cmd/registry/describe/describe.go @@ -60,22 +60,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { return runDescribe(opts) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/list/list.go b/pkg/cmd/registry/list/list.go index 32d41d8e0..b20aeccb0 100644 --- a/pkg/cmd/registry/list/list.go +++ b/pkg/cmd/registry/list/list.go @@ -122,20 +122,13 @@ func runList(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } - registryInstance, _ := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) - - if registryInstance != nil { - rows = mapResponseItemsToRows(&response.Items, registryInstance.GetId()) + if currCtx.ServiceRegistryID != "" { + rows = mapResponseItemsToRows(&response.Items, currCtx.ServiceRegistryID) } else { rows = mapResponseItemsToRows(&response.Items, "-") } diff --git a/pkg/cmd/registry/rule/describe/describe.go b/pkg/cmd/registry/rule/describe/describe.go index 9c5148c58..9d81522f6 100644 --- a/pkg/cmd/registry/rule/describe/describe.go +++ b/pkg/cmd/registry/rule/describe/describe.go @@ -64,22 +64,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { return err } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/rule/disable/disable.go b/pkg/cmd/registry/rule/disable/disable.go index 8183960ed..295c073c3 100644 --- a/pkg/cmd/registry/rule/disable/disable.go +++ b/pkg/cmd/registry/rule/disable/disable.go @@ -61,30 +61,7 @@ func NewDisableCommand(f *factory.Factory) *cobra.Command { return flagutil.RequiredWhenNonInteractiveError("yes") } - validator := rulecmdutil.Validator{ - Localizer: opts.localizer, - } - - if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" { - return err - } - - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/rule/enable/enable.go b/pkg/cmd/registry/rule/enable/enable.go index be57dfb6c..1c3ea7e1e 100644 --- a/pkg/cmd/registry/rule/enable/enable.go +++ b/pkg/cmd/registry/rule/enable/enable.go @@ -99,22 +99,7 @@ func NewEnableCommand(f *factory.Factory) *cobra.Command { ) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/rule/list/list.go b/pkg/cmd/registry/rule/list/list.go index 5f3fbde2f..7682ef3ed 100644 --- a/pkg/cmd/registry/rule/list/list.go +++ b/pkg/cmd/registry/rule/list/list.go @@ -72,22 +72,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) (err error) { - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/rule/update/update.go b/pkg/cmd/registry/rule/update/update.go index a33cdc6b9..3484ead11 100644 --- a/pkg/cmd/registry/rule/update/update.go +++ b/pkg/cmd/registry/rule/update/update.go @@ -74,22 +74,7 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { ) } - svcContext, err := opts.ServiceContext.Load() - if err != nil { - return err - } - - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) - if err != nil { - return err - } - - registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) if err != nil { return err } diff --git a/pkg/cmd/registry/use/use.go b/pkg/cmd/registry/use/use.go index f396542a4..614daefe2 100644 --- a/pkg/cmd/registry/use/use.go +++ b/pkg/cmd/registry/use/use.go @@ -90,17 +90,7 @@ func runUse(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer) if err != nil { return err } @@ -126,8 +116,8 @@ func runUse(opts *options) error { } nameTmplEntry := localize.NewEntry("Name", registry.GetName()) - svcConfig.ServiceRegistryID = registry.GetId() - svcContext.Contexts[svcContext.CurrentContext] = *svcConfig + currCtx.ServiceRegistryID = registry.GetId() + svcContext.Contexts[svcContext.CurrentContext] = *currCtx if err := opts.ServiceContext.Save(svcContext); err != nil { saveErrMsg := opts.localizer.MustLocalize("registry.use.error.saveError", nameTmplEntry) diff --git a/pkg/cmd/status/status.go b/pkg/cmd/status/status.go index 0effcf3aa..7bc0d7461 100644 --- a/pkg/cmd/status/status.go +++ b/pkg/cmd/status/status.go @@ -96,23 +96,18 @@ func runStatus(opts *options) error { return err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: opts.localizer, - } - var svcConfig *servicecontext.ServiceConfig if opts.name == "" { - opts.name, err = profileHandler.GetCurrentContext() + svcConfig, err = contextutil.GetCurrentContext(svcContext, opts.localizer) + if err != nil { + return err + } + } else { + svcConfig, err = contextutil.GetContext(svcContext, opts.localizer, opts.name) if err != nil { return err } - } - - svcConfig, err = profileHandler.GetContext(opts.name) - if err != nil { - return err } statusClient := newStatusClient(&clientConfig{ diff --git a/pkg/core/servicecontext/file.go b/pkg/core/servicecontext/file.go index b99252d4a..e94521188 100644 --- a/pkg/core/servicecontext/file.go +++ b/pkg/core/servicecontext/file.go @@ -42,12 +42,12 @@ func (c *File) Load() (*Context, error) { if err != nil { return nil, fmt.Errorf(errorFormat, "unable to read context file", err) } - var cfg Context - err = json.Unmarshal(data, &cfg) + var ctx Context + err = json.Unmarshal(data, &ctx) if err != nil { return nil, fmt.Errorf(errorFormat, "unable to parse contexts", err) } - return &cfg, nil + return &ctx, nil } // Save saves the given profiles to the context file. diff --git a/pkg/shared/cluster/services/kafka.go b/pkg/shared/cluster/services/kafka.go index e13815a8d..b717894be 100644 --- a/pkg/shared/cluster/services/kafka.go +++ b/pkg/shared/cluster/services/kafka.go @@ -25,17 +25,7 @@ func (s KafkaService) BuildServiceDetails(serviceName string, namespace string, return nil, err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: cliOpts.Localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return nil, err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, cliOpts.Localizer) if err != nil { return nil, err } @@ -44,7 +34,7 @@ func (s KafkaService) BuildServiceDetails(serviceName string, namespace string, var serviceId string if serviceName == "" { - if svcConfig.KafkaID == "" || ignoreContext { + if currCtx.KafkaID == "" || ignoreContext { // nolint selectedService, err := kafkautil.InteractiveSelect(cliOpts.Context, cliOpts.Connection, cliOpts.Logger, cliOpts.Localizer) if err != nil { @@ -56,7 +46,7 @@ func (s KafkaService) BuildServiceDetails(serviceName string, namespace string, serviceId = selectedService.GetId() serviceName = selectedService.GetName() } else { - serviceId = svcConfig.KafkaID + serviceId = currCtx.KafkaID selectedService, _, err := kafkautil.GetKafkaByID(cliOpts.Context, api.KafkaMgmt(), serviceId) if err != nil { return nil, err diff --git a/pkg/shared/cluster/services/service-registry.go b/pkg/shared/cluster/services/service-registry.go index aaf157d52..274cd7595 100644 --- a/pkg/shared/cluster/services/service-registry.go +++ b/pkg/shared/cluster/services/service-registry.go @@ -25,17 +25,7 @@ func (s RegistryService) BuildServiceDetails(serviceName string, namespace strin return nil, err } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: cliOpts.Localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return nil, err - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, cliOpts.Localizer) if err != nil { return nil, err } @@ -44,7 +34,7 @@ func (s RegistryService) BuildServiceDetails(serviceName string, namespace strin var serviceId string if serviceName == "" { - if svcConfig.ServiceRegistryID == "" || ignoreContext { + if currCtx.ServiceRegistryID == "" || ignoreContext { // nolint selectedService, err := serviceregistryutil.InteractiveSelect(cliOpts.Context, cliOpts.Connection, cliOpts.Logger) if err != nil { @@ -56,7 +46,7 @@ func (s RegistryService) BuildServiceDetails(serviceName string, namespace strin serviceId = selectedService.GetId() serviceName = selectedService.GetName() } else { - serviceId = svcConfig.ServiceRegistryID + serviceId = currCtx.ServiceRegistryID selectedService, _, err := serviceregistryutil.GetServiceRegistryByID( cliOpts.Context, api.ServiceRegistryMgmt(), serviceId) if err != nil { diff --git a/pkg/shared/contextutil/util.go b/pkg/shared/contextutil/util.go index 47872bb48..33b7f9c17 100644 --- a/pkg/shared/contextutil/util.go +++ b/pkg/shared/contextutil/util.go @@ -5,86 +5,109 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/localize" "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" + srsmgmtv1errors "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/error" + + registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client" + kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" kafkamgmtv1errors "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/error" - registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client" - srsmgmtv1errors "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/error" ) -// ContextHandler is a type with methods to obtain service data from context -type ContextHandler struct { - Context *servicecontext.Context - Localizer localize.Localizer +// GetContext returns the services associated with the context +func GetContext(svcContext *servicecontext.Context, localizer localize.Localizer, ctxName string) (*servicecontext.ServiceConfig, error) { + + ctx, ok := svcContext.Contexts[ctxName] + if !ok { + return nil, localizer.MustLocalizeError("context.common.error.context.notFound", localize.NewEntry("Name", svcContext.CurrentContext)) + } + + return &ctx, nil + +} + +// GetCurrentContext returns the name of the currently selected context +func GetCurrentContext(svcContext *servicecontext.Context, localizer localize.Localizer) (*servicecontext.ServiceConfig, error) { + + if svcContext.CurrentContext == "" { + return nil, localizer.MustLocalizeError("context.common.error.notSet") + } + + currCtx, ok := svcContext.Contexts[svcContext.CurrentContext] + if !ok { + return nil, localizer.MustLocalizeError("context.common.error.context.notFound", localize.NewEntry("Name", svcContext.CurrentContext)) + } + + return &currCtx, nil } // GetCurrentKafkaInstance returns the Kafka instance set in the currently selected context -func (c *ContextHandler) GetCurrentKafkaInstance(api kafkamgmtclient.DefaultApi) (*kafkamgmtclient.KafkaRequest, error) { +func GetCurrentKafkaInstance(f *factory.Factory) (*kafkamgmtclient.KafkaRequest, error) { - currentCtx, err := c.GetCurrentContext() + svcContext, err := f.ServiceContext.Load() if err != nil { return nil, err } - s, err := c.GetContext(currentCtx) + conn, err := f.Connection(connection.DefaultConfigRequireMasAuth) if err != nil { return nil, err } - if s.KafkaID == "" { - return nil, c.Localizer.MustLocalizeError("context.common.error.noKafkaID") + if svcContext.CurrentContext == "" { + return nil, f.Localizer.MustLocalizeError("context.common.error.notSet") } - kafkaInstance, _, err := api.GetKafkaById(context.Background(), s.KafkaID).Execute() + currCtx, ok := svcContext.Contexts[svcContext.CurrentContext] + if !ok { + return nil, f.Localizer.MustLocalizeError("context.common.error.context.notFound", localize.NewEntry("Name", svcContext.CurrentContext)) + } + + if currCtx.KafkaID == "" { + return nil, f.Localizer.MustLocalizeError("context.common.error.noKafkaID") + } + + kafkaInstance, _, err := conn.API().KafkaMgmt().GetKafkaById(context.Background(), currCtx.KafkaID).Execute() if kafkamgmtv1errors.IsAPIError(err, kafkamgmtv1errors.ERROR_7) { - return nil, c.Localizer.MustLocalizeError("context.common.error.kafka.notFound") + return nil, f.Localizer.MustLocalizeError("context.common.error.kafka.notFound") } return &kafkaInstance, err + } // GetCurrentRegistryInstance returns the Service Registry instance set in the currently selected context -func (c *ContextHandler) GetCurrentRegistryInstance(api registrymgmtclient.RegistriesApi) (*registrymgmtclient.Registry, error) { +func GetCurrentRegistryInstance(f *factory.Factory) (*registrymgmtclient.Registry, error) { - currentCtx, err := c.GetCurrentContext() + svcContext, err := f.ServiceContext.Load() if err != nil { return nil, err } - s, err := c.GetContext(currentCtx) + conn, err := f.Connection(connection.DefaultConfigRequireMasAuth) if err != nil { return nil, err } - if s.ServiceRegistryID == "" { - return nil, c.Localizer.MustLocalizeError("context.common.error.noRegistryID") + if svcContext.CurrentContext == "" { + return nil, f.Localizer.MustLocalizeError("context.common.error.notSet") } - registryInstance, _, err := api.GetRegistry(context.Background(), s.ServiceRegistryID).Execute() - - if srsmgmtv1errors.IsAPIError(err, srsmgmtv1errors.ERROR_2) { - return nil, c.Localizer.MustLocalizeError("context.common.error.registry.notFound") - } - - return ®istryInstance, nil -} - -// GetContext returns the services associated with the context -func (c *ContextHandler) GetContext(ctxName string) (*servicecontext.ServiceConfig, error) { - - currCtx, ok := c.Context.Contexts[ctxName] + currCtx, ok := svcContext.Contexts[svcContext.CurrentContext] if !ok { - return nil, c.Localizer.MustLocalizeError("context.common.error.context.notFound", localize.NewEntry("Name", ctxName)) + return nil, f.Localizer.MustLocalizeError("context.common.error.context.notFound", localize.NewEntry("Name", svcContext.CurrentContext)) } - return &currCtx, nil -} - -// GetCurrentContext returns the name of the currently selected context -func (c *ContextHandler) GetCurrentContext() (string, error) { + if currCtx.ServiceRegistryID == "" { + return nil, f.Localizer.MustLocalizeError("context.common.error.noRegistryID") + } - if c.Context.CurrentContext == "" { - return "", c.Localizer.MustLocalizeError("context.common.error.notSet") + registryInstance, _, err := conn.API().ServiceRegistryMgmt().GetRegistry(context.Background(), currCtx.ServiceRegistryID).Execute() + if srsmgmtv1errors.IsAPIError(err, srsmgmtv1errors.ERROR_2) { + return nil, f.Localizer.MustLocalizeError("context.common.error.registry.notFound") } - return c.Context.CurrentContext, nil + return ®istryInstance, err + } diff --git a/pkg/shared/kafkautil/util.go b/pkg/shared/kafkautil/util.go index 5f082631c..5183fc378 100644 --- a/pkg/shared/kafkautil/util.go +++ b/pkg/shared/kafkautil/util.go @@ -153,22 +153,12 @@ func FilterValidTopicNameArgs(f *factory.Factory, toComplete string) (validNames return validNames, directive } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: f.Localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return validNames, directive - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer) if err != nil { return validNames, directive } - instanceID := svcConfig.KafkaID + instanceID := currCtx.KafkaID if instanceID == "" { return validNames, directive } @@ -210,22 +200,12 @@ func FilterValidConsumerGroupIDs(f *factory.Factory, toComplete string) (validID return validIDs, directive } - profileHandler := &contextutil.ContextHandler{ - Context: svcContext, - Localizer: f.Localizer, - } - - currCtx, err := profileHandler.GetCurrentContext() - if err != nil { - return validIDs, directive - } - - svcConfig, err := profileHandler.GetContext(currCtx) + currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer) if err != nil { return validIDs, directive } - instanceID := svcConfig.KafkaID + instanceID := currCtx.KafkaID if instanceID == "" { return validIDs, directive }