Skip to content

Commit

Permalink
feat: standardise colors for printing to console (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda authored Jan 26, 2021
1 parent 25a7eb2 commit 2c7f7f0
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 27 deletions.
3 changes: 2 additions & 1 deletion cmd/rhoas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"errors"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
"os"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/build"

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
Expand Down
9 changes: 4 additions & 5 deletions pkg/cluster/kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"os"
"path/filepath"
"time"

"github.com/fatih/color"

"github.com/AlecAivazis/survey/v2"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -125,7 +124,7 @@ func (c *Kubernetes) Connect(ctx context.Context, secretName string, forceSelect
}

// print status
c.logger.Infof(statusMsg, color.HiGreenString(kafkaInstance.GetName()), color.HiGreenString(currentNamespace), color.HiGreenString(secretName))
c.logger.Infof(statusMsg, color.Info(kafkaInstance.GetName()), color.Info(currentNamespace), color.Info(secretName))

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

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

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

c.logger.Infof("Secret '%v' created", createdSecret.Name)
c.logger.Infof("Secret %v created", color.Info(createdSecret.Name))

return nil
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/cmd/cluster/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package status
import (
"context"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"

"github.com/MakeNowJust/heredoc"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
Expand All @@ -11,8 +12,6 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

"github.com/fatih/color"

"github.com/spf13/cobra"

// Get all auth schemes
Expand Down Expand Up @@ -84,17 +83,17 @@ func runStatus(opts *Options) error {
}

if isCRDInstalled {
operatorStatus = color.HiGreenString("Installed")
operatorStatus = color.Success("Installed")
} else {
operatorStatus = color.HiRedString("Not installed")
operatorStatus = color.Error("Not installed")
}

currentNamespace, err := clusterConn.CurrentNamespace()
if err != nil {
return err
}

logger.Info(fmt.Sprintf(statusMsg, color.HiGreenString(currentNamespace), operatorStatus))
logger.Info(fmt.Sprintf(statusMsg, color.Info(currentNamespace), operatorStatus))

return nil
}
5 changes: 3 additions & 2 deletions pkg/cmd/kafka/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

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

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

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

logger.Infof("Kafka instance '%v' has successfully been deleted", kafkaName)
logger.Infof("Kafka instance %v has successfully been deleted", color.Info(kafkaName))

currentKafka := cfg.Services.Kafka
// this is not the current cluster, our work here is done
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/auth/token"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

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

tr := createTransport(opts.insecureSkipTLSVerify)
Expand Down Expand Up @@ -265,7 +266,8 @@ func runLogin(opts *Options) error {
if !ok {
logger.Info("You are now logged in")
} else {
logger.Infof("You are now logged in as %v", userName)
rawUsername := fmt.Sprintf("%v", userName)
logger.Infof("You are now logged in as %v", color.Info(rawUsername))
}

cancel()
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/serviceaccount/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"os"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -132,7 +133,7 @@ func runCreate(opts *Options) error {
// indicating that the user should explicitly request overwriting of the file
_, err = os.Stat(opts.filename)
if err == nil && !opts.overwrite {
return fmt.Errorf("file '%v' already exists. Use --overwrite to overwrite the file, or --file-location flag to choose a custom location", opts.filename)
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))
}

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

logger.Infof("Service account '%v' created", serviceacct.GetName())
logger.Infof("Service account %v created", color.Info(serviceacct.GetName()))

creds := &credentials.Credentials{
ClientID: serviceacct.GetClientID(),
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/serviceaccount/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -58,7 +59,7 @@ func runDelete(opts *Options) (err error) {

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

err = survey.AskOne(promptConfirmDelete, &confirmDelete)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/serviceaccount/resetcredentials/reset_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"os"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -116,14 +117,14 @@ func runResetCredentials(opts *Options) (err error) {
// If the credentials file already exists, and the --overwrite flag is not set then return an error
// indicating that the user should explicitly request overwriting of the file
if _, err = os.Stat(opts.filename); err == nil && !opts.overwrite {
return fmt.Errorf("file '%v' already exists. Use --overwrite to overwrite the file, or --file-location to choose a custom location", opts.filename)
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))
}
}

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

if err = survey.AskOne(promptConfirmDelete, &confirmReset); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Color package is for printing a uniform set of colors for the CLI
package color

import (
"github.com/fatih/color"
)

// CodeSnippet returns a colored string for code and command snippets
func CodeSnippet(format string) string {
return color.HiMagentaString(format)
}

// Info returns a colored string for information messages
func Info(format string) string {
return color.HiCyanString(format)
}

// Success returns a colored string for success messages
func Success(format string) string {
return color.HiGreenString(format)
}

// Error returns a colored string for error messages
func Error(format string) string {
return color.HiRedString(format)
}
8 changes: 5 additions & 3 deletions pkg/connection/errors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package connection

import (
"errors"
"fmt"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
)

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

// AuthError defines an Authentication error
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/kafka/topics/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"strconv"
"time"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
pkgKafka "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
serviceapi "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/serviceapi/client"
"github.com/fatih/color"
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/plain"
)
Expand Down Expand Up @@ -174,8 +174,8 @@ func ListKafkaTopics(opts *Options) error {
topicPartition := &partitions[i]
replicas := strconv.Itoa(len(topicPartition.Replicas))
logger.Infof("Name: %v (Replicas: %v)\n",
color.HiGreenString(topicPartition.Topic),
color.HiRedString(replicas))
color.Success(topicPartition.Topic),
color.Info(replicas))
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/serviceaccount/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package credentials

import (
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/color"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -143,7 +144,7 @@ func ChooseFileLocation(outputFormat string, filePath string, overwrite bool) (s
}

overwriteFilePrompt := &survey.Confirm{
Message: fmt.Sprintf("The file '%v' already exists. Do you want to overwrite it?", filePath),
Message: fmt.Sprintf("file %v already exists. Do you want to overwrite it?", color.Info(filePath)),
}

err = survey.AskOne(overwriteFilePrompt, &overwrite)
Expand Down

0 comments on commit 2c7f7f0

Please sign in to comment.