The book picks up where it left off, since cobra-basedGolang AST parses struct fields to automatically generate CRUD code, added some more features. This code generator that automatically moves bricks for me is basically complete. ??
However, when using it in the project, it’s still a little bit flawed, that is, it can’t auto-complete the commands in the fish shell, mainly the commands I created, the commands I can’t remember myself ?. It’s also a bit of a pain in the ass to look at them by typing in the cobra help parameter each time. So I wondered if it is possible to auto-complete commands like smug. I looked it up, and it works:
Add fish autocomplete
cobra has built-in auto-completion for various shells. Such as, fish, bash, zsh, powershell.
For example, my bricklaying tool is called go_snip, and to see how to generate a fish autocomplete configuration, you can enter the command
> go_snip completion fish --help
Generate the autocompletion script for the fish shell.
To load completions in your current shell session:
go_snip completion fish | source
To load completions for every new session, execute once:
go_snip completion fish > ~/.config/fish/completions/go_snip.fish
You will need to start a new shell for this setup to take effect.
Usage:
go_snip completion fish [flags]
Flags:
-h, --help help for fish
--no-descriptions disable completion descriptions
If, however, the fish shell has not been configured for auto-completion before, you need to create a new directory:
mkdir ~/.config/fish/completions/
Then perform the Import Configuration operation:
> go_snip completion fish > ~/.config/fish/completions/go_snip.fish
Then, you can use autocomplete normally.
actual effect
Pressing tab once brings up the command prompt, and pressing tab twice brings up all the commands:
What is the content of the generated fish configuration?
For pure curiosity, open ~/.config/fish/completions/go_snip.fish
The previous function definitions are omitted. It is completely unreadable as if it were a ghost note.
# Remove any pre-existing completions for the program since we will be handling all of them.
complete -c go_snip -e
# this will get called after the two calls below and clear the $__go_snip_perform_completion_once_result global
complete -c go_snip -n '__go_snip_clear_perform_completion_once_result'
# The call to __go_snip_prepare_completions will setup __go_snip_comp_results
# which provides the program's completion choices.
# If this doesn't require order preservation, we don't use the -k flag
complete -c go_snip -n 'not __go_snip_requires_order_preservation && __go_snip_prepare_completions' -f -a '$__go_snip_comp_results'
# otherwise we use the -k flag
complete -k -c go_snip -n '__go_snip_requires_order_preservation && __go_snip_prepare_completions' -f -a '$__go_snip_comp_results'
Do you need to re-import after creating a new command
For example, I’ll add a new subcommand
> cobra-cli add test1
> go install
It is not necessary to import fish again to complete the configuration, the previous configuration supports listing all commands.
The configuration directory structure of fish
> tree ~/.config/fish/
/home/zhongwei/.config/fish/
├── completions
│ └── go_snip.fish
├── config.fish
└── fish_variables
1 directory, 3 files