Skip to content

Commit

Permalink
add config option to toggle colored output (gopasspw#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
qbit authored and dominikschulz committed Oct 24, 2017
1 parent f98eef3 commit 8ab5b4e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func New() *Config {
NoPager: false,
SafeContent: false,
UseSymbols: false,
NoColor: false,
},
Mounts: make(map[string]*StoreConfig),
Version: "",
Expand Down
1 change: 1 addition & 0 deletions config/store_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type StoreConfig struct {
Path string `yaml:"path"` // path to the root store
SafeContent bool `yaml:"safecontent"` // avoid showing passwords in terminal
UseSymbols bool `yaml:"usesymbols"` // always use symbols when generating passwords
NoColor bool `yaml:"nocolor"` // do not use color when outputing text
}

// ConfigMap returns a map of stringified config values for easy printing
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ This is a list of options available:
| `path` | `string` | Path to the root store. |
| `safecontent` | `bool` | Only output _safe content_ (i.e. everything but the first line of a secret) to the terminal. Use _copy_ (`-c`) to retrieve the password in the clipboard. |
| `usesymbols` | `bool` | If enabled - it will use symbols when generating passwords. |
| `nocolor` | `bool` | Do not use color. |
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func main() {
// always use symbols
ctx = ctxutil.WithUseSymbols(ctx, cfg.Root.UseSymbols)

// never use color
ctx = ctxutil.WithNoColor(ctx, cfg.Root.NoColor)

// check recipients conflicts with always trust, make sure it's not enabled
// when always trust is
if gpg.IsAlwaysTrust(ctx) {
Expand All @@ -95,7 +98,7 @@ func main() {
}

// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" || ctxutil.IsNoColor(ctx) {
color.NoColor = true
ctx = ctxutil.WithColor(ctx, false)
}
Expand Down
21 changes: 21 additions & 0 deletions utils/ctxutil/ctxutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
ctxKeyGitCommit
ctxKeyAlwaysYes
ctxKeyUseSymbols
ctxKeyNoColor
)

// WithDebug returns a context with an explizit value for debug
Expand Down Expand Up @@ -262,6 +263,26 @@ func IsUseSymbols(ctx context.Context) bool {
return bv
}

// WithNoColor returns a context with the value for ask for more set
func WithNoColor(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyNoColor, bv)
}

// HasNoColor returns true if a value for NoColor has been set in this context
func HasNoColor(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyNoColor).(bool)
return ok
}

// IsNoColor returns the value of ask for more or the default (false)
func IsNoColor(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNoColor).(bool)
if !ok {
return false
}
return bv
}

// WithAlwaysYes returns a context with the value of always yes set
func WithAlwaysYes(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyAlwaysYes, bv)
Expand Down
1 change: 1 addition & 0 deletions utils/ctxutil/ctxutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestComposite(t *testing.T) {
ctx = WithGitCommit(ctx, false)
ctx = WithUseSymbols(ctx, false)
ctx = WithAlwaysYes(ctx, true)
ctx = WithNoColor(ctx, true)

if !IsDebug(ctx) {
t.Errorf("Debug should be true")
Expand Down

0 comments on commit 8ab5b4e

Please sign in to comment.