Skip to content

Commit

Permalink
add new output and deprecate skip_output
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Feb 23, 2024
1 parent ed10e37 commit 0e83cf6
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 15 deletions.
38 changes: 38 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,46 @@ If you want to specify a minimum version for lefthook binary (e.g. if you need s
min_version: 1.1.3
```

### `output`

You can manage verbosity using the `output` config. You can specify what to print in your output by setting these values, which you need to have

Possible values are `meta,summary,success,failure,execution,execution_out,execution_info,skips`.
By default, all output values are enabled

You can also disable all output with setting `output: false`. In this case only errors will be printed.

This config quiets all outputs except for errors.

`output` is enabled if there is no `skip_output` and `LEFTHOOK_QUIET`.

**Example**

```yml
# lefthook.yml
output:
- meta # Print lefthook version
- summary # Print summary block (successful and failed steps)
- empty_summary # Print summary heading when there are no steps to run
- success # Print successful steps
- failure # Print failed steps printing
- execution # Print any execution logs (but prints if the execution failed)
- execution_out # Print execution output (but still prints failed commands output)
- execution_info # Print `EXECUTE > ...` logging
- skips # Print "skip" (i.e. no files matched)
```
You can also *extend* this list with an environment variable `LEFTHOOK_OUTPUT`:

```bash
LEFTHOOK_OUTPUT="meta,success,summary" lefthook run pre-commit
```

### `skip_output`

> **Deprecated:** This feature is deprecated and might be removed in future versions. Please, use `[output]` instead for managing verbosity.

You can manage the verbosity using the `skip_output` config. You can set whether lefthook should print some parts of its output.

Possible values are `meta,summary,success,failure,execution,execution_out,execution_info,skips`.
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
SourceDirLocal string `mapstructure:"source_dir_local"`
Rc string `mapstructure:"rc,omitempty"`
SkipOutput interface{} `mapstructure:"skip_output,omitempty"`
Output interface{} `mapstructure:"output,omitempty"`
Extends []string `mapstructure:"extends,omitempty"`
NoTTY bool `mapstructure:"no_tty,omitempty"`
AssertLefthookInstalled bool `mapstructure:"assert_lefthook_installed,omitempty"`
Expand Down
19 changes: 14 additions & 5 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

const (
envEnabled = "LEFTHOOK" // "0", "false"
envSkipOutput = "LEFTHOOK_QUIET" // "meta,success,failure,summary,skips,execution,execution_out,execution_info"
envEnabled = "LEFTHOOK" // "0", "false"
envSkipOutput = "LEFTHOOK_QUIET" // "meta,success,failure,summary,skips,execution,execution_out,execution_info"
envOutput = "LEFTHOOK_OUTPUT" // "meta,success,failure,summary,skips,execution,execution_out,execution_info"
)

type RunArgs struct {
Expand Down Expand Up @@ -75,10 +76,18 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
log.SetLevel(log.WarnLevel)
}

newTags := os.Getenv(envOutput)
tags := os.Getenv(envSkipOutput)

var logSettings log.SkipSettings
(&logSettings).ApplySettings(tags, cfg.SkipOutput)
var logSettings log.SettingsInterface

if tags == "" && cfg.SkipOutput == nil {
logSettings = log.NewSettings()
logSettings.ApplySettings(newTags, cfg.Output)
} else {
logSettings = log.NewSkipSettings() //nolint:staticcheck //SA1019: for temporary backward compatibility
logSettings.ApplySettings(tags, cfg.SkipOutput)
}

if !logSettings.SkipMeta() {
log.Box(
Expand Down Expand Up @@ -192,7 +201,7 @@ Run 'lefthook install' manually.`,
func printSummary(
duration time.Duration,
results []run.Result,
logSettings log.SkipSettings,
logSettings log.SettingsInterface,
) {
summaryPrint := log.Separate

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Options struct {
HookName string
GitArgs []string
ResultChan chan Result
SkipSettings log.SkipSettings
SkipSettings log.SettingsInterface
DisableTTY bool
Force bool
Files []string
Expand Down
14 changes: 8 additions & 6 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/lefthook/run/exec"
"github.com/evilmartians/lefthook/internal/log"
)

type TestExecutor struct{}
Expand Down Expand Up @@ -749,12 +750,13 @@ func TestRunAll(t *testing.T) {
executor := TestExecutor{}
runner := &Runner{
Options: Options{
Repo: repo,
Hook: tt.hook,
HookName: tt.hookName,
GitArgs: tt.args,
ResultChan: resultChan,
Force: tt.force,
Repo: repo,
Hook: tt.hook,
HookName: tt.hookName,
SkipSettings: log.NewSettings(),
GitArgs: tt.args,
ResultChan: resultChan,
Force: tt.force,
},
executor: executor,
}
Expand Down
13 changes: 13 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ const (
spinnerText = " waiting"
)

type SettingsInterface interface {
ApplySettings(tags string, skipOutput interface{})
SkipSuccess() bool
SkipFailure() bool
SkipSummary() bool
SkipMeta() bool
SkipExecution() bool
SkipExecutionOutput() bool
SkipExecutionInfo() bool
SkipSkips() bool
SkipEmptySummary() bool
}

type StyleLogger struct {
style lipgloss.Style
}
Expand Down
124 changes: 124 additions & 0 deletions internal/log/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package log

import (
"strings"
)

const (
meta = 1 << iota
success
failure
summary
skips
execution
executionOutput
executionInfo
emptySummary
enableAll = ^0 // Set all bits as 1
)

type Settings int16

func NewSettings() SettingsInterface {
var s Settings
return &s
}

func (s *Settings) ApplySettings(tags string, output interface{}) {
if tags == "" && (output == nil || output == "") {
s.enableAll(true)
return
}

if val, ok := output.(bool); ok {
s.enableAll(val)
return
}

if options, ok := output.([]interface{}); ok {
for _, option := range options {
if optStr, ok := option.(string); ok {
s.applySetting(optStr)
}
}
}

if tags != "" {
for _, tag := range strings.Split(tags, ",") {
s.applySetting(tag)
}
}
}

func (s *Settings) applySetting(setting string) {
switch setting {
case "meta":
*s |= meta
case "success":
*s |= success
case "failure":
*s |= failure
case "summary":
*s |= summary
case "skips":
*s |= skips
case "execution":
*s |= execution
case "execution_out":
*s |= executionOutput
case "execution_info":
*s |= executionInfo
case "empty_summary":
*s |= emptySummary
}
}

func (s *Settings) enableAll(val bool) {
if val {
*s = enableAll // Enable all params
} else {
*s |= skipFailure // Disable all params
}
}

// Checks the state of params.
func (s Settings) isEnable(option int16) bool {
return int16(s)&option != 0
}

// Using `SkipX` to maintain backward compatibility.
func (s Settings) SkipSuccess() bool {
return !s.isEnable(success)
}

func (s Settings) SkipFailure() bool {
return !s.isEnable(failure)
}

func (s Settings) SkipSummary() bool {
return !s.isEnable(summary)
}

func (s Settings) SkipMeta() bool {
return !s.isEnable(meta)
}

func (s Settings) SkipExecution() bool {
return !s.isEnable(execution)
}

func (s Settings) SkipExecutionOutput() bool {
return !s.isEnable(executionOutput)
}

func (s Settings) SkipExecutionInfo() bool {
return !s.isEnable(executionInfo)
}

func (s Settings) SkipSkips() bool {
return !s.isEnable(skips)
}

func (s Settings) SkipEmptySummary() bool {
return !s.isEnable(emptySummary)
}
Loading

0 comments on commit 0e83cf6

Please sign in to comment.