From 5d94809a58f356f801fbea73f26464314f73f66f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 9 Jan 2025 06:12:20 -0100 Subject: [PATCH] feat: run --jobs completion (#913) --- cmd/run.go | 11 +++++++++++ internal/lefthook/run.go | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cmd/run.go b/cmd/run.go index 2f4aad04..ab5a08ca 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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", @@ -106,5 +115,7 @@ func (run) New(opts *lefthook.Options) *cobra.Command { _ = runCmd.RegisterFlagCompletionFunc("commands", runHookCommandCompletions) + _ = runCmd.RegisterFlagCompletionFunc("jobs", runHookJobCompletions) + return &runCmd } diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 3331de63..575f53a4 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -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 { @@ -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