Skip to content

Commit

Permalink
fix by codereview
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Mar 5, 2024
1 parent 0e83cf6 commit 4e6d053
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
16 changes: 9 additions & 7 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,19 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
log.SetLevel(log.WarnLevel)
}

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

var logSettings log.SettingsInterface
var logSettings log.Settings

if tags == "" && cfg.SkipOutput == nil {
if outputSkipTags == "" && cfg.SkipOutput == nil {
logSettings = log.NewSettings()
logSettings.ApplySettings(newTags, cfg.Output)
logSettings.ApplySettings(outputLogTags, cfg.Output)
} else {
log.Warn("skip_output is deprecated, please use output option")

logSettings = log.NewSkipSettings() //nolint:staticcheck //SA1019: for temporary backward compatibility
logSettings.ApplySettings(tags, cfg.SkipOutput)
logSettings.ApplySettings(outputSkipTags, cfg.SkipOutput)
}

if !logSettings.SkipMeta() {
Expand Down Expand Up @@ -201,7 +203,7 @@ Run 'lefthook install' manually.`,
func printSummary(
duration time.Duration,
results []run.Result,
logSettings log.SettingsInterface,
logSettings log.Settings,
) {
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.SettingsInterface
SkipSettings log.Settings
DisableTTY bool
Force bool
Files []string
Expand Down
13 changes: 0 additions & 13 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ 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
45 changes: 29 additions & 16 deletions internal/log/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ const (
enableAll = ^0 // Set all bits as 1
)

type Settings int16
type Settings 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 OutputSettings int16

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

func (s *Settings) ApplySettings(tags string, output interface{}) {
func (s *OutputSettings) ApplySettings(tags string, output interface{}) {
if tags == "" && (output == nil || output == "") {
s.enableAll(true)
return
Expand All @@ -50,7 +63,7 @@ func (s *Settings) ApplySettings(tags string, output interface{}) {
}
}

func (s *Settings) applySetting(setting string) {
func (s *OutputSettings) applySetting(setting string) {
switch setting {
case "meta":
*s |= meta
Expand All @@ -73,7 +86,7 @@ func (s *Settings) applySetting(setting string) {
}
}

func (s *Settings) enableAll(val bool) {
func (s *OutputSettings) enableAll(val bool) {
if val {
*s = enableAll // Enable all params
} else {
Expand All @@ -82,43 +95,43 @@ func (s *Settings) enableAll(val bool) {
}

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

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

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

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

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

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

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

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

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

func (s Settings) SkipEmptySummary() bool {
func (s OutputSettings) SkipEmptySummary() bool {
return !s.isEnable(emptySummary)
}
2 changes: 1 addition & 1 deletion internal/log/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestSetting(t *testing.T) {
},
} { //nolint:dupl // In next versions the `skip_settings_test` will be removed
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
var settings Settings
var settings OutputSettings

(&settings).ApplySettings(tt.tags, tt.settings)

Expand Down
2 changes: 1 addition & 1 deletion internal/log/skip_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
type SkipSettings int16

// Deprecated: Use NewSettings instead.
func NewSkipSettings() SettingsInterface {
func NewSkipSettings() Settings {
var s SkipSettings
return &s
}
Expand Down

0 comments on commit 4e6d053

Please sign in to comment.