Skip to content

Commit d46ab5f

Browse files
committed
fix: refactor status command
1 parent 08a552b commit d46ab5f

File tree

5 files changed

+73
-132
lines changed

5 files changed

+73
-132
lines changed

pkg/cmd/status/status.go

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package status
22

33
import (
4-
"context"
5-
64
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
7-
"github.com/redhat-developer/app-services-cli/pkg/core/config"
85
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
9-
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
106
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
11-
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
127
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
13-
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
148
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
159
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
1610
"github.com/redhat-developer/app-services-cli/pkg/shared/servicespec"
@@ -19,13 +13,7 @@ import (
1913
)
2014

2115
type options struct {
22-
IO *iostreams.IOStreams
23-
Config config.IConfig
24-
Logger logging.Logger
25-
Connection factory.ConnectionFunc
26-
localizer localize.Localizer
27-
Context context.Context
28-
ServiceContext servicecontext.IContext
16+
f *factory.Factory
2917

3018
outputFormat string
3119
name string
@@ -34,32 +22,27 @@ type options struct {
3422

3523
func NewStatusCommand(f *factory.Factory) *cobra.Command {
3624
opts := &options{
37-
IO: f.IOStreams,
38-
Config: f.Config,
39-
Connection: f.Connection,
40-
Logger: f.Logger,
41-
services: servicespec.AllServiceLabels,
42-
localizer: f.Localizer,
43-
Context: f.Context,
44-
ServiceContext: f.ServiceContext,
25+
f: f,
4526
}
4627

4728
cmd := &cobra.Command{
4829
Use: "status [args]",
49-
Short: opts.localizer.MustLocalize("status.cmd.shortDescription"),
50-
Long: opts.localizer.MustLocalize("status.cmd.longDescription"),
51-
Example: opts.localizer.MustLocalize("status.cmd.example"),
30+
Short: f.Localizer.MustLocalize("status.cmd.shortDescription"),
31+
Long: f.Localizer.MustLocalize("status.cmd.longDescription"),
32+
Example: f.Localizer.MustLocalize("status.cmd.example"),
5233
ValidArgs: servicespec.AllServiceLabels,
5334
Args: cobra.RangeArgs(0, len(servicespec.AllServiceLabels)),
5435
RunE: func(cmd *cobra.Command, args []string) error {
5536
if len(args) > 0 {
5637
for _, s := range args {
5738
if !flagutil.IsValidInput(s, servicespec.AllServiceLabels...) {
58-
return opts.localizer.MustLocalizeError("status.error.args.error.unknownServiceError", localize.NewEntry("ServiceName", s))
39+
return f.Localizer.MustLocalizeError("status.error.args.error.unknownServiceError", localize.NewEntry("ServiceName", s))
5940
}
6041
}
6142

6243
opts.services = args
44+
} else {
45+
opts.services = servicespec.AllServiceLabels
6346
}
6447

6548
validOutputFormats := flagutil.ValidOutputFormats
@@ -71,9 +54,9 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command {
7154
},
7255
}
7356

74-
flags := flagutil.NewFlagSet(cmd, opts.localizer)
57+
flags := flagutil.NewFlagSet(cmd, f.Localizer)
7558

76-
flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name"))
59+
flags.StringVar(&opts.name, "name", "", f.Localizer.MustLocalize("context.common.flag.name"))
7760
flags.AddOutput(&opts.outputFormat)
7861

7962
flagutil.EnableOutputFlagCompletion(cmd)
@@ -82,55 +65,50 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command {
8265
}
8366

8467
func runStatus(opts *options) error {
85-
conn, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
86-
if err != nil {
87-
return err
88-
}
68+
factory := opts.f
8969

9070
if len(opts.services) > 0 {
91-
opts.Logger.Debug(opts.localizer.MustLocalize("status.log.debug.requestingStatusOfServices"), opts.services)
71+
opts.f.Logger.Debug(factory.Localizer.MustLocalize("status.log.debug.requestingStatusOfServices"), opts.services)
9272
}
9373

94-
svcContext, err := opts.ServiceContext.Load()
74+
svcContext, err := factory.ServiceContext.Load()
9575
if err != nil {
9676
return err
9777
}
9878

9979
var svcConfig *servicecontext.ServiceConfig
10080

10181
if opts.name == "" {
102-
svcConfig, err = contextutil.GetCurrentContext(svcContext, opts.localizer)
82+
svcConfig, err = contextutil.GetCurrentContext(svcContext, opts.f.Localizer)
10383
if err != nil {
10484
return err
10585
}
10686
} else {
107-
svcConfig, err = contextutil.GetContext(svcContext, opts.localizer, opts.name)
87+
svcConfig, err = contextutil.GetContext(svcContext, opts.f.Localizer, opts.name)
10888
if err != nil {
10989
return err
11090
}
11191
}
11292

11393
statusClient := newStatusClient(&clientConfig{
114-
config: opts.Config,
115-
connection: conn,
116-
Logger: opts.Logger,
117-
localizer: opts.localizer,
118-
context: opts.Context,
94+
f: factory,
11995
serviceConfig: svcConfig,
12096
})
12197

122-
status, ok, err := statusClient.BuildStatus(opts.name, opts.services)
98+
status, err := statusClient.BuildStatus(opts.services)
12399
if err != nil {
124100
return err
125101
}
102+
status.Name = svcContext.CurrentContext
103+
status.Location, _ = factory.ServiceContext.Location()
126104

127-
if !ok {
128-
opts.Logger.Info("")
129-
opts.Logger.Info(opts.localizer.MustLocalize("status.log.info.noStatusesAreUsed"))
105+
if !status.hasStatus() {
106+
factory.Logger.Info("")
107+
factory.Logger.Info(factory.Localizer.MustLocalize("status.log.info.noStatusesAreUsed"))
130108
return nil
131109
}
132110

133-
stdout := opts.IO.Out
111+
stdout := factory.IOStreams.Out
134112
if opts.outputFormat != "" {
135113
if err = dump.Formatted(stdout, opts.outputFormat, status); err != nil {
136114
return err

pkg/cmd/status/statusBuilder.go

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
package status
22

33
import (
4-
"context"
54
"fmt"
65
"io"
7-
"os"
8-
"path/filepath"
96
"reflect"
107
"strings"
118
"text/tabwriter"
129

13-
"github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil"
10+
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
11+
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
1412
"github.com/redhat-developer/app-services-cli/pkg/shared/servicespec"
1513
"github.com/redhat-developer/app-services-cli/pkg/shared/svcstatus"
14+
registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client"
1615

17-
kafkamgmtv1errors "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/error"
16+
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
1817

19-
"github.com/redhat-developer/app-services-cli/pkg/core/config"
20-
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
21-
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
2218
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
23-
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
2419

2520
"github.com/openconfig/goyang/pkg/indent"
2621
)
2722

2823
const tagTitle = "title"
2924

3025
type serviceStatus struct {
31-
Name string `json:"name,omitempty" title:"Name"`
32-
Location string `json:"location,omitempty" title:"Location"`
26+
Name string `json:"name,omitempty" title:"Service Context Name"`
27+
Location string `json:"location,omitempty" title:"Context File Location"`
3328
Kafka *kafkaStatus `json:"kafka,omitempty" title:"Kafka"`
3429
Registry *registryStatus `json:"registry,omitempty" title:"Service Registry"`
3530
}
3631

32+
func (s serviceStatus) hasStatus() bool {
33+
return s.Kafka != nil || s.Registry != nil
34+
}
35+
3736
type kafkaStatus struct {
3837
ID string `json:"id,omitempty"`
3938
Name string `json:"name,omitempty"`
@@ -50,91 +49,57 @@ type registryStatus struct {
5049
}
5150

5251
type clientConfig struct {
53-
context context.Context
54-
config config.IConfig
55-
Logger logging.Logger
56-
connection connection.Connection
57-
localizer localize.Localizer
52+
f *factory.Factory
5853
serviceConfig *servicecontext.ServiceConfig
5954
}
6055

6156
type statusClient struct {
62-
context context.Context
63-
config config.IConfig
64-
Logger logging.Logger
65-
conn connection.Connection
66-
localizer localize.Localizer
57+
f *factory.Factory
6758
serviceConfig *servicecontext.ServiceConfig
6859
}
6960

7061
// newStatusClient returns a new client to fetch service statuses
7162
// and build it into a service status config object
7263
func newStatusClient(cfg *clientConfig) *statusClient {
7364
return &statusClient{
74-
config: cfg.config,
75-
Logger: cfg.Logger,
76-
conn: cfg.connection,
77-
localizer: cfg.localizer,
65+
f: cfg.f,
7866
serviceConfig: cfg.serviceConfig,
7967
}
8068
}
8169

8270
// BuildStatus gets the status of all services currently set in the service context
83-
func (c *statusClient) BuildStatus(ctxName string, services []string) (status *serviceStatus, ok bool, err error) {
71+
func (c *statusClient) BuildStatus(services []string) (status *serviceStatus, err error) {
72+
factory := c.f
8473

8574
status = &serviceStatus{}
8675

87-
status.Name = ctxName
88-
89-
if rhoasContext := os.Getenv("RHOAS_CONTEXT"); rhoasContext != "" {
90-
status.Location = rhoasContext
91-
} else {
92-
ctxDirLocation, _ := servicecontext.DefaultDir()
93-
status.Location = filepath.Join(ctxDirLocation, "contexts.json")
94-
}
95-
9676
if stringInSlice(servicespec.KafkaServiceName, services) {
97-
if c.serviceConfig.KafkaID != "" {
98-
// nolint:govet
99-
kafkaStatus, err := c.getKafkaStatus(c.context, c.serviceConfig.KafkaID)
100-
if err != nil {
101-
if kafkamgmtv1errors.IsAPIError(err, kafkamgmtv1errors.ERROR_7) {
102-
err = kafkautil.NotFoundByIDError(c.serviceConfig.KafkaID)
103-
c.Logger.Error(err)
104-
c.Logger.Info(c.localizer.MustLocalize("status.log.info.rhoasKafkaUse"))
105-
}
106-
} else {
107-
status.Kafka = kafkaStatus
108-
ok = true
109-
}
110-
} else {
111-
c.Logger.Debug("No Kafka instance is currently used, skipping status check")
77+
kafkaResponse, err := contextutil.GetKafkaForServiceConfig(c.serviceConfig, factory)
78+
if err != nil {
79+
return status, err
11280
}
81+
kafkaStatus, err := c.getKafkaStatus(kafkaResponse)
82+
if err != nil {
83+
return status, err
84+
}
85+
status.Kafka = kafkaStatus
11386
}
11487

11588
if stringInSlice(servicespec.ServiceRegistryServiceName, services) {
116-
if c.serviceConfig.ServiceRegistryID != "" {
117-
// nolint:govet
118-
registry, newErr := c.getRegistryStatus(c.context, c.serviceConfig.ServiceRegistryID)
119-
if newErr != nil {
120-
return status, ok, newErr
121-
}
122-
status.Registry = registry
123-
ok = true
124-
} else {
125-
c.Logger.Debug("No service registry is currently used, skipping status check")
89+
registryResponse, err := contextutil.GetRegistryForServiceConfig(c.serviceConfig, factory)
90+
if err != nil {
91+
return status, err
12692
}
93+
registry, newErr := c.getRegistryStatus(registryResponse)
94+
if newErr != nil {
95+
return status, newErr
96+
}
97+
status.Registry = registry
12798
}
128-
129-
return status, ok, err
99+
return status, err
130100
}
131101

132-
func (c *statusClient) getKafkaStatus(ctx context.Context, id string) (status *kafkaStatus, err error) {
133-
kafkaResponse, _, err := c.conn.API().KafkaMgmt().GetKafkaById(ctx, id).Execute()
134-
if err != nil {
135-
return nil, err
136-
}
137-
102+
func (c *statusClient) getKafkaStatus(kafkaResponse *kafkamgmtclient.KafkaRequest) (status *kafkaStatus, err error) {
138103
status = &kafkaStatus{
139104
ID: kafkaResponse.GetId(),
140105
Name: kafkaResponse.GetName(),
@@ -149,12 +114,7 @@ func (c *statusClient) getKafkaStatus(ctx context.Context, id string) (status *k
149114
return status, err
150115
}
151116

152-
func (c *statusClient) getRegistryStatus(ctx context.Context, id string) (status *registryStatus, err error) {
153-
registry, _, err := c.conn.API().ServiceRegistryMgmt().GetRegistry(ctx, id).Execute()
154-
if err != nil {
155-
return nil, err
156-
}
157-
117+
func (c *statusClient) getRegistryStatus(registry *registrymgmtclient.Registry) (status *registryStatus, err error) {
158118
status = &registryStatus{
159119
ID: registry.GetId(),
160120
Name: registry.GetName(),

pkg/core/localize/locales/en/cmd/status.en.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,3 @@ one = 'No services set in context. To set a service in context, run "rhoas conte
3333

3434
[status.log.debug.noKafkaSelected]
3535
one = 'No Kafka instance is currently used, skipping status check'
36-
37-
[status.log.info.rhoasKafkaUse]
38-
one = 'Run "rhoas context use-kafka" to use another Kafka instance.'

pkg/core/servicecontext/file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type File struct{}
2020

2121
const errorFormat = "%v: %w"
2222

23-
const envName = "RHOAS_CONTEXT"
23+
const ContextEnvName = "RHOAS_CONTEXT"
2424

2525
// Load loads the profiles from the context file. If the context file doesn't exist
2626
// it will return an empty context object.
@@ -97,7 +97,7 @@ func (c *File) Remove() error {
9797
// Location gets the path to the context file
9898
func (c *File) Location() (path string, err error) {
9999

100-
if rhoasContext := os.Getenv(envName); rhoasContext != "" {
100+
if rhoasContext := os.Getenv(ContextEnvName); rhoasContext != "" {
101101

102102
_, err := os.Stat(rhoasContext)
103103
if os.IsNotExist(err) {
@@ -120,7 +120,7 @@ func (c *File) Location() (path string, err error) {
120120

121121
// Checks if context has custom location
122122
func HasCustomLocation() bool {
123-
rhoasContext := os.Getenv(envName)
123+
rhoasContext := os.Getenv(ContextEnvName)
124124
return rhoasContext != ""
125125
}
126126

0 commit comments

Comments
 (0)