Skip to content

Commit

Permalink
refactor(context): remove context handler struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 authored and wtrocki committed Apr 11, 2022
1 parent f733fb4 commit bb51d04
Show file tree
Hide file tree
Showing 55 changed files with 174 additions and 870 deletions.
7 changes: 4 additions & 3 deletions pkg/cmd/context/contextcmdutil/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/cmd/context/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/cmd/context/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/cmd/context/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions pkg/cmd/kafka/acl/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 10 additions & 24 deletions pkg/cmd/kafka/acl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand Down Expand Up @@ -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
}
34 changes: 10 additions & 24 deletions pkg/cmd/kafka/acl/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand Down Expand Up @@ -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
}
17 changes: 1 addition & 16 deletions pkg/cmd/kafka/acl/grant/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/cmd/kafka/acl/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/cmd/kafka/consumergroup/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/cmd/kafka/consumergroup/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions pkg/cmd/kafka/consumergroup/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/cmd/kafka/consumergroup/resetoffset/reset_offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 3 additions & 13 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit bb51d04

Please sign in to comment.