Skip to content

Commit

Permalink
feat: run --jobs completion (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
scop authored Jan 9, 2025
1 parent aff6f88 commit 5d94809
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func (run) New(opts *lefthook.Options) *cobra.Command {
return
}

runHookJobCompletions := func(cmd *cobra.Command, args []string, toComplete string) (ret []string, compDir cobra.ShellCompDirective) {
compDir = cobra.ShellCompDirectiveNoFileComp
if len(args) == 0 {
return
}
ret = lefthook.ConfigHookJobCompletions(opts, args[0])
return
}

runCmd := cobra.Command{
Use: "run hook-name [git args...]",
Short: "Execute group of hooks",
Expand Down Expand Up @@ -106,5 +115,7 @@ func (run) New(opts *lefthook.Options) *cobra.Command {

_ = runCmd.RegisterFlagCompletionFunc("commands", runHookCommandCompletions)

_ = runCmd.RegisterFlagCompletionFunc("jobs", runHookJobCompletions)

return &runCmd
}
24 changes: 24 additions & 0 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ func ConfigHookCommandCompletions(opts *Options, hookName string) []string {
return lefthook.configHookCommandCompletions(hookName)
}

func ConfigHookJobCompletions(opts *Options, hookName string) []string {
lefthook, err := initialize(opts)
if err != nil {
return nil
}
return lefthook.configHookJobCompletions(hookName)
}

func (l *Lefthook) configHookCommandCompletions(hookName string) []string {
cfg, err := config.Load(l.Fs, l.repo)
if err != nil {
Expand All @@ -301,6 +309,22 @@ func (l *Lefthook) configHookCommandCompletions(hookName string) []string {
}
}

func (l *Lefthook) configHookJobCompletions(hookName string) []string {
cfg, err := config.Load(l.Fs, l.repo)
if err != nil {
return nil
}
if hook, found := cfg.Hooks[hookName]; !found {
return nil
} else {
jobs := make([]string, 0, len(hook.Jobs))
for _, job := range hook.Jobs {
jobs = append(jobs, job.Name)
}
return jobs
}
}

// parseFilesFromString parses both `\0`-separated files.
func parseFilesFromString(paths string) []string {
var result []string
Expand Down

0 comments on commit 5d94809

Please sign in to comment.