Skip to content

Commit

Permalink
feature: Add an option to disable spinner (#396)
Browse files Browse the repository at this point in the history
Closes #208
  • Loading branch information
mrexox committed Dec 13, 2022
1 parent c5fc306 commit 539b473
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 20 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ jobs:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.47.3
version: v1.50.1
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
Expand All @@ -37,12 +36,9 @@ linters:
- noctx
- nolintlint
- revive
- rowserrcheck
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bench:

bin/golangci-lint:
@test -x $$(go env GOPATH)/bin/golangci-lint || \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.47.3
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.50.1

lint: bin/golangci-lint
$$(go env GOPATH)/bin/golangci-lint run
11 changes: 9 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import (
)

func newRunCmd(opts *lefthook.Options) *cobra.Command {
runArgs := lefthook.RunArgs{}

runCmd := cobra.Command{
Use: "run hook-name [git args...]",
Short: "Execute group of hooks",
Example: "lefthook run pre-commit pre-push",
Example: "lefthook run pre-commit",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// args[0] - hook name
// args[1:] - git hook arguments, number and value depends on the hook
return lefthook.Run(opts, args[0], args[1:])
return lefthook.Run(opts, runArgs, args[0], args[1:])
},
}

runCmd.Flags().BoolVarP(
&runArgs.NoTTY, "no-tty", "n", false,
"run hook non-interactively, disable spinner",
)

return &runCmd
}
15 changes: 15 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Top level options](#top-level-options)
- [`colors`](#colors)
- [`no_tty`](#no-tty)
- [`extends`](#extends)
- [`min_version`](#min_version)
- [`skip_output`](#skip_output)
Expand Down Expand Up @@ -61,6 +62,20 @@ Whether enable or disable colorful output of Lefthook. This option can be overwr
colors: false
```

### `no_tty`

**Default: `false`**

Whether hide spinner and other interactive things. This can be also controlled with `--no-tty` option for `lefthook run` command.

**Example**

```yml
# lefthook.yml

no_tty: true
```

### `extends`

You can extend your config with another one YAML file. Its content will be merged. Extends for `lefthook.yml`, `lefthook-local.yml`, and [`remote`](#remote) configs are handled separately, so you can have different extends in these files.
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
SourceDir string `mapstructure:"source_dir"`
SourceDirLocal string `mapstructure:"source_dir_local"`
Rc string `mapstructure:"rc"`
NoTTY bool `mapstructure:"no_tty"`

Hooks map[string]*Hook
}
Expand Down
21 changes: 17 additions & 4 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ const (
envVerbose = "LEFTHOOK_VERBOSE" // keep all output
)

func Run(opts *Options, hookName string, gitArgs []string) error {
type RunArgs struct {
NoTTY bool
}

func Run(opts *Options, args RunArgs, hookName string, gitArgs []string) error {
lefthook, err := initialize(opts)
if err != nil {
return err
}

return lefthook.Run(hookName, gitArgs)
return lefthook.Run(hookName, args, gitArgs)
}

func (l *Lefthook) Run(hookName string, gitArgs []string) error {
func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
if os.Getenv(envEnabled) == "0" || os.Getenv(envEnabled) == "false" {
return nil
}
Expand Down Expand Up @@ -97,7 +101,16 @@ Run 'lefthook install' manually.`,

startTime := time.Now()
resultChan := make(chan runner.Result, len(hook.Commands)+len(hook.Scripts))
run := runner.NewRunner(l.Fs, l.repo, hook, gitArgs, resultChan, logSettings)

run := runner.NewRunner(
l.Fs,
l.repo,
hook,
gitArgs,
resultChan,
logSettings,
cfg.NoTTY || args.NoTTY,
)

sourceDirs := []string{
filepath.Join(l.repo.RootPath, cfg.SourceDir),
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pre-commit:
t.Setenv(env, value)
}

err = lefthook.Run(tt.hook, tt.gitArgs)
err = lefthook.Run(tt.hook, RunArgs{}, tt.gitArgs)
if err != nil {
if !tt.error {
t.Errorf("unexpected error: %s", err)
Expand Down
17 changes: 11 additions & 6 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Runner struct {
resultChan chan Result
exec Executor
logSettings log.SkipSettings
ttyDisabled bool
}

func NewRunner(
Expand All @@ -47,6 +48,7 @@ func NewRunner(
args []string,
resultChan chan Result,
logSettings log.SkipSettings,
ttyDisabled bool,
) *Runner {
return &Runner{
fs: fs,
Expand All @@ -56,6 +58,7 @@ func NewRunner(
resultChan: resultChan,
exec: CommandExecutor{},
logSettings: logSettings,
ttyDisabled: ttyDisabled,
}
}

Expand All @@ -71,8 +74,10 @@ func (r *Runner) RunAll(hookName string, sourceDirs []string) {
return
}

log.StartSpinner()
defer log.StopSpinner()
if !r.ttyDisabled {
log.StartSpinner()
defer log.StopSpinner()
}

scriptDirs := make([]string, len(sourceDirs))
for _, sourceDir := range sourceDirs {
Expand Down Expand Up @@ -248,7 +253,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
args = append(args, path)
args = append(args, r.args[:]...)

if script.Interactive {
if script.Interactive && !r.ttyDisabled {
log.StopSpinner()
defer log.StartSpinner()
}
Expand All @@ -258,7 +263,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
root: r.repo.RootPath,
args: args,
failText: script.FailText,
interactive: script.Interactive,
interactive: script.Interactive && !r.ttyDisabled,
env: script.Env,
})
}
Expand Down Expand Up @@ -340,7 +345,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
return
}

if command.Interactive {
if command.Interactive && !r.ttyDisabled {
log.StopSpinner()
defer log.StartSpinner()
}
Expand All @@ -350,7 +355,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
root: filepath.Join(r.repo.RootPath, command.Root),
args: args,
failText: command.FailText,
interactive: command.Interactive,
interactive: command.Interactive && !r.ttyDisabled,
env: command.Env,
})
}
Expand Down

0 comments on commit 539b473

Please sign in to comment.