Skip to content

Commit

Permalink
Add no colour option
Browse files Browse the repository at this point in the history
It uses color.NoColour global from color library directly, I could use the
DisableColor from the Color option but that would have involved passing around
the nocolour flag which not much benefits

Fixes #289

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel authored and tekton-robot committed Sep 24, 2019
1 parent 7d09fe5 commit 0e9a460
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/cli/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ type Params interface {
SetNamespace(string)
Namespace() string

// SetNoColour set colouring or not
SetNoColour(bool)

Time() clockwork.Clock
}
5 changes: 5 additions & 0 deletions pkg/cli/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cli
import (
"k8s.io/client-go/rest"

"github.com/fatih/color"
"github.com/jonboulle/clockwork"
"github.com/pkg/errors"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -104,6 +105,10 @@ func (p *TektonParams) config() (*rest.Config, error) {
return config, nil
}

func (p *TektonParams) SetNoColour(b bool) {
color.NoColor = b
}

func (p *TektonParams) SetNamespace(ns string) {
p.namespace = ns
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
const (
kubeConfig = "kubeconfig"
namespace = "namespace"
nocolour = "nocolour"
)

// AddTektonOptions amends command to add flags required to initialise a cli.Param
Expand All @@ -36,6 +37,10 @@ func AddTektonOptions(cmd *cobra.Command) {
namespace, "n", "",
"namespace to use (default: from $KUBECONFIG)")

cmd.PersistentFlags().BoolP(
nocolour, "C", false,
"disable colouring (default: false)")

// Add custom completion for that command as specified in
// bashCompletionFlags map
for name, completion := range completion.ShellCompletionMap {
Expand Down Expand Up @@ -69,6 +74,12 @@ func InitParams(p cli.Params, cmd *cobra.Command) error {
p.SetNamespace(ns)
}

nocolourFlag, err := cmd.Flags().GetBool(nocolour)
if err != nil {
return err
}
p.SetNoColour(nocolourFlag)

return nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ package flags
import (
"testing"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/tektoncd/cli/pkg/cli"
)

func TestFlags_add_shell_completion(t *testing.T) {
Expand All @@ -36,5 +39,15 @@ func TestFlags_add_shell_completion(t *testing.T) {
if pflag.Annotations[cobra.BashCompCustom][0] != shellfunc {
t.Errorf("annotation should have been added to the flag")
}
}

func TestFlags_colouring(t *testing.T) {
cmd := &cobra.Command{}
cmd.SetArgs([]string{"--nocolour"})
_ = InitParams(&cli.TektonParams{}, cmd)
assert.False(t, color.NoColor)

_ = InitParams(&cli.TektonParams{}, &cobra.Command{})
assert.True(t, color.NoColor)

}
7 changes: 1 addition & 6 deletions pkg/formatted/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ func NewColor() *Color {
}
}

//PrintBlue prints the formatted content to given destination in blue color
func (c *Color) PrintBlue(w io.Writer, format string, args ...interface{}) {
c.blue.Fprintf(w, format, args...)
}

//PrintBlue prints the formatted content to given destination in red color
//PrintRed prints the formatted content to given destination in red color
func (c *Color) PrintRed(w io.Writer, format string, args ...interface{}) {
c.red.Fprintf(w, format, args...)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/test/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (p *Params) Namespace() string {
return p.ns
}

func (p *Params) SetNoColour(b bool) {
}

func (p *Params) SetKubeConfigPath(path string) {
p.kc = path
}
Expand Down

0 comments on commit 0e9a460

Please sign in to comment.