Skip to content

Commit

Permalink
filesTypeToFn
Browse files Browse the repository at this point in the history
  • Loading branch information
markovichecha authored and mrexox committed Jun 9, 2022
1 parent b3f5982 commit 6802534
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,22 @@ func (l *Lefthook) executeCommand(commandName string, gitArgs []string, hook *co
filesCommand = command.Files
}

runner := command.Run
if strings.Contains(runner, config.SubStagedFiles) {
files, err := l.repo.StagedFiles()
if err == nil {
runner = strings.ReplaceAll(runner, config.SubStagedFiles, prepareFiles(command, files))
}
}
if strings.Contains(runner, config.SubFiles) || filesCommand != "" {
files, err := git.FilesByCommand(filesCommand)
if err == nil {
runner = strings.ReplaceAll(runner, config.SubFiles, prepareFiles(command, files))
}
filesTypeToFn := map[string]func() ([]string, error){
config.SubStagedFiles: l.repo.StagedFiles,
config.PushFiles: l.repo.PushFiles,
config.SubAllFiles: l.repo.AllFiles,
config.SubFiles: func() ([]string, error) {
return git.FilesByCommand(filesCommand)
},
}
if strings.Contains(runner, config.PushFiles) {
files, err := l.repo.PushFiles()
if err == nil {
runner = strings.ReplaceAll(runner, config.PushFiles, prepareFiles(command, files))
}
}
if strings.Contains(runner, config.SubAllFiles) {
files, err := l.repo.AllFiles()
if err == nil {
runner = strings.ReplaceAll(runner, config.SubAllFiles, prepareFiles(command, files))

runner := command.Run
for filesType, filesFn := range filesTypeToFn {
if strings.Contains(runner, filesType) {
files, err := filesFn()
if err == nil {
runner = strings.ReplaceAll(runner, filesType, prepareFiles(command, files))
}
}
}

Expand Down

0 comments on commit 6802534

Please sign in to comment.