-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: Add
--color
flag to cerbos compile
By default, the output color level is detected from the environment by checking if stdout is a TTY and looking for various environment variables. See https://pkg.go.dev/github.com/jwalton/go-supportscolor for details. Signed-off-by: Andrew Haines <haines@cerbos.dev>
- Loading branch information
Showing
9 changed files
with
402 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2021-2022 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package outputcolor | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/jwalton/go-supportscolor" | ||
) | ||
|
||
type Level uint8 | ||
|
||
const ( | ||
None = Level(supportscolor.None) | ||
Basic = Level(supportscolor.Basic) | ||
Ansi256 = Level(supportscolor.Ansi256) | ||
Ansi16m = Level(supportscolor.Ansi16m) | ||
) | ||
|
||
var TypeMapper = kong.TypeMapper(reflect.TypeOf((*Level)(nil)), kong.MapperFunc(decode)) | ||
|
||
func (l *Level) Resolve(disable bool) Level { | ||
if disable { | ||
return None | ||
} | ||
|
||
if l != nil { | ||
return *l | ||
} | ||
|
||
return Level(supportscolor.SupportsColor(os.Stdout.Fd(), supportscolor.SniffFlagsOption(false)).Level) | ||
} | ||
|
||
func (l Level) Enabled() bool { | ||
return l > None | ||
} | ||
|
||
func decode(ctx *kong.DecodeContext, target reflect.Value) error { | ||
level, err := scan(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
target.Set(reflect.ValueOf(level)) | ||
return nil | ||
} | ||
|
||
func scan(ctx *kong.DecodeContext) (*Level, error) { | ||
token := ctx.Scan.Peek() | ||
|
||
switch token.Type { | ||
case kong.FlagValueToken: | ||
return parse(ctx.Scan.Pop().Value) | ||
|
||
case kong.ShortFlagTailToken, kong.UntypedToken: | ||
level, err := parse(token.Value) | ||
if err == nil { | ||
ctx.Scan.Pop() | ||
return level, nil | ||
} | ||
} | ||
|
||
return pointer(Basic), nil | ||
} | ||
|
||
func parse(v interface{}) (*Level, error) { | ||
s, ok := v.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid flag value (expected string, got %T)", v) | ||
} | ||
|
||
switch s { | ||
case "auto": | ||
return nil, nil | ||
|
||
case "false", "never": | ||
return pointer(None), nil | ||
|
||
case "true", "always": | ||
return pointer(Basic), nil | ||
|
||
case "256": | ||
return pointer(Ansi256), nil | ||
|
||
case "16m", "full", "truecolor": | ||
return pointer(Ansi16m), nil | ||
|
||
default: | ||
return nil, fmt.Errorf("invalid value for output color level: %q", s) | ||
} | ||
} | ||
|
||
func pointer(level Level) *Level { | ||
return &level | ||
} |
Oops, something went wrong.