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

function _hr_clean_search_path() {
  local searchPath="$1"
  if [[ "${searchPath:0:2}" == "~/" ]]; then
    searchPath="$HOME/${searchPath:2}"
  fi
  if [[ ! -d "${searchPath}" ]]; then
    searchPath="$(dirname "${searchPath}")"
  fi
  if [[ ! -d "${searchPath}" ]]; then
    searchPath=
  fi
  echo "$searchPath"
}

function _hr_mangle_search_results() {
  local originalSearchPath="$1"
  local -a results=( "${@:2}" )
  if [[ "${originalSearchPath:0:2}" == "~/" ]]; then
    results=( "${results[@]/#$HOME/~}" )
  fi
  echo "${results[@]}"
}

function _hr_chart_folders() {
  local -a chartSources
  local originalSearchPath="${words[CURRENT]}"
  local searchPath="${originalSearchPath}"
  local replacedTilde=false
  searchPath="$(_hr_clean_search_path "$searchPath")"
  chartSources=( $(fd ${searchPath:+--search-path="${searchPath}"} -t f Chart.yaml -X dirname) )
  chartSources=( $(_hr_mangle_search_results "$originalSearchPath" "${chartSources[@]}") )
  _wanted sources expl 'Helm Chart source' \
    _multi_parts -f / chartSources
}

function _hr_chart_tarballs() {
  local -a chartTarballs
  local originalSearchPath="${words[CURRENT]}"
  local searchPath="${originalSearchPath}"
  local replacedTilde=false
  searchPath="$(_hr_clean_search_path "$searchPath")"
  chartTarballs=( $(fd ${searchPath:+--search-path="${searchPath}"} --no-ignore -t f -e tgz -e tar) )
  chartTarballs=( $(for tarball in "${chartTarballs[@]}"; do tar --wildcards -atf "$tarball" \*/Chart.yaml &>/dev/null && echo "$tarball"; done) )
  chartTarballs=( $(_hr_mangle_search_results "$originalSearchPath" "${chartTarballs[@]}") )
  _wanted sources expl 'Helm Chart tarballs' \
    _multi_parts -f / chartTarballs
}

function _hr_charts() {
  _alternative 'chart_folders:Helm Chart source:_hr_chart_folders' \
    'chart_tars:Helm Chart tarballs:_hr_chart_tarballs'
}
function _hr_yamls() {
  local -a hrs
  local originalSearchPath="${words[CURRENT]}"
  local searchPath="${originalSearchPath}"
  searchPath="$(_hr_clean_search_path "$searchPath")"
  hrs=( $(fd ${searchPath:+--search-path="${searchPath}"} -t f -e yaml -e yml -X rg '^kind: HelmRelease$' -l) )
  hrs=( $(_hr_mangle_search_results "$originalSearchPath" "${hrs[@]}") )
  _wanted yamls expl 'HelmRelease yaml' \
    _multi_parts -f / hrs
}
function _hr_helm_opts() {
  local -a command=( ${words[1]:2:l} )
  [[ "${#command}" -eq 0 ]] && command=( template )
  [[ "${command[1]}" == diff ]] && command=( diff upgrade )
  local seperatorIndex=${words[(Ie)--]}
  local -a newWords=( helm "${command[@]}" "${words[$seperatorIndex+1,-1]}" )
  if [[ "${newWords[-1]}" == "" && "$CURRENT" == $(( seperatorIndex + ${#command} )) ]]; then
    newWords[-1]=-
  fi
  local CURRENT=$(( CURRENT - seperatorIndex + ${#command} + 1 ))
  local 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}" ]] || [[ "$firstParam" == - ]]; then
    _alternative 'sources:Helm Chart source:_hr_charts'
  else
    _alternative 'yamls:Flux HelmRelease yaml:_hr_yamls'
  fi
}
function _hr() {
  case "${words[(Ie)--]}" in
    2)
      _arguments "1:seperator:(--)" \
        "*:helm:_hr_helm_opts"
      ;;
    3)
      _arguments "1:first:_hr_one" \
        "2:seperator:(--)" \
        "*:helm:_hr_helm_opts"
      ;;
    4)
      ;&
    0)
      _arguments "1:first:_hr_both" \
        "2:second:_hr_one" \
        "3:seperator:(--)" \
        "*:helm:_hr_helm_opts"
    ;;
  esac
}

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