summaryrefslogtreecommitdiff
path: root/functions/interpreter/vyatta-image-complete
blob: 48110cfc63a4d962249fef07cc1e355a70ea046e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# vyatta bash operational mode image name completions
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006, 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: John Southworth
# Date: 2011
# Description: setup bash completion for image file names
# 
# **** End License ****
_vyatta_image_is_file()
{
  local cur=$1
  if ! [[ ${cur:0:1} =~ "/" ]]; then
    cur=${cur/:/}
    topdir=${cur%%/*}
    cur=${cur#$topdir/}
  fi
  if [[ $topdir == "running" ]]; then
    cur="/${cur}"
  elif [[ $topdir == "disk-install" ]]; then
    cur="/lib/live/mount/persistence/${cur}"
  elif [[ ${cur:0:1} =~ "/" ]]; then
    cur=${cur}
  else
    cur="/lib/live/mount/persistence/boot/${topdir}/rw/${cur}"
  fi
  if [[ -f ${cur} ]]; then
    return 0;
  else
    return 1;
  fi
}

_vyatta_image_file_complete()
{
  if _vyatta_image_is_file ${COMP_WORDS[(( ${#COMP_WORDS[@]}-2 ))]}; then
    _vyatta_op_completions=( "" " " )
    return 0;
  fi

  _vyatta_image_path_complete
}

declare -a non_comps=( "http(s)://<user>:<passwd>@<host>/<file>" \
 "scp://<user>:<passwd>@<host>/<file>" \
 "sftp://<user>:<passwd>@<host>/<file>" \
 "ftp(s)://<user>:<passwd>@<host>/<file>" \
 "tftp://<host>/<file>" )

_vyatta_image_path_complete()
{
  compopt -o nospace
  local -a reply
  if _vyatta_image_is_file $cur ; then
    foundfile=1
    _vyatta_op_completions=( "${cur} " )
    _vyatta_op_noncompletions=( )
    return 0;
  fi
  if [[ ${cur:0:1} =~ "/" ]]; then
    reply=( $(compgen -f ${cur}) )
    for ((i=0; i < ${#reply[@]}; i++)); do
      [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/
    done
    _vyatta_op_completions=( "${reply[@]}" )
    return
  fi
  if [[ ${cur} == "" ]]; then
    reply=( $(compgen -d /lib/live/mount/persistence/boot/ | grep -v grub) )
    for i in `seq 0 $[${#reply[@]}-1]`; do
      file=${reply[$i]}
      reply[$i]=${file/#\/lib\/live\/mount\/persistence\/boot\//}
      reply[$i]="${reply[$i]}://"
    done
    reply+=( "running://" )
    if [[ -d /lib/live/mount/persistence/opt/vyatta/etc/config || -d /lib/live/mount/persistence/config ]]; then
      reply+=( "disk-install://" )
    fi
    _vyatta_op_noncompletions=( "${non_comps[@]}" )
  else 
     _vyatta_op_noncompletions=( )
    if ! [[ $cur =~ .*:\/\/ ]]; then
      if [[ $cur =~ .*:\/ ]]; then
        cur=${cur/:\//}
      fi
      if [[ $cur =~ .*: ]]; then
        cur=${cur/:/}
      fi
      local isrunningimg=$(compgen -W "running" -- ${cur})
      local isdiskinstall=$(compgen -W "disk-install" -- ${cur})
      if [[ $isrunningimg == "running" ]];then
        cur="/"
      elif [[ $isdiskinstall == "disk-install" ]]; then
        cur="/lib/live/mount/persistence/"
      else
        cur="/lib/live/mount/persistence/boot/${cur}"
      fi
      reply=( $(compgen -f ${cur} | grep -v grub) )
      for i in `seq 0 $[${#reply[@]}-1]`; do
        file=${reply[$i]}
        if [[ $isrunningimg == "running" ]];then
          reply[$i]="running://"
        elif [[ $isdiskinstall == "disk-install" ]]; then
          reply[$i]="disk-install://" 
        else
          reply[$i]=${file/#\/lib\/live\/mount\/persistence\/boot\//}
          if [[ -d /lib/live/mount/persistence/boot/${reply[$i]} ]]; then
            reply[$i]="${reply[$i]/#\//}://"
          fi
        fi
      done
    else
      cur=${cur/:/}
      topdir=${cur%%/*}
      cur=${cur#$topdir//}
      if [[ $topdir == "running" ]]; then
        cur="/${cur}"
      elif [[ $topdir == "disk-install" ]]; then
        cur="/lib/live/mount/persistence/${cur}"
      else
        cur="/lib/live/mount/persistence/boot/${topdir}/rw/${cur}"
      fi
      reply=( $(compgen -f ${cur}) )
      # for loop from _filedirs() in /etc/bash_completion
      for ((i=0; i < ${#reply[@]}; i++)); do
        if [[ ${cur:0:1} != "'" ]]; then 
           [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/
           if [[ ${cur:0:1} == '"' ]]; then 
             reply[i]=${reply[i]//\\/\\\\}
             reply[i]=${reply[i]//\"/\\\"}
             reply[i]=${reply[i]//\$/\\\$}
           else
             reply[i]=$(printf %q ${reply[i]})
           fi
        fi
      done
      for i in `seq 0 $[${#reply[@]}-1]`; do
        file=${reply[$i]}
        if [[ $topdir == "running" ]]; then
          reply[$i]=${file/#\//"$topdir://"}
        elif [[ $topdir == "disk-install" ]]; then
          reply[$i]=${file/#\/lib\/live\/mount\/persistence\//"$topdir://"} 
        else
          reply[$i]=${file/#\/lib\/live\/mount\/persistence\/boot\/$topdir/"$topdir://"}
          reply[$i]=${reply[$i]/\/rw\/}
        fi
      done
    fi 
  fi
  _vyatta_op_completions=( "${reply[@]}" )
  return 0
}