Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run --jobs completion #913

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading