Skip to content

Commit

Permalink
feat: add priorities to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 30, 2023
1 parent 64fefde commit 71c5cef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type Command struct {
Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty" toml:"files,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty" toml:"env,omitempty"`

Root string `mapstructure:"root" yaml:",omitempty" json:"root,omitempty" toml:"root,omitempty"`
Exclude string `mapstructure:"exclude" yaml:",omitempty" json:"exclude,omitempty" toml:"exclude,omitempty"`
Root string `mapstructure:"root" yaml:",omitempty" json:"root,omitempty" toml:"root,omitempty"`
Exclude string `mapstructure:"exclude" yaml:",omitempty" json:"exclude,omitempty" toml:"exclude,omitempty"`
Priority int `mapstructure:"priority" yaml:",omitempty" json:"priority,omitempty" toml:"priority,omitempty"`

FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty" toml:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty" toml:"interactive,omitempty"`
Expand Down
22 changes: 18 additions & 4 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (r *Runner) runScripts(ctx context.Context, dir string) {
continue
}

if script.Interactive {
if script.Interactive && !r.Hook.Piped {
interactiveScripts = append(interactiveScripts, file)
continue
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func (r *Runner) runCommands(ctx context.Context) {
}
}

sortAlnum(commands)
sortAlnum(commands, r.Hook.Commands)

interactiveCommands := make([]string, 0)
var wg sync.WaitGroup
Expand All @@ -343,7 +343,7 @@ func (r *Runner) runCommands(ctx context.Context) {
continue
}

if r.Hook.Commands[name].Interactive {
if r.Hook.Commands[name].Interactive && !r.Hook.Piped {
interactiveCommands = append(interactiveCommands, name)
continue
}
Expand Down Expand Up @@ -536,8 +536,22 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
// If the command names starts with letter the command name will be sorted alphabetically.
//
// []string{"1_command", "10command", "3 command", "command5"} // -> 1_command, 3 command, 10command, command5
func sortAlnum(strs []string) {
func sortAlnum(strs []string, commands map[string]*config.Command) {
sort.SliceStable(strs, func(i, j int) bool {
commandI, iok := commands[strs[i]]
commandJ, jok := commands[strs[j]]

if iok && jok && (commandI.Priority != 0 || commandJ.Priority != 0) {
if commandI.Priority == 0 {
return false
}
if commandJ.Priority == 0 {
return true
}

return commandI.Priority < commandJ.Priority
}

numEnds := -1
for idx, ch := range strs[i] {
if unicode.IsDigit(ch) {
Expand Down

0 comments on commit 71c5cef

Please sign in to comment.