mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-08-06 15:24:25 +00:00
Some checks are pending
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
* refactor(cli): migrate to Cobra framework for shell completion support Replace the hand-rolled ocrFlagSet + switch dispatch with spf13/cobra, enabling native bash/zsh/fish/powershell completion via `ocr completion`. Key changes: - Add root.go (rootCmd definition, version flag with -V shorthand) - Add completion.go (ocr completion [bash|zsh|fish|powershell]) - Add shared_flags.go (reusable flag registration helpers + validation) - Rewrite all *_cmd.go to use cobra.Command with RunE - Delete flags.go (ocrFlagSet, expandShortFlags, parseXxxFlags) - Add compat_test.go (test compatibility wrappers for existing tests) - Promote github.com/spf13/cobra from indirect to direct dependency Behavioral improvements over the previous implementation: - Shell completion for all commands, flags, and enum values - "Did you mean?" suggestions for misspelled commands - cobra.NoArgs on review/scan prevents silent positional arg ignoring - Cleaner error messages on unknown flags (no full flag dump) - Consistent help output format across all subcommands Closes #576 * fix(cli): add Args: cobra.NoArgs to viewerCmd Prevents `ocr viewer localhost:3000` from silently ignoring the positional argument and starting on the default address. Consistent with reviewCmd and scanCmd. * feat(cli): add "Did you mean?" suggestions for misspelled flags Cobra only suggests corrections for unknown subcommands, not flags. Add a levenshtein-distance based suggestion that fires when cobra returns an "unknown flag" error, matching the same UX pattern. Examples: --hel → Did you mean this? --help --audienc → Did you mean this? --audience --comit → Did you mean this? --commit * fix(deps): promote spf13/pflag to direct dependency After the Cobra migration, pflag is directly imported but was still marked as indirect in go.mod, causing CI's go-mod-tidy check to fail. * refactor(cli): use idiomatic cobra patterns for args validation and flag errors Replace hand-rolled argument validation in configSetCmd/configUnsetCmd with cobra.ExactArgs, and move flag typo suggestions from post-hoc error string parsing into SetFlagErrorFunc where cobra provides the command context directly.
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var completionCmd = &cobra.Command{
|
|
Use: "completion [bash|zsh|fish|powershell]",
|
|
Short: "Generate shell completion scripts",
|
|
Long: completionLongHelp,
|
|
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.GenBashCompletionV2(os.Stdout, true)
|
|
case "zsh":
|
|
return rootCmd.GenZshCompletion(os.Stdout)
|
|
case "fish":
|
|
return rootCmd.GenFishCompletion(os.Stdout, true)
|
|
case "powershell":
|
|
return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
|
|
default:
|
|
return fmt.Errorf("unsupported shell: %s", args[0])
|
|
}
|
|
},
|
|
}
|
|
|
|
const completionLongHelp = `Generate shell completion scripts for OCR.
|
|
|
|
To load completions:
|
|
|
|
Bash:
|
|
$ source <(ocr completion bash)
|
|
# To load completions for each session, execute once:
|
|
# Linux:
|
|
$ ocr completion bash > /etc/bash_completion.d/ocr
|
|
# macOS:
|
|
$ ocr completion bash > $(brew --prefix)/etc/bash_completion.d/ocr
|
|
|
|
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:
|
|
$ ocr completion zsh > "${fpath[1]}/_ocr"
|
|
# You will need to start a new shell for this setup to take effect.
|
|
|
|
Fish:
|
|
$ ocr completion fish | source
|
|
# To load completions for each session, execute once:
|
|
$ ocr completion fish > ~/.config/fish/completions/ocr.fish
|
|
|
|
PowerShell:
|
|
PS> ocr completion powershell | Out-String | Invoke-Expression
|
|
# To load completions for every new session, run:
|
|
PS> ocr completion powershell > ocr.ps1
|
|
# and source this file from your PowerShell profile.`
|