Skip to content

Commit

Permalink
chore: better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Apr 1, 2024
1 parent 44eb0e3 commit e09d0a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type Command struct {
StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
}

type commandRunReplace struct {
Run string `mapstructure:"run"`
}

func (c Command) Validate() error {
if !isRunnerFilesCompatible(c.Run) {
return errFilesIncompatible
Expand All @@ -44,14 +48,10 @@ func (c Command) DoSkip(gitState git.State) bool {
return skipChecker.Check(gitState, c.Skip, c.Only)
}

func (c Command) GetPriority() int {
func (c Command) ExecutionPriority() int {
return c.Priority
}

type commandRunReplace struct {
Run string `mapstructure:"run"`
}

func mergeCommands(base, extra *viper.Viper) (map[string]*Command, error) {
if base == nil && extra == nil {
return nil, nil
Expand Down
10 changes: 5 additions & 5 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ type Script struct {
StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
}

type scriptRunnerReplace struct {
Runner string `mapstructure:"runner"`
}

func (s Script) DoSkip(gitState git.State) bool {
skipChecker := NewSkipChecker(NewOsExec())
return skipChecker.Check(gitState, s.Skip, s.Only)
}

type scriptRunnerReplace struct {
Runner string `mapstructure:"runner"`
}

func (s Script) GetPriority() int {
func (s Script) ExecutionPriority() int {
return s.Priority
}

Expand Down
32 changes: 16 additions & 16 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewRunner(opts Options) *Runner {

type executable interface {
*config.Command | *config.Script
GetPriority() int
ExecutionPriority() int
}

// RunAll runs scripts and commands.
Expand Down Expand Up @@ -546,45 +546,45 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
}
}

// sortByPriority sorts the names by preceding numbers if they occur and special priority if it is set.
// sortByPriority sorts the tags by preceding numbers if they occur and special priority if it is set.
// If the names starts with letter the command name will be sorted alphabetically.
// If there's a `priority` field defined for a command or script it will be used instead of alphanumeric sorting.
//
// []string{"1_command", "10command", "3 command", "command5"} // -> 1_command, 3 command, 10command, command5
func sortByPriority[E executable](array []string, exe map[string]E) {
sort.SliceStable(array, func(i, j int) bool {
exeI, iOk := exe[array[i]]
exeJ, jOk := exe[array[j]]
func sortByPriority[E executable](tags []string, executables map[string]E) {
sort.SliceStable(tags, func(i, j int) bool {
exeI, okI := executables[tags[i]]
exeJ, okJ := executables[tags[j]]

if iOk && exeI.GetPriority() != 0 || jOk && exeJ.GetPriority() != 0 {
if !iOk || exeI.GetPriority() == 0 {
if okI && exeI.ExecutionPriority() != 0 || okJ && exeJ.ExecutionPriority() != 0 {
if !okI || exeI.ExecutionPriority() == 0 {
return false
}
if !jOk || exeJ.GetPriority() == 0 {
if !okJ || exeJ.ExecutionPriority() == 0 {
return true
}

return exeI.GetPriority() < exeJ.GetPriority()
return exeI.ExecutionPriority() < exeJ.ExecutionPriority()
}

numEnds := -1
for idx, ch := range array[i] {
for idx, ch := range tags[i] {
if unicode.IsDigit(ch) {
numEnds = idx
} else {
break
}
}
if numEnds == -1 {
return array[i] < array[j]
return tags[i] < tags[j]
}
numI, err := strconv.Atoi(array[i][:numEnds+1])
numI, err := strconv.Atoi(tags[i][:numEnds+1])
if err != nil {
return array[i] < array[j]
return tags[i] < tags[j]
}

numEnds = -1
for idx, ch := range array[j] {
for idx, ch := range tags[j] {
if unicode.IsDigit(ch) {
numEnds = idx
} else {
Expand All @@ -594,7 +594,7 @@ func sortByPriority[E executable](array []string, exe map[string]E) {
if numEnds == -1 {
return true
}
numJ, err := strconv.Atoi(array[j][:numEnds+1])
numJ, err := strconv.Atoi(tags[j][:numEnds+1])
if err != nil {
return true
}
Expand Down

0 comments on commit e09d0a9

Please sign in to comment.