mirror of https://github.com/hashicorp/terraform
parent
dbc1c63d79
commit
6c736bd3c4
@ -0,0 +1,35 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/colorstring"
|
||||
)
|
||||
|
||||
// Meta are the meta-options that are available on all or most commands.
|
||||
type Meta struct {
|
||||
Color bool
|
||||
}
|
||||
|
||||
// Colorize returns the colorization structure for a command.
|
||||
func (m *Meta) Colorize() *colorstring.Colorize {
|
||||
return &colorstring.Colorize{
|
||||
Colors: colorstring.DefaultColors,
|
||||
Disable: !m.Color,
|
||||
Reset: true,
|
||||
}
|
||||
}
|
||||
|
||||
// process will process the meta-parameters out of the arguments. This
|
||||
// will potentially modify the args in-place. It will return the resulting
|
||||
// slice.
|
||||
func (m *Meta) process(args []string) []string {
|
||||
m.Color = true
|
||||
|
||||
for i, v := range args {
|
||||
if v == "-no-color" {
|
||||
m.Color = false
|
||||
return append(args[:i], args[i+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMetaColorize(t *testing.T) {
|
||||
var m *Meta
|
||||
var args, args2 []string
|
||||
|
||||
// Test basic, no change
|
||||
m = new(Meta)
|
||||
args = []string{"foo", "bar"}
|
||||
args2 = []string{"foo", "bar"}
|
||||
args = m.process(args)
|
||||
if !reflect.DeepEqual(args, args2) {
|
||||
t.Fatalf("bad: %#v", args)
|
||||
}
|
||||
if m.Colorize().Disable {
|
||||
t.Fatal("should not be disabled")
|
||||
}
|
||||
|
||||
// Test disable #1
|
||||
m = new(Meta)
|
||||
args = []string{"foo", "-no-color", "bar"}
|
||||
args2 = []string{"foo", "bar"}
|
||||
args = m.process(args)
|
||||
if !reflect.DeepEqual(args, args2) {
|
||||
t.Fatalf("bad: %#v", args)
|
||||
}
|
||||
if !m.Colorize().Disable {
|
||||
t.Fatal("should be disabled")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue