Skip to content

Instantly share code, notes, and snippets.

@yriveiro
Forked from raffraffraff/k3d_complete
Created December 20, 2021 17:44
Show Gist options
  • Select an option

  • Save yriveiro/c5064b8e4beddba34e09aa4004714116 to your computer and use it in GitHub Desktop.

Select an option

Save yriveiro/c5064b8e4beddba34e09aa4004714116 to your computer and use it in GitHub Desktop.

Revisions

  1. @raffraffraff raffraffraff revised this gist Feb 6, 2020. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,13 @@
    # Bash completion script for k3d, because I couldn't find one. This is just a quick and dirty
    # completion script I wrote for myself. It may not work on MacOS bash (I don't use Mac, but I
    # know it's got questionable bash/awk).
    # Bash completion script for k3d, because I couldn't find one. Do what you want with it.

    # This should work in zsh as long as you have this in your .zshrc:
    # ```
    # If you're using zsh, this script should work as long as you have these lines in .zshrc:
    #
    # autoload bashcompinit
    # bashcompinit
    # source /path/to/your/bash_completion_file
    # ```
    #

    # functions to grab k3d clusters info
    # functions to grab k3d clusters info [would be nice if k3d list had a simple mode for grabbing names]
    __started-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " running " {gsub(" ","");print $2}'
    @@ -62,6 +60,7 @@ __k3d_command_opts ()
    echo "${complete[*]}"
    }

    # Main k3d completion function
    __k3d_complete ()
    {
    local cur dashoptions prev param
  2. @raffraffraff raffraffraff revised this gist Feb 5, 2020. 1 changed file with 10 additions and 19 deletions.
    29 changes: 10 additions & 19 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ __k3d_complete ()

    # default options
    dashoptions='-h --help -v --version --verbose --timestamp'
    commands='check-tools shell create add-node delete stop start list get-kubeconfig import-images version help'
    k3d_commands='check-tools shell create add-node delete stop start list get-kubeconfig import-images version help'

    # command specific options
    shell_opts='--name --command --shell'
    @@ -89,25 +89,16 @@ __k3d_complete ()
    help_opts=''
    list_opts=''

    case ${#COMP_WORDS[*]} in
    1) COMPREPLY=( $( compgen -W "$dashoptions" -- $cur ) )
    return 0
    ;;
    2) case "$cur" in
    -*) COMPREPLY=( $( compgen -W "$dashoptions" -- $cur ) )
    return 0
    ;;
    *) COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
    return 0
    ;;
    esac
    ;;
    3) COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
    return 0
    ;;
    esac


    case "$prev" in
    k3d)
    COMPREPLY=( $( compgen -W "$k3d_commands" -- $cur ) )
    return 0
    ;;
    check-tools | shell | create | add-node | delete | stop | start | list | get-kubeconfig | import-images)
    COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
    return 0
    ;;
    --name | -n)
    case ${COMP_WORDS[1]} in
    start)
  3. @raffraffraff raffraffraff revised this gist Feb 5, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion k3d_complete
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,8 @@ __k3d_command_opts ()
    ct) command="check-tools";;
    esac
    opts_var="${command//-/_}_opts"
    opts_string="${!opts_var}"
    eval opts_string='$'"$opts_var"
    # opts_string="${!opts_var}" < does not work in zsh
    complete=()
    for opt in $opts_string; do
    __check_existing_compword "${opt}"
  4. @raffraffraff raffraffraff revised this gist Feb 5, 2020. 1 changed file with 38 additions and 36 deletions.
    74 changes: 38 additions & 36 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,15 @@
    # Bash completion script for k3d, because I couldn't find one. This is just a quick and dirty
    # completion script I wrote for myself. It may not work on MacOS bash (I don't use Mac, but I
    # know it's got questionable bash/awk). However, it should work fine in zsh via bashcompinit:
    # know it's got questionable bash/awk).

    # This should work in zsh as long as you have this in your .zshrc:
    # ```
    # autoload bashcompinit
    # bashcompinit
    # source /path/to/your/bash_completion_file
    # ```

    # functions to grab k3d clusters info
    __started-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " running " {gsub(" ","");print $2}'
    @@ -22,8 +25,8 @@ __all-k3d-clusters ()
    k3d list | sed '1,3d;$d' | awk -F'|' '{gsub(" ","");print $2}'
    }

    # function to remove existing comp words
    _in_compwords ()
    # function to check if an opt is already in COMP_WORDS
    __check_existing_compword ()
    {
    for option in ${COMP_WORDS[*]}; do
    if [ "$1" == "$option" ]; then
    @@ -33,7 +36,32 @@ _in_compwords ()
    return 0
    }

    _k3d-complete ()
    # function to lookup command opts and remove duplicates
    __k3d_command_opts ()
    {
    local opt opts_var complete command complete
    command=${COMP_WORDS[1]}
    # translate short commands into full commands
    case $command in
    c) command="create";;
    d|del) command="delete";;
    l|ls) command="list";;
    i) command="import-images";;
    ct) command="check-tools";;
    esac
    opts_var="${command//-/_}_opts"
    opts_string="${!opts_var}"
    complete=()
    for opt in $opts_string; do
    __check_existing_compword "${opt}"
    if [ $? -eq 0 ]; then
    complete+=("$opt")
    fi
    done
    echo "${complete[*]}"
    }

    __k3d_complete ()
    {
    local cur dashoptions prev param

    @@ -73,17 +101,7 @@ _k3d-complete ()
    ;;
    esac
    ;;
    3) COMMAND=${COMP_WORDS[1]}
    case $COMMAND in
    c) COMMAND="create";;
    d|del) COMMAND="delete";;
    l|ls) COMMAND="list";;
    i) COMMAND="import-images";;
    ct) COMMAND="check-tools";;
    *) echo "COMMAND='$COMMAND'";;
    esac
    COMMAND_FUNCTION="${COMMAND//-/_}_opts"
    COMPREPLY=( $(compgen -W "${!COMMAND_FUNCTION}" -- $cur ) )
    3) COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
    return 0
    ;;
    esac
    @@ -132,6 +150,9 @@ _k3d-complete ()
    COMPREPLY=( $( compgen -W "6443" -- $cur ) )
    return 0
    ;;
    --port | -p)
    return 0 # we do not guess port config
    ;;
    --volume | -v)
    case "$cur" in
    *:*) return 0
    @@ -155,26 +176,7 @@ _k3d-complete ()
    esac

    case "$cur" in
    -*) COMMAND=${COMP_WORDS[1]}
    case $COMMAND in
    c) COMMAND="create";;
    d|del) COMMAND="delete";;
    l|ls) COMMAND="list";;
    i) COMMAND="import-images";;
    ct) COMMAND="check-tools";;
    *) echo "COMMAND='$COMMAND'";;
    esac
    OPTIONS_VAR="${COMMAND//-/_}_opts"
    OPTIONS_STRING=${!OPTIONS_VAR}
    COMMAND_OPTIONS=()
    for OPTION in ${OPTIONS_STRING}; do
    _in_compwords "$OPTION"
    if [ $? -eq 0 ]; then
    COMMAND_OPTIONS+=("$OPTION")
    fi
    done

    COMPREPLY=( $(compgen -W "${COMMAND_OPTIONS[*]}" -- $cur ) )
    *) COMPREPLY=( $(compgen -W "$(__k3d_command_opts)" -- $cur ) )
    return 0
    ;;
    esac
    @@ -183,4 +185,4 @@ _k3d-complete ()

    }

    complete -F _k3d-complete k3d
    complete -F __k3d_complete k3d
  5. @raffraffraff raffraffraff revised this gist Feb 4, 2020. 1 changed file with 24 additions and 8 deletions.
    32 changes: 24 additions & 8 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@
    # source /path/to/your/bash_completion_file
    # ```

    # functions to grab k3d running info
    __started-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " running " {gsub(" ","");print $2}'
    @@ -75,29 +74,38 @@ _k3d-complete ()
    esac
    ;;
    3) COMMAND=${COMP_WORDS[1]}
    case $COMMAND in
    c) COMMAND="create";;
    d|del) COMMAND="delete";;
    l|ls) COMMAND="list";;
    i) COMMAND="import-images";;
    ct) COMMAND="check-tools";;
    *) echo "COMMAND='$COMMAND'";;
    esac
    COMMAND_FUNCTION="${COMMAND//-/_}_opts"
    COMPREPLY=( $(compgen -W "${!COMMAND_FUNCTION}" -- $cur ) )
    return 0
    ;;
    esac

    case "$prev" in
    --name | -n)
    case ${COMP_WORDS[1]} in
    start)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__stopped-k3d-clusters)" -- $cur ) )
    return 0
    ;;
    stop)
    ;;
    stop)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__started-k3d-clusters)" -- $cur ) )
    return 0
    ;;
    create)
    ;;
    create|c)
    # sorry, can't help you here
    return 0
    ;;
    *)
    *)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__all-k3d-clusters)" -- $cur ) )
    return 0
    @@ -125,7 +133,7 @@ _k3d-complete ()
    return 0
    ;;
    --volume | -v)
    case "$cur" in
    case "$cur" in
    *:*) return 0
    ;;
    '')
    @@ -148,6 +156,14 @@ _k3d-complete ()

    case "$cur" in
    -*) COMMAND=${COMP_WORDS[1]}
    case $COMMAND in
    c) COMMAND="create";;
    d|del) COMMAND="delete";;
    l|ls) COMMAND="list";;
    i) COMMAND="import-images";;
    ct) COMMAND="check-tools";;
    *) echo "COMMAND='$COMMAND'";;
    esac
    OPTIONS_VAR="${COMMAND//-/_}_opts"
    OPTIONS_STRING=${!OPTIONS_VAR}
    COMMAND_OPTIONS=()
    @@ -164,7 +180,7 @@ _k3d-complete ()
    esac

    return 0

    }

    complete -F _k3d-complete k3d
  6. @raffraffraff raffraffraff revised this gist Feb 4, 2020. 1 changed file with 39 additions and 11 deletions.
    50 changes: 39 additions & 11 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,6 @@
    # source /path/to/your/bash_completion_file
    # ```



    # functions to grab k3d running info
    __started-k3d-clusters ()
    {
    @@ -38,8 +36,7 @@ _in_compwords ()

    _k3d-complete ()
    {
    local cur prev dashoptions commands shell_opts create_opts add_node_opts delete_opts list_opts
    local stop_opts start_opts get_kubeconfig_opts import_images_opts version_opts help_opts
    local cur dashoptions prev param

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    @@ -83,9 +80,8 @@ _k3d-complete ()
    return 0
    ;;
    esac

    case "$prev" in
    --name)
    --name | -n)
    case ${COMP_WORDS[1]} in
    start)
    # show stopped clusters
    @@ -95,7 +91,8 @@ _k3d-complete ()
    stop)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__started-k3d-clusters)" -- $cur ) )
    return 0 ;;
    return 0
    ;;
    create)
    # sorry, can't help you here
    return 0
    @@ -107,15 +104,46 @@ _k3d-complete ()
    ;;
    esac
    ;;
    --wait)
    COMPREPLY=( $( compgen -W "-1 10 30 60 120" -- $cur ) )
    --wait | -t)
    COMPREPLY=( $( compgen -W "-1 15 30 60 120" -- $cur ) )
    return 0
    ;;
    --workers | -w)
    COMPREPLY=( $( compgen -W "2 4 8" -- $cur ) )
    return 0
    ;;
    --workers)
    COMPREPLY=( $( compgen -W "2 4 6 10 15 20" -- $cur ) )
    --registry-name)
    COMPREPLY=( $( compgen -W "registry.local" -- $cur ) )
    return 0
    ;;
    --registry-port)
    COMPREPLY=( $( compgen -W "5000" -- $cur ) )
    return 0
    ;;
    --api-port | -a)
    COMPREPLY=( $( compgen -W "6443" -- $cur ) )
    return 0
    ;;
    --volume | -v)
    case "$cur" in
    *:*) return 0
    ;;
    '')
    COMPREPLY=($( compgen -W '/' -- "$cur" ))
    type compopt &>/dev/null && compopt -o nospace
    return 0
    ;;
    /*)
    COMPREPLY=($( compgen -f -X '/' -- "$cur" ))
    type compopt &>/dev/null && compopt -o nospace
    return 0
    ;;
    esac;
    return 0

    COMPREPLY=( $( compgen -d -X -- $cur ) )
    return 0
    ;;
    esac

    case "$cur" in
  7. @raffraffraff raffraffraff revised this gist Feb 4, 2020. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    # Bash completion script for k3d, because I couldn't find one.
    # This is just a quick and dirty completion script I wrote for myself
    # Bash completion script for k3d, because I couldn't find one. This is just a quick and dirty
    # completion script I wrote for myself. It may not work on MacOS bash (I don't use Mac, but I
    # know it's got questionable bash/awk). However, it should work fine in zsh via bashcompinit:
    # ```
    # autoload bashcompinit
    # bashcompinit
    # source /path/to/your/bash_completion_file
    # ```



    # functions to grab k3d running info
    __started-k3d-clusters ()
    @@ -30,7 +38,8 @@ _in_compwords ()

    _k3d-complete ()
    {
    local cur dashoptions prev param
    local cur prev dashoptions commands shell_opts create_opts add_node_opts delete_opts list_opts
    local stop_opts start_opts get_kubeconfig_opts import_images_opts version_opts help_opts

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
  8. @raffraffraff raffraffraff created this gist Feb 4, 2020.
    133 changes: 133 additions & 0 deletions k3d_complete
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    # Bash completion script for k3d, because I couldn't find one.
    # This is just a quick and dirty completion script I wrote for myself

    # functions to grab k3d running info
    __started-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " running " {gsub(" ","");print $2}'
    }

    __stopped-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '$4 == " stopped " {gsub(" ","");print $2}'
    }

    __all-k3d-clusters ()
    {
    k3d list | sed '1,3d;$d' | awk -F'|' '{gsub(" ","");print $2}'
    }

    # function to remove existing comp words
    _in_compwords ()
    {
    for option in ${COMP_WORDS[*]}; do
    if [ "$1" == "$option" ]; then
    return 1
    fi
    done
    return 0
    }

    _k3d-complete ()
    {
    local cur dashoptions prev param

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

    # default options
    dashoptions='-h --help -v --version --verbose --timestamp'
    commands='check-tools shell create add-node delete stop start list get-kubeconfig import-images version help'

    # command specific options
    shell_opts='--name --command --shell'
    create_opts='--name --volume --port --port-auto-offset --api-port --wait --image --server-arg --agent-arg --env --label --workers --auto-restart --enable-registry --registry-name --registry-port --registries-file'
    add_node_opts='--role --name --count --image --arg --env --volume --k3s --k3s-secret --k3s-token'
    delete_opts='--name --all --prune'
    stop_opts='--name --all'
    start_opts='--name --all'
    get_kubeconfig_opts='--name --all --overwrite'
    import_images_opts='--name --no-remove'

    # commands with no options
    version_opts=''
    help_opts=''
    list_opts=''

    case ${#COMP_WORDS[*]} in
    1) COMPREPLY=( $( compgen -W "$dashoptions" -- $cur ) )
    return 0
    ;;
    2) case "$cur" in
    -*) COMPREPLY=( $( compgen -W "$dashoptions" -- $cur ) )
    return 0
    ;;
    *) COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
    return 0
    ;;
    esac
    ;;
    3) COMMAND=${COMP_WORDS[1]}
    COMMAND_FUNCTION="${COMMAND//-/_}_opts"
    COMPREPLY=( $(compgen -W "${!COMMAND_FUNCTION}" -- $cur ) )
    return 0
    ;;
    esac

    case "$prev" in
    --name)
    case ${COMP_WORDS[1]} in
    start)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__stopped-k3d-clusters)" -- $cur ) )
    return 0
    ;;
    stop)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__started-k3d-clusters)" -- $cur ) )
    return 0 ;;
    create)
    # sorry, can't help you here
    return 0
    ;;
    *)
    # show stopped clusters
    COMPREPLY=( $( compgen -W "$(__all-k3d-clusters)" -- $cur ) )
    return 0
    ;;
    esac
    ;;
    --wait)
    COMPREPLY=( $( compgen -W "-1 10 30 60 120" -- $cur ) )
    return 0
    ;;
    --workers)
    COMPREPLY=( $( compgen -W "2 4 6 10 15 20" -- $cur ) )
    return 0
    ;;

    esac

    case "$cur" in
    -*) COMMAND=${COMP_WORDS[1]}
    OPTIONS_VAR="${COMMAND//-/_}_opts"
    OPTIONS_STRING=${!OPTIONS_VAR}
    COMMAND_OPTIONS=()
    for OPTION in ${OPTIONS_STRING}; do
    _in_compwords "$OPTION"
    if [ $? -eq 0 ]; then
    COMMAND_OPTIONS+=("$OPTION")
    fi
    done

    COMPREPLY=( $(compgen -W "${COMMAND_OPTIONS[*]}" -- $cur ) )
    return 0
    ;;
    esac

    return 0

    }

    complete -F _k3d-complete k3d