summarylogtreecommitdiffstats
path: root/_helmrelease-tools
blob: 1229bf4c9b35fb066717e82488275b5b0c8c12ef (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
#compdef hr hrDiff hrInstall hrUninstall hrUpgrade

function _hr_charts() {
  local -a charts
  local searchPath="${words[CURRENT]}"
  if [[ "${searchPath:0:2}" == "~/" ]]; then
    searchPath="$HOME/${searchPath:2}"
  fi
  if [[ ! -d "${searchPath}" ]]; then
    searchPath="$(dirname "${searchPath}")"
  fi
  charts=( $(fd ${searchPath:+--search-path="${searchPath}"} -t f Chart.yaml -X dirname | sed "s#^$HOME/#~/#g; s#^\./##") )
  _wanted sources expl 'Helm Chart source' \
    _multi_parts / charts
}
function _hr_yamls() {
  local -a hrs
  local searchPath="${words[CURRENT]}"
  if [[ "${searchPath:0:2}" == "~/" ]]; then
    searchPath="$HOME/${searchPath:2}"
  fi
  if [[ ! -d "${searchPath}" ]]; then
    searchPath="$(dirname "${searchPath}")"
  fi
  if [[ ! -d "${searchPath}" ]]; then
    searchPath=
  fi
  hrs=( $(fd ${searchPath:+--search-path="${searchPath}"} -t f -e yaml -e yml -X rg '^kind: HelmRelease$' -l | sed "s#^$HOME/#~/#g; s#^\./##") )
  _wanted yamls expl 'HelmRelease yaml' \
    _multi_parts / hrs
}
function _hr_helm_opts() {
  local command="${words[1]:2:l}"
  [[ -z "$command" ]] && command=template
  local -a newWords
  local seperatorIndex=1
  for index in {1..${#words[@]}}; do
    if [[ "${words[$index]}" == -- ]]; then
      seperatorIndex=$index
      break
    fi
  done

  CURRENT=$(( CURRENT - seperatorIndex + 2 ))
  newWords=( helm $command ${words[$seperatorIndex+1,-1]} )
  if [[ "${words[-1][-1]}" == "" ]]; then
    newWords+=( - )
  fi
  echo "$CURRENT: ${newWords[@]}" >> /tmp/d
  words=( ${newWords[@]} )
  _helm
}
function _hr_both() {
  _alternative 'yamls:Flux HelmRelease yaml:_hr_yamls' \
    'sources:Helm Chart source:_hr_charts'
}
function _hr_one() {
  local firstParam="${words[CURRENT-1]}"
  if [[ "${firstParam:0:2}" == "~/" ]]; then
    firstParam="$HOME/${firstParam:2}"
  fi
  if [[ -f "${firstParam}" ]]; then
    _alternative 'sources:Helm Chart source:_hr_charts'
  else
    _alternative 'yamls:Flux HelmRelease yaml:_hr_yamls'
  fi
}
function _hr() {
  _arguments "1:first:_hr_both" \
    "2:second:_hr_one" \
    "3:seperator:(--)" \
    "*:helm:_hr_helm_opts"
}

case $service in
  hr|hrDiff|hrInstall|hrUninstall|hrUpgrade)
    _hr
    ;;
  *)
    _message "unknown command ${service}" && ret=1
    ;;
esac