Skip to content
This repository

Knife bash completion script #200

Merged
merged 1 commit into from over 1 year ago

2 participants

Jerry Jackson Nathaniel Eliot
Jerry Jackson

I've added a basic bash_completion script for the knife command.; find it at config/knife.bash.

It currently supports most, if not all, vanilla knife commands and further extension will be one of my ongoing projects.

See config/knife.bash.README.md for installation instructions.

Jerry Jackson I've added a basic bash_completion script for the knife command. It c…
…urrently supports all most knife commands and will be an ongoing project. See knife.bash.README.md for installation instructions.
60e1bda
Nathaniel Eliot temujin9 merged commit 8298cbd into from October 23, 2012
Nathaniel Eliot temujin9 closed this October 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 23, 2012
Jerry Jackson I've added a basic bash_completion script for the knife command. It c…
…urrently supports all most knife commands and will be an ongoing project. See knife.bash.README.md for installation instructions.
60e1bda
This page is out of date. Refresh to see the latest.
153  config/knife.bash
... ...
@@ -0,0 +1,153 @@
  1
+# Original Author: Avishai Ish-Shalom <avishai@fewbytes.com>
  2
+# Minor Ironfan-esque additions: Jerry Jackson <jerry.w.jackson@gmail.com>
  3
+
  4
+# first argument set the command level
  5
+_get_knife_completions() {
  6
+    n=$1
  7
+    shift
  8
+    # first argument is knife, so shift it
  9
+    #[ "$1" == "knife" ] && shift
  10
+    local opts
  11
+    opts="$($@ --help | grep -E '^knife' | cut -f$n -d" " | grep -v -E '[][[:upper:].]+' |grep -v '(options)' | sort -u)"
  12
+    _upvar opts "$opts"
  13
+    return 0
  14
+}
  15
+
  16
+_flatten_knife_command() {
  17
+    echo ${words[*]:0:$(( ${#words[*]} -1 ))} | tr ' ' '_'
  18
+}
  19
+# Check cache file for category in file ( passed as $1 ) and run command if cache is empty
  20
+# Designed to be used with _get_knife_completions() and use the opts variables for options
  21
+_completion_cache() {
  22
+    local flag COMMAND
  23
+    local OPTIND=1
  24
+    while getopts "c" flag "$@"; do
  25
+        case $flag in
  26
+        c)
  27
+            COMMAND=yes
  28
+            ;;
  29
+        *)
  30
+            ;;
  31
+        esac
  32
+    done
  33
+    shift $(( $OPTIND - 1 ))
  34
+
  35
+    local CACHE_FILE="$1"
  36
+    shift
  37
+    if [ ! -f "$CACHE_FILE" ]; then
  38
+        if [[ "$COMMAND" == "yes" ]]; then
  39
+            opts=$( eval $@ 2>/dev/null )
  40
+            RET=$?
  41
+        else
  42
+            $@ 2>/dev/null
  43
+            RET=$?
  44
+        fi
  45
+        if [[ $RET -eq 0 ]]; then
  46
+            [ -d "$(dirname "$CACHE_FILE")" ] && echo $opts >"$CACHE_FILE"
  47
+        else
  48
+            unset opts
  49
+        fi
  50
+    else
  51
+        opts=$(cat "$CACHE_FILE")
  52
+    fi
  53
+    _upvar opts "$opts"
  54
+}
  55
+
  56
+_chef_completion_cache() {
  57
+    local CACHE_DIR=${CHEF_HOME:-"$HOME/.chef"}/.completion_cache
  58
+    unset C
  59
+    local C
  60
+    if [[ "$1" == "-c" ]]; then
  61
+        C="-c"
  62
+        shift
  63
+    fi
  64
+    CACHE_FILE="$CACHE_DIR/$1"; shift
  65
+    _completion_cache $C "$CACHE_FILE" "$@"
  66
+    _upvar opts "$opts"
  67
+}
  68
+
  69
+_knife() {
  70
+    if [[ ${cur} =~ /^-/ ]]; then
  71
+        return 0
  72
+    fi
  73
+    local opts cur prev cword words flattened_knife_command
  74
+    _chef_completion_cache knife_commands _get_knife_completions 2 knife
  75
+    _get_comp_words_by_ref cur prev cword words
  76
+    flattened_knife_command=$(_flatten_knife_command)
  77
+    COMPREPLY=()
  78
+    case $flattened_knife_command in
  79
+    *from_file|from_file_*)
  80
+        COMPREPLY=( $(compgen -f -- ${cur} ) )
  81
+        return 0
  82
+        ;;
  83
+    *knife_cookbook_upload|*knife_cookbook_test)
  84
+        local chef_repos
  85
+        if [[ -z $CHEF_REPOS ]]; then
  86
+            CHEF_CONFIG="${CHEF_HOME:-$HOME/.chef}/knife.rb"
  87
+            chef_repos=( $( ruby -e "def cookbook_path(*args); args.each{|a| puts a }; end; eval(File.open('$CHEF_CONFIG','r').lines.select{|l| l =~ /cookbook_path/}.first)" ) )
  88
+        else
  89
+            chef_repos=( ${CHEF_REPOS[@]} )
  90
+        fi
  91
+        if [[ -n "$chef_repos" ]]; then
  92
+            opts=$( ls -1p ${chef_repos[@]} | sort -u | ${SED} -n 's/\/$//p' )
  93
+            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  94
+        fi
  95
+        ;;
  96
+    *knife_data)
  97
