Skip to content

Commit c9e13e4

Browse files
rkpattnaik780wtrocki
authored andcommitted
feat(context): add command for context creation
1 parent 663a43b commit c9e13e4

File tree

13 files changed

+257
-47
lines changed

13 files changed

+257
-47
lines changed

docs/commands/rhoas_context.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_context_create.md

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_context_status.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/context/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"github.com/redhat-developer/app-services-cli/pkg/cmd/context/create"
45
"github.com/redhat-developer/app-services-cli/pkg/cmd/context/list"
56
"github.com/redhat-developer/app-services-cli/pkg/cmd/context/status"
67
"github.com/redhat-developer/app-services-cli/pkg/cmd/context/use"
@@ -21,6 +22,7 @@ func NewContextCmd(f *factory.Factory) *cobra.Command {
2122
use.NewUseCommand(f),
2223
status.NewStatusCommand(f),
2324
list.NewListCommand(f),
25+
create.NewCreateCommand(f),
2426
)
2527
return cmd
2628
}

pkg/cmd/context/create/create.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package create
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
8+
"github.com/redhat-developer/app-services-cli/pkg/core/config"
9+
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
10+
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
11+
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
12+
"github.com/redhat-developer/app-services-cli/pkg/core/profile"
13+
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
14+
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
type options struct {
19+
IO *iostreams.IOStreams
20+
Config config.IConfig
21+
Logger logging.Logger
22+
Connection factory.ConnectionFunc
23+
localizer localize.Localizer
24+
Context context.Context
25+
Profiles profile.IContext
26+
27+
name string
28+
kafkaID string
29+
registryID string
30+
}
31+
32+
func NewCreateCommand(f *factory.Factory) *cobra.Command {
33+
34+
opts := &options{
35+
Config: f.Config,
36+
Connection: f.Connection,
37+
IO: f.IOStreams,
38+
Logger: f.Logger,
39+
localizer: f.Localizer,
40+
Profiles: f.Profile,
41+
}
42+
43+
cmd := &cobra.Command{
44+
Use: "create",
45+
Short: f.Localizer.MustLocalize("context.create.cmd.shortDescription"),
46+
Long: f.Localizer.MustLocalize("context.create.cmd.longDescription"),
47+
Example: f.Localizer.MustLocalize("context.create.cmd.example"),
48+
Args: cobra.NoArgs,
49+
RunE: func(cmd *cobra.Command, args []string) error {
50+
return runCreate(opts)
51+
},
52+
}
53+
54+
flags := flagutil.NewFlagSet(cmd, opts.localizer)
55+
56+
flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name"))
57+
flags.StringVar(&opts.kafkaID, "kafka-id", "", opts.localizer.MustLocalize("context.common.flag.name"))
58+
flags.StringVar(&opts.registryID, "registry-id", "", opts.localizer.MustLocalize("context.common.flag.name"))
59+
60+
return cmd
61+
62+
}
63+
64+
func runCreate(opts *options) error {
65+
66+
context, err := opts.Profiles.Load()
67+
if err != nil {
68+
return err
69+
}
70+
71+
profileHandler := &profileutil.ContextHandler{
72+
Context: context,
73+
Localizer: opts.localizer,
74+
}
75+
76+
profiles := context.Contexts
77+
78+
currentCtx, _ := profileHandler.GetContext(context.CurrentContext)
79+
if currentCtx != nil {
80+
return errors.New("context already exists")
81+
}
82+
83+
services := profile.ServiceConfig{
84+
KafkaID: opts.kafkaID,
85+
ServiceRegistryID: opts.registryID,
86+
}
87+
88+
profiles[opts.name] = services
89+
90+
context.Contexts = profiles
91+
92+
err = opts.Profiles.Save(context)
93+
if err != nil {
94+
return err
95+
}
96+
97+
return nil
98+
}

pkg/cmd/context/status/status.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type options struct {
2525
Context context.Context
2626
Profiles profile.IContext
2727

28-
name string
28+
name string
29+
outputFormat string
2930
}
3031

3132
func NewStatusCommand(f *factory.Factory) *cobra.Command {
@@ -53,6 +54,7 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command {
5354
flags := flagutil.NewFlagSet(cmd, opts.localizer)
5455

5556
flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name"))
57+
flags.AddOutput(&opts.outputFormat)
5658

5759
return cmd
5860
}
@@ -85,7 +87,7 @@ func runStatus(opts *options) error {
8587
}
8688
}
8789

