Skip to content

Commit

Permalink
feat: alias status and use commands to context cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 authored and wtrocki committed Apr 11, 2022
1 parent 6fd2b94 commit bfdd4ac
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 209 deletions.
6 changes: 2 additions & 4 deletions cmd/rhoas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/cmd/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
35 changes: 35 additions & 0 deletions pkg/cmd/context/contextcmdutil/flagset.go
Original file line number Diff line number Diff line change
@@ -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"),
)

}
6 changes: 3 additions & 3 deletions pkg/cmd/context/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 14 additions & 12 deletions pkg/cmd/context/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/context/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 0 additions & 94 deletions pkg/cmd/context/status/status.go

This file was deleted.

7 changes: 4 additions & 3 deletions pkg/cmd/context/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/kafka/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit bfdd4ac

Please sign in to comment.