Skip to content

Commit

Permalink
Merge branch 'fix-color-output'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jun 26, 2021
2 parents 7553e00 + bd0f2da commit bc8cf06
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
CGO_ENABLED: 0
- run: go test -v -race -coverprofile coverage.txt -covermode=atomic
# Dog fooding 🐶
- run: ./actionlint
- run: ./actionlint -color
- uses: codecov/codecov-action@v1
lint:
name: Lint
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
shell: bash
- name: Check workflow files
run: ${{ steps.get_actionlint.outputs.executable }}
run: ${{ steps.get_actionlint.outputs.executable }} -color
shell: bash
```

Expand Down
12 changes: 11 additions & 1 deletion cmd/actionlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ func main() {
var opts actionlint.LinterOptions
var ignorePats ignorePatterns
var initConfig bool
var noColor bool
var color bool

flag.Var(&ignorePats, "ignore", "Regular expression matching to error messages you want to ignore. This flag can be specified multiple times")
flag.StringVar(&opts.Shellcheck, "shellcheck", "shellcheck", "Command name or file path of \"shellcheck\" external command")
flag.StringVar(&opts.Pyflakes, "pyflakes", "pyflakes", "Command name or file path of \"pyflakes\" external command")
flag.BoolVar(&opts.Oneline, "oneline", false, "Use one line per one error. Useful for reading error messages from programs")
flag.StringVar(&opts.ConfigFile, "config-file", "", "File path to config file")
flag.BoolVar(&initConfig, "init-config", false, "Generate default config file at .github/actionlint.yaml in current project")
flag.BoolVar(&opts.NoColor, "no-color", false, "Disable colorful output")
flag.BoolVar(&noColor, "no-color", false, "Disable colorful output")
flag.BoolVar(&color, "color", false, "Always enable colorful output. This is useful to force colorful outputs")
flag.BoolVar(&opts.Verbose, "verbose", false, "Enable verbose output")
flag.BoolVar(&opts.Debug, "debug", false, "Enable debug output (for development)")
flag.BoolVar(&ver, "version", false, "Show version and how this binary was installed")
Expand All @@ -128,6 +131,13 @@ func main() {

opts.IgnorePatterns = ignorePats

if color {
opts.Color = actionlint.ColorOptionKindAlways
}
if noColor {
opts.Color = actionlint.ColorOptionKindNever
}

errs, err := run(flag.Args(), &opts, initConfig)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
Expand Down
32 changes: 25 additions & 7 deletions linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ const (
LogLevelDebug = 2
)

// ColorOptionKind is kind of colorful output behavior.
type ColorOptionKind int

const (
// ColorOptionKindAuto is kind to determine to colorize errors output automatically. It is
// determined based on pty and $NO_COLOR environment variable. See document of fatih/color
// for more details.
ColorOptionKindAuto ColorOptionKind = iota
// ColorOptionKindAlways is kind to always colorize errors output.
ColorOptionKindAlways
// ColorOptionKindNever is kind never to colorize errors output.
ColorOptionKindNever
)

// LinterOptions is set of options for Linter instance. This struct should be created by user and
// given to NewLinter factory function.
type LinterOptions struct {
Expand All @@ -40,8 +54,8 @@ type LinterOptions struct {
// LogWriter is io.Writer object to use to print log outputs. Note that error outputs detected
// by the linter are not included in the log outputs.
LogWriter io.Writer
// NoColor is flag if colorful output is enabled.
NoColor bool
// Color is option for colorizing error outputs. See ColorOptionKind document for each enum values.
Color ColorOptionKind
// Oneline is flag if one line output is enabled. When enabling it, one error is output per one
// line. It is useful when reading outputs from programs.
Oneline bool
Expand All @@ -68,7 +82,6 @@ type Linter struct {
out io.Writer
logOut io.Writer
logLevel LogLevel
noColor bool
oneline bool
shellcheck string
pyflakes string
Expand All @@ -87,15 +100,21 @@ func NewLinter(out io.Writer, opts *LinterOptions) (*Linter, error) {
} else if opts.Debug {
l = LogLevelDebug
}
if opts.NoColor {
if opts.Color == ColorOptionKindNever {
color.NoColor = true
} else {
if opts.Color == ColorOptionKindAlways {
color.NoColor = false
}
// Allow colorful output on Windows
if f, ok := out.(*os.File); ok {
out = colorable.NewColorable(f)
}
}

var lout io.Writer = os.Stderr
if opts.LogWriter != nil {
lout = opts.LogWriter
} else if !opts.NoColor {
lout = colorable.NewColorable(os.Stderr)
}

var cfg *Config
Expand All @@ -121,7 +140,6 @@ func NewLinter(out io.Writer, opts *LinterOptions) (*Linter, error) {
out,
lout,
l,
opts.NoColor,
opts.Oneline,
opts.Shellcheck,
opts.Pyflakes,
Expand Down

0 comments on commit bc8cf06

Please sign in to comment.