88-
err = dump.Formatted(stdout, "yml", currentCtx)
90+
err = dump.Formatted(stdout, opts.outputFormat, currentCtx)
8991
if err != nil {
9092
return err
9193
}

pkg/cmd/context/use/use.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
1010
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
1111
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
12+
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
1213
"github.com/spf13/cobra"
1314

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

66+
profileHandler := &profileutil.ContextHandler{
67+
Context: context,
68+
Localizer: opts.localizer,
69+
}
70+
71+
_, err = profileHandler.GetContext(context.CurrentContext)
72+
if err != nil {
73+
return err
74+
}
75+
6576
context.CurrentContext = opts.name
6677

6778
err = opts.Profiles.Save(context)

pkg/cmd/registry/rule/describe/describe.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ import (
66

77
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
88
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
9-
"github.com/redhat-developer/app-services-cli/pkg/core/config"
109
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
1110
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
1211
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
1312
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
13+
"github.com/redhat-developer/app-services-cli/pkg/core/profile"
1414
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
1515
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
16+
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
1617
"github.com/spf13/cobra"
1718

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

2122
type options struct {
2223
IO *iostreams.IOStreams
23-
Config config.IConfig
2424
Connection factory.ConnectionFunc
2525
Logger logging.Logger
2626
localizer localize.Localizer
2727
Context context.Context
28+
Profiles profile.IContext
2829

2930
ruleType string
3031

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

4041
opts := &options{
4142
IO: f.IOStreams,
42-
Config: f.Config,
4343
Connection: f.Connection,
4444
Logger: f.Logger,
4545
localizer: f.Localizer,
4646
Context: f.Context,
47+
Profiles: f.Profile,
4748
}
4849

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

66-
cfg, err := opts.Config.Load()
67+
context, err := opts.Profiles.Load()
6768
if err != nil {
6869
return err
6970
}
7071

71-
instanceID, ok := cfg.GetServiceRegistryIdOk()
72-
if !ok {
73-
return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected")
72+
profileHandler := &profileutil.ContextHandler{
73+
Context: context,
74+
Localizer: opts.localizer,
75+
}
76+
77+
conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
78+
if err != nil {
79+
return err
80+
}
81+
82+
registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt())
83+
if err != nil {
84+
return err
7485
}
7586

76-
opts.registryID = instanceID
87+
opts.registryID = registryInstance.GetId()
7788

7889
return runDescribe(opts)
7990
},

pkg/cmd/registry/rule/disable/disable.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@ import (
88
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
99
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
1010
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
11-
"github.com/redhat-developer/app-services-cli/pkg/core/config"
1211
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
1312
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
1413
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
1514
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
15+
"github.com/redhat-developer/app-services-cli/pkg/core/profile"
1616
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
1717
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
18+
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil"
1819
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
1920
"github.com/spf13/cobra"
2021
)
2122

2223
type options struct {
2324
IO *iostreams.IOStreams
24-
Config config.IConfig
2525
Connection factory.ConnectionFunc
2626
Logger logging.Logger
2727
localizer localize.Localizer
2828
Context context.Context
29+
Profiles profile.IContext
2930

3031
ruleType string
3132

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

4243
opts := &options{
4344
IO: f.IOStreams,
44-
Config: f.Config,
4545
Connection: f.Connection,
4646
Logger: f.Logger,
4747
localizer: f.Localizer,
4848
Context: f.Context,
49+
Profiles: f.Profile,
4950
}
5051

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

67-
cfg, err := opts.Config.Load()
68+
if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" {
69+
return err
70+
}
71+
72+
context, err := opts.Profiles.Load()
6873
if err != nil {
6974
return err
7075
}
7176

72-
if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" {
77+
profileHandler := &profileutil.ContextHandler{
78+
Context: context,
79+
Localizer: opts.localizer,
80+
}
81+
82+
conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
83+
if err != nil {
7384
return err
7485
}
7586

76-
instanceID, ok := cfg.GetServiceRegistryIdOk()
77-
if !ok {
78-
return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected")
87+
registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt())
88+
if err != nil {
89+
return err
7990
}
8091

81-
opts.registryID = instanceID
92+
opts.registryID = registryInstance.GetId()
8293

8394
return runDisable(opts)
8495
},

0 commit comments

Comments
 (0)