Skip to content

Commit e043bb8

Browse files
author
Enda Phelan
committed
feat: standardise colors for printing to console
1 parent 25a7eb2 commit e043bb8

File tree

12 files changed

+61
-27
lines changed

12 files changed

+61
-27
lines changed

cmd/rhoas/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
76
"os"
87

8+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
9+
910
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/build"
1011

1112
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"

pkg/cluster/kubernetes_cluster.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
78
"os"
89
"path/filepath"
910
"time"
1011

11-
"github.com/fatih/color"
12-
1312
"github.com/AlecAivazis/survey/v2"
1413
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
1514
apiv1 "k8s.io/api/core/v1"
@@ -125,7 +124,7 @@ func (c *Kubernetes) Connect(ctx context.Context, secretName string, forceSelect
125124
}
126125

127126
// print status
128-
c.logger.Infof(statusMsg, color.HiGreenString(kafkaInstance.GetName()), color.HiGreenString(currentNamespace), color.HiGreenString(secretName))
127+
c.logger.Infof(statusMsg, color.Info(kafkaInstance.GetName()), color.Info(currentNamespace), color.Info(secretName))
129128

130129
var shouldContinue bool
131130
confirm := &survey.Confirm{
@@ -253,15 +252,15 @@ func (c *Kubernetes) createSecret(ctx context.Context, serviceAcct *serviceapicl
253252

254253
_, err = c.clientset.CoreV1().Secrets(namespace).Get(ctx, secretName, metav1.GetOptions{})
255254
if err == nil {
256-
return fmt.Errorf("Secret '%v' already exists. Please choose a different name with --secret-name", secretName)
255+
return fmt.Errorf("Secret %v already exists. Please choose a different name with --secret-name", color.Info(secretName))
257256
}
258257

259258
createdSecret, err := c.clientset.CoreV1().Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
260259
if err != nil {
261260
return fmt.Errorf("Could not create secret: %w", err)
262261
}
263262

264-
c.logger.Infof("Secret '%v' created", createdSecret.Name)
263+
c.logger.Infof("Secret %v created", color.Info(createdSecret.Name))
265264

266265
return nil
267266
}

pkg/cmd/cluster/status/status.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package status
33
import (
44
"context"
55
"fmt"
6+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
67

78
"github.com/MakeNowJust/heredoc"
89
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
@@ -11,8 +12,6 @@ import (
1112
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
1213
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
1314

14-
"github.com/fatih/color"
15-
1615
"github.com/spf13/cobra"
1716

1817
// Get all auth schemes
@@ -84,17 +83,17 @@ func runStatus(opts *Options) error {
8483
}
8584

8685
if isCRDInstalled {
87-
operatorStatus = color.HiGreenString("Installed")
86+
operatorStatus = color.Success("Installed")
8887
} else {
89-
operatorStatus = color.HiRedString("Not installed")
88+
operatorStatus = color.Error("Not installed")
9089
}
9190

9291
currentNamespace, err := clusterConn.CurrentNamespace()
9392
if err != nil {
9493
return err
9594
}
9695

97-
logger.Info(fmt.Sprintf(statusMsg, color.HiGreenString(currentNamespace), operatorStatus))
96+
logger.Info(fmt.Sprintf(statusMsg, color.Info(currentNamespace), operatorStatus))
9897

9998
return nil
10099
}

pkg/cmd/kafka/delete/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
78
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
89

910
"github.com/MakeNowJust/heredoc"
@@ -99,7 +100,7 @@ func runDelete(opts *options) error {
99100

100101
var confirmDeleteAction bool
101102
var promptConfirmAction = &survey.Confirm{
102-
Message: fmt.Sprintf("Are you sure you want to delete the Kafka instance '%v'?", kafkaName),
103+
Message: fmt.Sprintf("Are you sure you want to delete the Kafka instance %v?", color.Info(kafkaName)),
103104
}
104105

105106
err = survey.AskOne(promptConfirmAction, &confirmDeleteAction)
@@ -132,7 +133,7 @@ func runDelete(opts *options) error {
132133
return fmt.Errorf("Unable to delete Kafka instance: %w", apiErr)
133134
}
134135

135-
logger.Infof("Kafka instance '%v' has successfully been deleted", kafkaName)
136+
logger.Infof("Kafka instance %v has successfully been deleted", color.Info(kafkaName))
136137

137138
currentKafka := cfg.Services.Kafka
138139
// this is not the current cluster, our work here is done

pkg/cmd/login/login.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
1616
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/auth/token"
1717
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
18+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
1819
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
1920

2021
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/browser"
@@ -141,7 +142,7 @@ func runLogin(opts *Options) error {
141142
return err
142143
}
143144
if gatewayURL.Scheme != "http" && gatewayURL.Scheme != "https" {
144-
return fmt.Errorf("Scheme missing from URL '%v'. Please add either 'https' or 'https'.", unparsedGatewayURL)
145+
return fmt.Errorf("Scheme missing from URL %v. Please add either 'https' or 'https'.", color.Info(unparsedGatewayURL))
145146
}
146147

147148
tr := createTransport(opts.insecureSkipTLSVerify)
@@ -265,7 +266,8 @@ func runLogin(opts *Options) error {
265266
if !ok {
266267
logger.Info("You are now logged in")
267268
} else {
268-
logger.Infof("You are now logged in as %v", userName)
269+
rawUsername := fmt.Sprintf("%v", userName)
270+
logger.Infof("You are now logged in as %v", color.Info(rawUsername))
269271
}
270272

271273
cancel()

pkg/cmd/serviceaccount/create/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
78
"os"
89

910
"github.com/AlecAivazis/survey/v2"
@@ -132,7 +133,7 @@ func runCreate(opts *Options) error {
132133
// indicating that the user should explicitly request overwriting of the file
133134
_, err = os.Stat(opts.filename)
134135
if err == nil && !opts.overwrite {
135-
return fmt.Errorf("file '%v' already exists. Use --overwrite to overwrite the file, or --file-location flag to choose a custom location", opts.filename)
136+
return fmt.Errorf("file %v already exists. Use --overwrite to overwrite the file, or --file-location flag to choose a custom location", color.Info(opts.filename))
136137
}
137138

138139
// create the service account
@@ -147,7 +148,7 @@ func runCreate(opts *Options) error {
147148
return fmt.Errorf("Could not create service account: %w", apiErr)
148149
}
149150

150-
logger.Infof("Service account '%v' created", serviceacct.GetName())
151+
logger.Infof("Service account %v created", color.Info(serviceacct.GetName()))
151152

152153
creds := &credentials.Credentials{
153154
ClientID: serviceacct.GetClientID(),

pkg/cmd/serviceaccount/delete/delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/MakeNowJust/heredoc"
99
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
1010
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
11+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
1112
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
1213
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
1314
"github.com/spf13/cobra"
@@ -58,7 +59,7 @@ func runDelete(opts *Options) (err error) {
5859

5960
var confirmDelete bool
6061
promptConfirmDelete := &survey.Confirm{
61-
Message: fmt.Sprintf("Are you sure you want to delete the service account with ID '%v'?", opts.id),
62+
Message: fmt.Sprintf("Are you sure you want to delete the service account with ID %v?", color.Info(opts.id)),
6263
}
6364

6465
err = survey.AskOne(promptConfirmDelete, &confirmDelete)

pkg/cmd/serviceaccount/resetcredentials/reset_credentials.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
78
"os"
89

910
"github.com/AlecAivazis/survey/v2"
@@ -116,14 +117,14 @@ func runResetCredentials(opts *Options) (err error) {
116117
// If the credentials file already exists, and the --overwrite flag is not set then return an error
117118
// indicating that the user should explicitly request overwriting of the file
118119
if _, err = os.Stat(opts.filename); err == nil && !opts.overwrite {
119-
return fmt.Errorf("file '%v' already exists. Use --overwrite to overwrite the file, or --file-location to choose a custom location", opts.filename)
120+
return fmt.Errorf("file %v already exists. Use --overwrite to overwrite the file, or --file-location to choose a custom location", color.Info(opts.filename))
120121
}
121122
}
122123

123124
// prompt the user to confirm their wish to proceed with this action
124125
var confirmReset bool
125126
promptConfirmDelete := &survey.Confirm{
126-
Message: fmt.Sprintf("Are you sure you want to reset the credentials for the service account with ID '%v'?", opts.id),
127+
Message: fmt.Sprintf("Are you sure you want to reset the credentials for the service account with ID %v?", color.Info(opts.id)),
127128
}
128129

129130
if err = survey.AskOne(promptConfirmDelete, &confirmReset); err != nil {

pkg/color/color.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Color package is for printing a uniform set of colors for the CLI
2+
package color
3+
4+
import (
5+
"github.com/fatih/color"
6+
)
7+
8+
// CodeSnippet returns a colored string for code and command snippets
9+
func CodeSnippet(format string) string {
10+
return color.HiMagentaString(format)
11+
}
12+
13+
// Info returns a colored string for information messages
14+
func Info(format string) string {
15+
return color.HiCyanString(format)
16+
}
17+
18+
// Success returns a colored string for success messages
19+
func Success(format string) string {
20+
return color.HiGreenString(format)
21+
}
22+
23+
// Error returns a colored string for error messages
24+
func Error(format string) string {
25+
return color.HiRedString(format)
26+
}

pkg/connection/errors.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package connection
22

33
import (
4-
"errors"
54
"fmt"
5+
6+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
67
)
78

89
var (
10+
loginCmd = color.CodeSnippet("rhoas login")
911
// ErrNotLoggedIn defines when a user is not authenticated
10-
ErrNotLoggedIn = errors.New("Not logged in. Run `rhoas login` to authenticate")
12+
ErrNotLoggedIn = fmt.Errorf("Not logged in. Run %v to authenticate", loginCmd)
1113
// ErrSessionExpired defines when a user's session has expired
12-
ErrSessionExpired = errors.New("Session expired. Run `rhoas login` to authenticate")
14+
ErrSessionExpired = fmt.Errorf("Session expired. Run %v to authenticate", loginCmd)
1315
)
1416

1517
// AuthError defines an Authentication error

0 commit comments

Comments
 (0)