Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Implement NO_COLOR environment variable to enable/disable Aurora pack…
Browse files Browse the repository at this point in the history
…age Color property (#3330)

* use NO_COLOR environment variable to enable/disable Aurora Color

* add check TERM environment variable to enable/disable Aurora Color

* handle TTY on aurora pkg

* move aurora funcs to component

* sort imports

* fix the sort imports

* remove unused tests

* unit tests
  • Loading branch information
mpanchajanya committed Oct 4, 2022
1 parent c1caaaa commit 3e3fb14
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 77 deletions.
39 changes: 7 additions & 32 deletions cli/core/pkg/cli/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"fmt"
"io"
"os"
"strings"
"text/template"
"unicode"

"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"

"github.com/vmware-tanzu/tanzu-framework/cli/runtime/component"
"github.com/vmware-tanzu/tanzu-framework/cli/runtime/config"
)

Expand Down Expand Up @@ -60,7 +58,7 @@ func (u *MainUsage) GenerateDescriptor(c *cobra.Command, w io.Writer) error {
if err != nil {
serverString = "Not logged in"
} else {
serverString = fmt.Sprintf("Logged in to %s", underline(s.Name))
serverString = fmt.Sprintf("Logged in to %s", component.Underline(s.Name))
}
d := struct {
*cobra.Command
Expand Down Expand Up @@ -140,32 +138,9 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e

// TemplateFuncs are the template usage funcs.
var TemplateFuncs = template.FuncMap{
"rpad": rpad,
"bold": bold,
"underline": underline,
"trimTrailingWhitespaces": trimRightSpace,
"beginsWith": beginsWith,
}

// rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
func rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
}

func underline(s string) string {
return aurora.Underline(s).String()
}

func bold(s string) string {
return aurora.Bold(s).String()
}

func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func beginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
"rpad": component.Rpad,
"bold": component.Bold,
"underline": component.Underline,
"trimTrailingWhitespaces": component.TrimRightSpace,
"beginsWith": component.BeginsWith,
}
6 changes: 0 additions & 6 deletions cli/core/pkg/cli/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cli

import (
"fmt"
"testing"

"github.com/logrusorgru/aurora"
Expand All @@ -25,8 +24,3 @@ func TestGenerateDescriptor(t *testing.T) {
err := f(c)
require.NoError(t, err)
}

func TestRPad(t *testing.T) {
s := rpad("my string", 15)
fmt.Println(s + "this")
}
9 changes: 2 additions & 7 deletions cli/core/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

"github.com/aunum/log"
"github.com/briandowns/spinner"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"

"github.com/vmware-tanzu/tanzu-framework/cli/core/pkg/cli"
cliconfig "github.com/vmware-tanzu/tanzu-framework/cli/core/pkg/config"
"github.com/vmware-tanzu/tanzu-framework/cli/core/pkg/pluginmanager"
cliapi "github.com/vmware-tanzu/tanzu-framework/cli/runtime/apis/cli/v1alpha1"
configapi "github.com/vmware-tanzu/tanzu-framework/cli/runtime/apis/config/v1alpha1"
"github.com/vmware-tanzu/tanzu-framework/cli/runtime/component"
"github.com/vmware-tanzu/tanzu-framework/cli/runtime/config"
)

Expand All @@ -32,7 +32,6 @@ var RootCmd = &cobra.Command{

var (
noInit bool
color = true
forceNoInit = "true" // a string variable so as to be overridable via linker flag
)

Expand All @@ -47,15 +46,11 @@ func NewRootCmd() (*cobra.Command, error) {
if ni != "" || strings.EqualFold(forceNoInit, "true") {
noInit = true
}
if os.Getenv("TANZU_CLI_NO_COLOR") != "" {
color = false
}

// configure defined environment variables under tanzu config file
cliconfig.ConfigureEnvVariables()

au := aurora.NewAurora(color)
RootCmd.Short = au.Bold(`Tanzu CLI`).String()
RootCmd.Short = component.Bold(`Tanzu CLI`)

// TODO (pbarker): silencing usage for now as we are getting double usage from plugins on errors
RootCmd.SilenceUsage = true
Expand Down
55 changes: 55 additions & 0 deletions cli/runtime/component/colorable-tty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package component

import (
"fmt"
"os"
"strings"
"unicode"

auroraPackage "github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
)

var aurora auroraPackage.Aurora

func init() {
NewAurora()
}

func NewAurora() auroraPackage.Aurora {
if aurora == nil {
aurora = auroraPackage.NewAurora(IsTTYEnabled())
}
return aurora
}

func IsTTYEnabled() bool {
ttyEnabled := true
if os.Getenv("TANZU_CLI_NO_COLOR") != "" || os.Getenv("NO_COLOR") != "" || strings.EqualFold(os.Getenv("TERM"), "DUMB") || !isatty.IsTerminal(os.Stdout.Fd()) {
ttyEnabled = false
}
return ttyEnabled
}

// Rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
func Rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
}

func Underline(s string) string {
return aurora.Underline(s).String()
}

func Bold(s string) string {
return aurora.Bold(s).String()
}

func TrimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func BeginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
34 changes: 34 additions & 0 deletions cli/runtime/component/colorable-tty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package component

import (
"fmt"
"os"
"testing"
)

func TestIsTTYEnabled(t *testing.T) {
envTests := []struct {
key, val string
want bool
}{
{"TANZU_CLI_NO_COLOR", "1", false},
{"NO_COLOR", "1", false},
{"TERM", "dumb", false},
{"TERM", "duMb", false},
}

for _, tt := range envTests {
testName := fmt.Sprintf("(%v,%v):%v", tt.key, tt.val, tt.want)
t.Run(testName, func(t *testing.T) {
err := os.Setenv(tt.key, tt.val)
if err != nil {
return
}
ans := IsTTYEnabled()
if ans != tt.want {
t.Errorf("got %v : want %v", ans, tt.want)
}
})
}

}
39 changes: 7 additions & 32 deletions cli/runtime/plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
package plugin

import (
"fmt"
"os"
"strings"
"text/template"
"unicode"

"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"

"github.com/vmware-tanzu/tanzu-framework/cli/runtime/component"
)

// UsageFunc is the usage func for a plugin.
Expand Down Expand Up @@ -51,32 +49,9 @@ Use "{{if beginsWith .CommandPath "tanzu "}}{{.CommandPath}}{{else}}tanzu {{.Com

// TemplateFuncs are the template usage funcs.
var TemplateFuncs = template.FuncMap{
"rpad": rpad,
"bold": bold,
"underline": underline,
"trimTrailingWhitespaces": trimRightSpace,
"beginsWith": beginsWith,
}

// rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
func rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
}

func underline(s string) string {
return aurora.Underline(s).String()
}

func bold(s string) string {
return aurora.Bold(s).String()
}

func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func beginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
"rpad": component.Rpad,
"bold": component.Bold,
"underline": component.Underline,
"trimTrailingWhitespaces": component.TrimRightSpace,
"beginsWith": component.BeginsWith,
}

0 comments on commit 3e3fb14

Please sign in to comment.