aboutsummaryrefslogtreecommitdiff
path: root/lib/keyringer/completions/bash
blob: 3c082934cc04c7ffbe8551f2ea0b982544c01305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#
# Keyringer bash completion
#

# Completion for git subcommand
_keyringer_git_complete() {
  if [ -e "/etc/bash_completion.d/git" ]; then
    (
      source /etc/bash_completion.d/git
      cd $path
      COMP_WORDS=(git $*)
      COMP_CWORD=$((${#COMP_WORDS[*]} - 1))

      if [ "$COMP_CWORD" == "0" ]; then
        COMP_CWORD=1
      fi

      _git
      echo ${COMPREPLY[@]}
    )
  fi
}

_keyringer() {
  # Standard stuff
  local cur prev opts config
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  command="${COMP_WORDS[2]}"

  # Initial options
  config="$HOME/.keyringer"
  keyrings="`ls $config | sed -e 's/config//'`"

  # Process config
  source $config/config
  path="`eval echo '$'$instance`"

  # Available instances
  instances="`echo $keyrings | sed -e 's/ /|/'`"

  # The current instance
  instance="${COMP_WORDS[1]}"

  # Command completions
  if [ "${#COMP_WORDS[@]}" == "2" ]; then
    opts="$keyrings"
  elif [ "${#COMP_WORDS[@]}" == "3" ] && echo "${prev}" | grep -qe "[$instances]"; then
    opts="`keyringer $instance commands`"
  elif [ "${#COMP_WORDS[@]}" == "4" ]; then
    case "${prev}" in
      options|preferences)
        opts="ls edit add"
        ;;
      recipients)
        opts="ls edit"
        ;;
      ls|encrypt|encrypt-batch|decrypt|edit|append|append-batch|del|recrypt)
        opts="$(bash -c "set -f && keyringer $instance ls -p -d ${cur}*" 2> /dev/null)"
        ;;
      genpair)
        opts="gpg ssh ssl ssl-self"
        ;;
      git)
        opts="$(_keyringer_git_complete ${cur})"
        ;;
      *)
        ;;
    esac
  elif [ "${#COMP_WORDS[@]}" == "5" ]; then
    case "${command}" in
      recipients)
        opts="$(cd $path/config/recipients && ls -p ${cur}* 2> /dev/null)"
        ;;
      genpair)
        opts="$(bash -c "set -f && keyringer $instance ls -p -d ${cur}*" 2> /dev/null)"
        ;;
      git)
        # TODO
        opts="$(_keyringer_git_complete ${prev} ${cur})"
        ;;
      *)
        ;;
    esac
  elif [ "${command}" == "git" ]; then
    # TODO
    true
  fi

  # Avoid annoying bell and extra tab
  bind 'set show-all-if-ambiguous on'

  # Return the available options
  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  [[ $COMPREPLY == */ ]] && compopt -o nospace
  return 0
}

complete -F _keyringer keyringer