Skip to content

Commit abd3308

Browse files
committed
amp: shell completion
1 parent 0913450 commit abd3308

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ git config core.hooksPath .githooks
4848
chmod +x .githooks/pre-commit
4949
```
5050

51+
### Shell Completions
52+
53+
Generate shell completions:
54+
55+
```bash
56+
dibra completion bash > /usr/local/etc/bash_completion.d/dibra
57+
dibra completion zsh > /usr/local/share/zsh/site-functions/_dibra
58+
dibra completion fish > ~/.config/fish/completions/dibra.fish
59+
```
60+
5161
## Limitations
5262
* Currently supports only Debian/Ubuntu systems.

cmd/controller/main.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func main() {
7878
return
7979
}
8080

81+
if len(os.Args) > 1 && os.Args[1] == "completion" {
82+
if err := runCompletion(os.Args[2:]); err != nil {
83+
fatal("completion failed: %v", err)
84+
}
85+
return
86+
}
87+
8188
validateCommand := false
8289
if len(os.Args) > 1 && os.Args[1] == "validate" {
8390
validateCommand = true
@@ -2810,6 +2817,28 @@ func runSchema(args []string) error {
28102817
}
28112818
}
28122819

2820+
func runCompletion(args []string) error {
2821+
if len(args) == 0 {
2822+
return fmt.Errorf("usage: dibra completion <bash|zsh|fish>")
2823+
}
2824+
if len(args) > 1 {
2825+
return fmt.Errorf("usage: dibra completion <bash|zsh|fish>")
2826+
}
2827+
2828+
switch args[0] {
2829+
case "bash":
2830+
fmt.Print(bashCompletion)
2831+
case "zsh":
2832+
fmt.Print(zshCompletion)
2833+
case "fish":
2834+
fmt.Print(fishCompletion)
2835+
default:
2836+
return fmt.Errorf("usage: dibra completion <bash|zsh|fish>")
2837+
}
2838+
2839+
return nil
2840+
}
2841+
28132842
func normalizeYAML(value interface{}) (interface{}, error) {
28142843
data, err := json.Marshal(value)
28152844
if err != nil {
@@ -2972,6 +3001,120 @@ func sanitizeModuleName(name string) string {
29723001
return clean
29733002
}
29743003

3004+
const bashCompletion = `#!/usr/bin/env bash
3005+
3006+
_dibra_subcommands="init convert schema validate completion"
3007+
_dibra_completion_kinds="bash zsh fish"
3008+
_dibra_schema_subcommands="install upgrade status"
3009+
3010+
_dibra_complete() {
3011+
local cur prev
3012+
cur="${COMP_WORDS[COMP_CWORD]}"
3013+
prev="${COMP_WORDS[COMP_CWORD-1]}"
3014+
3015+
if [[ ${COMP_CWORD} -le 1 ]]; then
3016+
COMPREPLY=( $(compgen -W "${_dibra_subcommands}" -- "${cur}") )
3017+
return 0
3018+
fi
3019+
3020+
case "${COMP_WORDS[1]}" in
3021+
completion)
3022+
COMPREPLY=( $(compgen -W "${_dibra_completion_kinds}" -- "${cur}") )
3023+
return 0
3024+
;;
3025+
schema)
3026+
if [[ ${COMP_CWORD} -eq 2 ]]; then
3027+
COMPREPLY=( $(compgen -W "${_dibra_schema_subcommands}" -- "${cur}") )
3028+
return 0
3029+
fi
3030+
;;
3031+
esac
3032+
3033+
case "${cur}" in
3034+
-*)
3035+
COMPREPLY=( $(compgen -W "--config --force-agent-upload --v --version --agent-path --agent-build --i --inventory --e --extra-vars --validate" -- "${cur}") )
3036+
return 0
3037+
;;
3038+
esac
3039+
}
3040+
3041+
complete -F _dibra_complete dibra
3042+
`
3043+
3044+
const zshCompletion = `#compdef dibra
3045+
3046+
_dibra() {
3047+
local -a commands
3048+
commands=(
3049+
'init:initialize a new CUE project'
3050+
'convert:convert YAML to CUE'
3051+
'schema:install or check schema'
3052+
'validate:validate config and exit'
3053+
'completion:generate shell completions'
3054+
)
3055+
3056+
if (( CURRENT == 2 )); then
3057+
_describe 'command' commands
3058+
return
3059+
fi
3060+
3061+
case "${words[2]}" in
3062+
completion)
3063+
_values 'shell' bash zsh fish
3064+
return
3065+
;;
3066+
schema)
3067+
if (( CURRENT == 3 )); then
3068+
_values 'schema command' install upgrade status
3069+
return
3070+
fi
3071+
;;
3072+
esac
3073+
3074+
_arguments \
3075+
'--config[Path to playbook config file (YAML or CUE)]:file:_files' \
3076+
'--force-agent-upload[Force upload of agent binary]' \
3077+
'--v[Verbose output]' \
3078+
'--version[Print version and exit]' \
3079+
'--agent-path[Path to a pre-built agent binary]:file:_files' \
3080+
'--agent-build[Build agent from source (requires Go)]' \
3081+
'--i[Path to YAML inventory file]:file:_files' \
3082+
'--inventory[Path to YAML inventory file]:file:_files' \
3083+
'--e[Extra variables (key=value or @file.yaml)]' \
3084+
'--extra-vars[Extra variables (key=value or @file.yaml)]' \
3085+
'--validate[Validate config and exit (no execution)]'
3086+
}
3087+
3088+
_dibra "$@"
3089+
`
3090+
3091+
const fishCompletion = `function __dibra_needs_command
3092+
set -l cmd (commandline -opc)
3093+
test (count $cmd) -eq 1
3094+
end
3095+
3096+
complete -c dibra -n '__dibra_needs_command' -f -a 'init' -d 'Initialize a new CUE project'
3097+
complete -c dibra -n '__dibra_needs_command' -f -a 'convert' -d 'Convert YAML to CUE'
3098+
complete -c dibra -n '__dibra_needs_command' -f -a 'schema' -d 'Install or check schema'
3099+
complete -c dibra -n '__dibra_needs_command' -f -a 'validate' -d 'Validate config and exit'
3100+
complete -c dibra -n '__dibra_needs_command' -f -a 'completion' -d 'Generate shell completions'
3101+
3102+
complete -c dibra -n '__fish_seen_subcommand_from completion' -f -a 'bash zsh fish'
3103+
complete -c dibra -n '__fish_seen_subcommand_from schema' -f -a 'install upgrade status'
3104+
3105+
complete -c dibra -l config -r -d 'Path to playbook config file (YAML or CUE)'
3106+
complete -c dibra -l force-agent-upload -d 'Force upload of agent binary'
3107+
complete -c dibra -l v -d 'Verbose output'
3108+
complete -c dibra -l version -d 'Print version and exit'
3109+
complete -c dibra -l agent-path -r -d 'Path to a pre-built agent binary'
3110+
complete -c dibra -l agent-build -d 'Build agent from source (requires Go)'
3111+
complete -c dibra -l i -r -d 'Path to YAML inventory file'
3112+
complete -c dibra -l inventory -r -d 'Path to YAML inventory file'
3113+
complete -c dibra -l e -r -d 'Extra variables (key=value or @file.yaml)'
3114+
complete -c dibra -l extra-vars -r -d 'Extra variables (key=value or @file.yaml)'
3115+
complete -c dibra -l validate -d 'Validate config and exit (no execution)'
3116+
`
3117+
29753118
func renderArgs(args map[string]interface{}, context map[string]interface{}) (map[string]interface{}, error) {
29763119
rendered, err := vars.RenderValue(args, context)
29773120
if err != nil {

0 commit comments

Comments
 (0)