Skip to content

Commit c070da4

Browse files
authored
feat(cli): enable cobra autocompletion
1 parent fd31710 commit c070da4

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

internal/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func init() {
6060
Hidden: true,
6161
})
6262

63-
rootCmd.CompletionOptions.DisableDefaultCmd = true
63+
rootCmd.CompletionOptions.DisableDefaultCmd = false
6464

6565
cobra.AddTemplateFunc("typeMap", func(cmds []*cobra.Command) map[string][]*cobra.Command {
6666
m := make(map[string][]*cobra.Command)

scripts/setup.sh

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,117 @@ fi
100100

101101
echo "Done."
102102
echo ""
103-
echo "IMPORTANT: ensure you add /opt/pel/formae/bin to your PATH, and reload your shell configuration"
103+
echo "IMPORTANT: ensure you add /opt/pel/formae/bin to your PATH, and reload your shell configuration"
104+
echo ""
105+
106+
CURRENT_SHELL=$(basename "$SHELL" 2>/dev/null || echo "bash")
107+
108+
setup_bash_completions() {
109+
echo "Setting up bash completions..."
110+
111+
if [[ "$OS" == "darwin" ]]; then
112+
# Check if bash-completion is installed on macOS
113+
if ! brew list bash-completion &>/dev/null && ! brew list bash-completion@2 &>/dev/null; then
114+
echo "Warning: bash-completion is not installed. Install it with:"
115+
echo " brew install bash-completion"
116+
echo "Then add the following to your ~/.bash_profile or ~/.bashrc:"
117+
echo ' [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"'
118+
echo ""
119+
return
120+
fi
121+
fi
122+
123+
# Generate completion file
124+
${INSTALLPREFIX}/formae/bin/formae completion bash > formae-cli-completion.bash 2>/dev/null || {
125+
echo "Warning: Could not generate bash completions. Make sure formae is in your PATH."
126+
return
127+
}
128+
129+
if [[ "$OS" == "darwin" ]]; then
130+
# macOS with Homebrew bash-completion
131+
completion_dir="/usr/local/etc/bash_completion.d"
132+
if [[ -d "$completion_dir" ]]; then
133+
if [[ $(id -u) = 0 ]]; then
134+
cp formae-cli-completion.bash "$completion_dir/formae"
135+
else
136+
sudo cp formae-cli-completion.bash "$completion_dir/formae"
137+
fi
138+
echo "Bash completions installed to $completion_dir"
139+
else
140+
mkdir -p ~/.local/share/bash-completion/completions
141+
cp formae-cli-completion.bash ~/.local/share/bash-completion/completions/formae
142+
echo "Bash completions installed to ~/.local/share/bash-completion/completions"
143+
fi
144+
else
145+
# Linux
146+
mkdir -p ~/.local/share/bash-completion/completions
147+
cp formae-cli-completion.bash ~/.local/share/bash-completion/completions/formae
148+
echo "Bash completions installed to ~/.local/share/bash-completion/completions"
149+
fi
150+
151+
rm -f formae-cli-completion.bash
152+
echo "Bash completions setup complete!"
153+
}
154+
155+
setup_zsh_completions() {
156+
echo "Setting up zsh completions..."
157+
158+
# Create zsh completion directory if it doesn't exist
159+
zsh_completion_dir="${HOME}/.zsh/completions"
160+
mkdir -p "$zsh_completion_dir"
161+
162+
${INSTALLPREFIX}/formae/bin/formae completion zsh > "${zsh_completion_dir}/_formae" 2>/dev/null || {
163+
echo "Warning: Could not generate zsh completions. Make sure formae is in your PATH."
164+
return
165+
}
166+
167+
if ! echo "$FPATH" | grep -q "$zsh_completion_dir"; then
168+
echo "Add the following to your ~/.zshrc:"
169+
echo " fpath=(${zsh_completion_dir} \$fpath)"
170+
echo " autoload -U compinit && compinit"
171+
fi
172+
173+
echo "Zsh completions installed to ${zsh_completion_dir}/_formae"
174+
echo "Zsh completions setup complete!"
175+
}
176+
177+
echo "Shell completions:"
178+
if [[ "$OS" == "darwin" ]]; then
179+
echo "For zsh (default on macOS):"
180+
echo " Add completions with: formae completion zsh > ~/.zsh/completions/_formae"
181+
echo " Then add to ~/.zshrc: fpath=(~/.zsh/completions \$fpath) && autoload -U compinit && compinit"
182+
echo ""
183+
echo "For bash (requires bash-completion via Homebrew):"
184+
echo " First install: brew install bash-completion"
185+
echo " Then add completions with: formae completion bash > /usr/local/etc/bash_completion.d/formae"
186+
else
187+
echo "For bash:"
188+
echo " formae completion bash > ~/.local/share/bash-completion/completions/formae"
189+
echo " mkdir -p ~/.local/share/bash-completion/completions"
190+
echo ""
191+
echo "For zsh:"
192+
echo " mkdir -p ~/.zsh/completions"
193+
echo " formae completion zsh > ~/.zsh/completions/_formae"
194+
echo " Add to ~/.zshrc: fpath=(~/.zsh/completions \$fpath) && autoload -U compinit && compinit"
195+
fi
196+
197+
echo ""
198+
199+
if ! "$skip_prompt"; then
200+
echo "Would you like to install shell completions for $CURRENT_SHELL? (y/N)"
201+
read -r install_completions
202+
if [[ "$install_completions" =~ ^[Yy]$ ]]; then
203+
case "$CURRENT_SHELL" in
204+
bash)
205+
setup_bash_completions
206+
;;
207+
zsh)
208+
setup_zsh_completions
209+
;;
210+
*)
211+
echo "Automatic completion setup not supported for $CURRENT_SHELL"
212+
echo "Please follow the manual instructions above."
213+
;;
214+
esac
215+
fi
216+
fi

0 commit comments

Comments
 (0)