Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash

# Bash completion script for kubectl oadp plugin
# Source this file or add it to your bash completion directory

_kubectl_oadp_complete() {
local cur prev words cword
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words=("${COMP_WORDS[@]}")
cword=$COMP_CWORD

# Find where 'oadp' appears in the command line
local oadp_index=0
for ((i=0; i<${#words[@]}; i++)); do
if [[ "${words[i]}" == "oadp" ]]; then
oadp_index=$i
break
fi
done

# Calculate position relative to 'oadp' command
local pos=$((cword - oadp_index))

case $pos in
1)
# First level subcommands after 'oadp'
COMPREPLY=($(compgen -W "backup restore version client nabsl-request nonadmin na" -- "$cur"))
;;
2)
# Second level - depends on first subcommand
local subcmd="${words[$((oadp_index + 1))]}"
case "$subcmd" in
backup)
COMPREPLY=($(compgen -W "create delete describe download get logs" -- "$cur"))
;;
restore)
COMPREPLY=($(compgen -W "create delete describe get logs" -- "$cur"))
;;
client)
COMPREPLY=($(compgen -W "config" -- "$cur"))
;;
nabsl-request)
COMPREPLY=($(compgen -W "get describe approve reject" -- "$cur"))
;;
nonadmin|na)
COMPREPLY=($(compgen -W "backup bsl" -- "$cur"))
;;
version)
# No subcommands for version
COMPREPLY=()
;;
esac
;;
3)
# Third level - for nonadmin commands
local subcmd="${words[$((oadp_index + 1))]}"
local subsubcmd="${words[$((oadp_index + 2))]}"
case "$subcmd" in
nonadmin|na)
case "$subsubcmd" in
backup)
COMPREPLY=($(compgen -W "create get logs describe delete" -- "$cur"))
;;
bsl)
COMPREPLY=($(compgen -W "create" -- "$cur"))
;;
esac
;;
esac
;;
*)
# For resource names and flags, provide common options
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--help -h --namespace -n --kubeconfig --context --include-resources --exclude-resources --include-namespaces --exclude-namespaces --snapshot-volumes --wait --dry-run -o --output" -- "$cur"))
fi
;;
esac
}

# Register completion for different command forms
complete -F _kubectl_oadp_complete kubectl-oadp
complete -F _kubectl_oadp_complete oadp

# For kubectl plugin integration
_kubectl_oadp() {
_kubectl_oadp_complete
}

# Register with kubectl if available
if command -v kubectl >/dev/null 2>&1; then
complete -F _kubectl_oadp_complete kubectl
fi
48 changes: 48 additions & 0 deletions install_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Installation script for OADP bash completion

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPLETION_SCRIPT="$SCRIPT_DIR/kubectl_oadp_completion.sh"

echo "Installing OADP bash completion..."

# Check if the completion script exists
if [[ ! -f "$COMPLETION_SCRIPT" ]]; then
echo "Error: Completion script not found at $COMPLETION_SCRIPT"
exit 1
fi

# Option 1: Install to user's bash completion directory
USER_COMPLETION_DIR="$HOME/.bash_completion.d"
if [[ ! -d "$USER_COMPLETION_DIR" ]]; then
mkdir -p "$USER_COMPLETION_DIR"
fi

cp "$COMPLETION_SCRIPT" "$USER_COMPLETION_DIR/kubectl_oadp"
echo "Installed to: $USER_COMPLETION_DIR/kubectl_oadp"

# Option 2: Add to .bashrc if not already there
BASHRC="$HOME/.bashrc"
SOURCE_LINE="source $USER_COMPLETION_DIR/kubectl_oadp"

if [[ -f "$BASHRC" ]] && ! grep -q "kubectl_oadp" "$BASHRC"; then
echo "" >> "$BASHRC"
echo "# OADP kubectl plugin completion" >> "$BASHRC"
echo "$SOURCE_LINE" >> "$BASHRC"
echo "Added source line to $BASHRC"
fi

echo ""
echo "Installation complete!"
echo ""
echo "To activate completion in your current shell, run:"
echo " source $USER_COMPLETION_DIR/kubectl_oadp"
echo ""
echo "Or restart your terminal to load it automatically."
echo ""
echo "Test it with:"
echo " kubectl oadp <TAB>"
echo " oc oadp <TAB>"
99 changes: 99 additions & 0 deletions kubectl_complete_oadp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

# Bash completion for kubectl oadp plugin

_kubectl_oadp() {
local cur prev words cword
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Get all words in the command line
words=("${COMP_WORDS[@]}")
cword=$COMP_CWORD

# Handle 'kubectl oadp ...' command structure
case $cword in
2)
# Completing after 'kubectl oadp'
local commands="backup restore version client nabsl-request nonadmin na"
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
;;
3)
# Completing after 'kubectl oadp SUBCOMMAND'
case "${words[2]}" in
backup)
COMPREPLY=($(compgen -W "create delete describe download get logs" -- "$cur"))
;;
restore)
COMPREPLY=($(compgen -W "create delete describe get logs" -- "$cur"))
;;
client)
COMPREPLY=($(compgen -W "config" -- "$cur"))
;;
nabsl-request)
COMPREPLY=($(compgen -W "get describe approve reject" -- "$cur"))
;;
nonadmin|na)
COMPREPLY=($(compgen -W "backup bsl" -- "$cur"))
;;
esac
;;
4)
# Completing after 'kubectl oadp nonadmin/na SUBCOMMAND'
if [[ "${words[2]}" == "nonadmin" || "${words[2]}" == "na" ]]; then
case "${words[3]}" in
backup)
COMPREPLY=($(compgen -W "create get logs describe delete" -- "$cur"))
;;
bsl)
COMPREPLY=($(compgen -W "create" -- "$cur"))
;;
esac
fi
;;
*)
# For flags/options
if [[ "$cur" == -* ]]; then
local flags="--help -h --namespace -n --kubeconfig --context --include-resources --exclude-resources --include-namespaces --exclude-namespaces --snapshot-volumes --wait --dry-run -o --output"
COMPREPLY=($(compgen -W "$flags" -- "$cur"))
fi
;;
esac
}

# Simple test function
test_oadp_completion() {
echo "=== Testing OADP Completion ==="

# Test 1: Basic completion
echo "Test: kubectl oadp <TAB>"
COMP_WORDS=("kubectl" "oadp" "")
COMP_CWORD=2
_kubectl_oadp
echo "Results: ${COMPREPLY[*]}"
echo

# Test 2: Nonadmin completion
echo "Test: kubectl oadp nonadmin <TAB>"
COMP_WORDS=("kubectl" "oadp" "nonadmin" "")
COMP_CWORD=3
_kubectl_oadp
echo "Results: ${COMPREPLY[*]}"
echo

# Test 3: Backup completion
echo "Test: kubectl oadp nonadmin backup <TAB>"
COMP_WORDS=("kubectl" "oadp" "nonadmin" "backup" "")
COMP_CWORD=4
_kubectl_oadp
echo "Results: ${COMPREPLY[*]}"
echo
}

# Register completion for kubectl
complete -F _kubectl_oadp kubectl

echo "OADP bash completion loaded successfully!"
echo "Try typing: kubectl oadp <TAB>"
echo "Run 'test_oadp_completion' to verify it works"
Loading
Loading