Skip to content

Commit

Permalink
feat(context): add command for context creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 authored and wtrocki committed Apr 11, 2022
1 parent 663a43b commit c9e13e4
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 47 deletions.
1 change: 1 addition & 0 deletions docs/commands/rhoas_context.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions docs/commands/rhoas_context_create.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/commands/rhoas_context_status.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/cmd/context/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package context

import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/context/create"
"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"
Expand All @@ -21,6 +22,7 @@ func NewContextCmd(f *factory.Factory) *cobra.Command {
use.NewUseCommand(f),
status.NewStatusCommand(f),
list.NewListCommand(f),
create.NewCreateCommand(f),
)
return cmd
}
98 changes: 98 additions & 0 deletions pkg/cmd/context/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package create

import (
"context"
"errors"

"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/config"
"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/profile"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
"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
Profiles profile.IContext

name string
kafkaID string
registryID string
}

func NewCreateCommand(f *factory.Factory) *cobra.Command {

opts := &options{
Config: f.Config,
Connection: f.Connection,
IO: f.IOStreams,
Logger: f.Logger,
localizer: f.Localizer,
Profiles: f.Profile,
}

cmd := &cobra.Command{
Use: "create",
Short: f.Localizer.MustLocalize("context.create.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("context.create.cmd.longDescription"),
Example: f.Localizer.MustLocalize("context.create.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runCreate(opts)
},
}

flags := flagutil.NewFlagSet(cmd, opts.localizer)

flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name"))
flags.StringVar(&opts.kafkaID, "kafka-id", "", opts.localizer.MustLocalize("context.common.flag.name"))
flags.StringVar(&opts.registryID, "registry-id", "", opts.localizer.MustLocalize("context.common.flag.name"))

return cmd

}

func runCreate(opts *options) error {

context, err := opts.Profiles.Load()
if err != nil {
return err
}

profileHandler := &profileutil.ContextHandler{
Context: context,
Localizer: opts.localizer,
}

profiles := context.Contexts

currentCtx, _ := profileHandler.GetContext(context.CurrentContext)
if currentCtx != nil {
return errors.New("context already exists")
}

services := profile.ServiceConfig{
KafkaID: opts.kafkaID,
ServiceRegistryID: opts.registryID,
}

profiles[opts.name] = services

context.Contexts = profiles

err = opts.Profiles.Save(context)
if err != nil {
return err
}

return nil
}
6 changes: 4 additions & 2 deletions pkg/cmd/context/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type options struct {
Context context.Context
Profiles profile.IContext

name string
name string
outputFormat string
}

func NewStatusCommand(f *factory.Factory) *cobra.Command {
Expand Down Expand Up @@ -53,6 +54,7 @@ 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"))
flags.AddOutput(&opts.outputFormat)

return cmd
}
Expand Down Expand Up @@ -85,7 +87,7 @@ func runStatus(opts *options) error {
}
}

err = dump.Formatted(stdout, "yml", currentCtx)
err = dump.Formatted(stdout, opts.outputFormat, currentCtx)
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/cmd/context/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
"github.com/spf13/cobra"

"github.com/redhat-developer/app-services-cli/pkg/core/profile"
Expand Down Expand Up @@ -62,6 +63,16 @@ func runUse(opts *options) error {
return err
}

profileHandler := &profileutil.ContextHandler{
Context: context,
Localizer: opts.localizer,
}

_, err = profileHandler.GetContext(context.CurrentContext)
if err != nil {
return err
}

context.CurrentContext = opts.name

err = opts.Profiles.Save(context)
Expand Down
27 changes: 19 additions & 8 deletions pkg/cmd/registry/rule/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import (

"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/config"
"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/profile"
"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/spf13/cobra"

registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
)

type options struct {
IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
Profiles profile.IContext

ruleType string

Expand All @@ -39,11 +40,11 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {

opts := &options{
IO: f.IOStreams,
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
localizer: f.Localizer,
Context: f.Context,
Profiles: f.Profile,
}

cmd := &cobra.Command{
Expand All @@ -63,17 +64,27 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
return err
}

cfg, err := opts.Config.Load()
context, err := opts.Profiles.Load()
if err != nil {
return err
}

instanceID, ok := cfg.GetServiceRegistryIdOk()
if !ok {
return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected")
profileHandler := &profileutil.ContextHandler{
Context: context,
Localizer: opts.localizer,
}

conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
if err != nil {
return err
}

registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt())
if err != nil {
return err
}

opts.registryID = instanceID
opts.registryID = registryInstance.GetId()

return runDescribe(opts)
},
Expand Down
29 changes: 20 additions & 9 deletions pkg/cmd/registry/rule/disable/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/config"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
"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/profile"
"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"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
"github.com/spf13/cobra"
)

type options struct {
IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
Profiles profile.IContext

ruleType string

Expand All @@ -41,11 +42,11 @@ func NewDisableCommand(f *factory.Factory) *cobra.Command {

opts := &options{
IO: f.IOStreams,
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
localizer: f.Localizer,
Context: f.Context,
Profiles: f.Profile,
}

cmd := &cobra.Command{
Expand All @@ -64,21 +65,31 @@ func NewDisableCommand(f *factory.Factory) *cobra.Command {
Localizer: opts.localizer,
}

cfg, err := opts.Config.Load()
if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" {
return err
}

context, err := opts.Profiles.Load()
if err != nil {
return err
}

if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" {
profileHandler := &profileutil.ContextHandler{
Context: context,
Localizer: opts.localizer,
}

conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
if err != nil {
return err
}

instanceID, ok := cfg.GetServiceRegistryIdOk()
if !ok {
return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected")
registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt())
if err != nil {
return err
}

opts.registryID = instanceID
opts.registryID = registryInstance.GetId()

return runDisable(opts)
},
Expand Down
Loading

0 comments on commit c9e13e4

Please sign in to comment.