+        opts="bag"
  98
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  99
+        return 0
  100
+        ;;
  101
+    *knife_search)
  102
+        _chef_completion_cache -c ${words[0]}_data_bags "${words[0]} data bag list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  103
+        OPTS="node ${opts}"
  104
+        COMPREPLY=( $(compgen -W "${OPTS}" -- ${cur}) )
  105
+        return 0
  106
+        ;;
  107
+    *knife_node_show|*knife_node_edit|*knife_node_delete|*knife_tag_*)
  108
+        _chef_completion_cache -c ${words[0]}_nodes "${words[0]} node list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  109
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  110
+        return 0
  111
+        ;;
  112
+    *knife_role_edit|*knife_role_show|*knife_role_delete)
  113
+        _chef_completion_cache -c ${words[0]}_roles "${words[0]} role list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  114
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  115
+        return 0
  116
+        ;;
  117
+    *knife_data_bag_delete|*knife_data_bag_show|*knife_data_bag_edit)
  118
+        _chef_completion_cache -c ${words[0]}_data_bags "${words[0]} data bag list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  119
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  120
+        return 0
  121
+        ;;
  122
+    *knife_data_bag_delete_*|*knife_data_bag_show_*|*knife_data_bag_edit_*)
  123
+        _chef_completion_cache -c ${words[0]}_data_bag_$prev "${words[0]} data bag show $prev 2>/dev/null|${SED} -r -e 's/[\"\ ,]//g' -e '/^[^0-9A-Za-z._-]+/d'"
  124
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  125
+        return 0
  126
+        ;;
  127
+    *knife_client_list|*knife_client_show|*knife_client_edit)
  128
+        _chef_completion_cache -c ${words[0]}_clients "${words[0]} client list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  129
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  130
+        return 0
  131
+        ;;
  132
+    *knife_environment_show|*knife_environment_edit|*knife_environment_delete)
  133
+        _chef_completion_cache -c ${words[0]}_environments "${words[0]} environment list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'"
  134
+        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  135
+        return 0
  136
+        ;;
  137
+    *)
  138
+        case $cword in
  139
+        1)
  140
+            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  141
+            return 0
  142
+            ;;
  143
+        *)
  144
+            _chef_completion_cache $flattened_knife_command _get_knife_completions $(( $cword + 1 )) ${words[*]}
  145
+            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  146
+            return 0
  147
+            ;;
  148
+        esac
  149
+        ;;
  150
+    esac
  151
+    [[ ${#COMPREPLY[@]} -ge 1 ]] && return 0
  152
+}
  153
+complete -F _knife knife
13  config/knife.bash.README.md
Source Rendered
... ...
@@ -0,0 +1,13 @@
  1
+# knife bash completion
  2
+
  3
+## Installation
  4
+=================
  5
+1)	Place the file knife.bash where bash expects to find it's autocompletion scripts.
  6
+		On most modern unices this would equate to '/etc/bash_completion.d/'. This has
  7
+		been verified in, at least, Ubuntu and Gentoo.
  8
+2)	Restart your shell.
  9
+3)	Enjoy tab-completion of your knife commands.
  10
+
  11
+## Overview
  12
+=================
  13
+Credit goes to the original author; Avishai Ish-Shalom <avishai@fewbytes.com>. I have made and will continue to make needed changes; eg. things present in Ironfan but not in vanilla Chef.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.