From 2d690bbe1c826cc054aa41984f8f61937b2bd723 Mon Sep 17 00:00:00 2001 From: "Mariano Z." Date: Sat, 31 May 2025 17:12:08 -0300 Subject: [PATCH] dev: automated commit - 2025-05-31 17:12:06 --- main.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index c4b7a5c..d98618a 100644 --- a/main.go +++ b/main.go @@ -37,15 +37,15 @@ type Script struct { var config Config -func log(msg string, args ...interface{}) { +func log(msg string, args ...any) { fmt.Printf(Green+"[RUN]"+NC+" "+msg+"\n", args...) } -func warn(msg string, args ...interface{}) { +func warn(msg string, args ...any) { fmt.Printf(Yellow+"[WARN]"+NC+" "+msg+"\n", args...) } -func errorLog(msg string, args ...interface{}) { +func errorLog(msg string, args ...any) { fmt.Fprintf(os.Stderr, Red+"[ERROR]"+NC+" "+msg+"\n", args...) } @@ -352,6 +352,21 @@ var rootCmd = &cobra.Command{ var runCmd = &cobra.Command{ Use: "run [filters...]", Short: "Run scripts matching filters (or all if no filters)", + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + scripts, err := findScripts(nil) + if err != nil { + return nil, cobra.ShellCompDirectiveNoFileComp + } + + var completions []string + for _, script := range scripts { + scriptName := strings.TrimSuffix(script.Name, ".sh") + if strings.HasPrefix(scriptName, toComplete) { + completions = append(completions, scriptName+"\t"+script.Description) + } + } + return completions, cobra.ShellCompDirectiveNoFileComp + }, RunE: func(cmd *cobra.Command, args []string) error { if err := ensureRunsDir(); err != nil { return err @@ -499,6 +514,61 @@ echo "✅ %s completed successfully" }, } +var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: `To load completions: + +Bash: + $ source <(dev completion bash) + + # To load completions for each session, execute once: + # Linux: + $ dev completion bash > /etc/bash_completion.d/dev + # macOS: + $ dev completion bash > $(brew --prefix)/etc/bash_completion.d/dev + +Zsh: + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ dev completion zsh > "${fpath[1]}/_dev" + + # You will need to start a new shell for this setup to take effect. + +Fish: + $ dev completion fish | source + + # To load completions for each session, execute once: + $ dev completion fish > ~/.config/fish/completions/dev.fish + +PowerShell: + PS> dev completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> dev completion powershell > dev.ps1 + # and source this file from your PowerShell profile. +`, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + RunE: func(cmd *cobra.Command, args []string) error { + switch args[0] { + case "bash": + return rootCmd.GenBashCompletion(os.Stdout) + case "zsh": + return rootCmd.GenZshCompletion(os.Stdout) + case "fish": + return rootCmd.GenFishCompletion(os.Stdout, true) + case "powershell": + return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) + } + return nil + }, +} + var depsCmd = &cobra.Command{ Use: "deps", Aliases: []string{"check"}, @@ -514,7 +584,7 @@ func main() { runCmd.Flags().BoolVar(&config.DryRun, "dry", false, "Show what would run without executing") runCmd.Flags().BoolVarP(&config.Verbose, "verbose", "v", false, "Show detailed output during execution") - rootCmd.AddCommand(runCmd, pushCmd, listCmd, newCmd, depsCmd) + rootCmd.AddCommand(runCmd, pushCmd, listCmd, newCmd, depsCmd, completionCmd) if err := rootCmd.Execute(); err != nil { os.Exit(1)