summaryrefslogtreecommitdiff
path: root/plugins/shell-proxy/shell-proxy.plugin.zsh
blob: 9d45b52691d756fc726442227916ab54e7faf562 (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
#!/usr/bin/bash
# shellcheck disable=SC1090,SC2154

proxy() {
  # deprecate $DEFAULT_PROXY, use SHELLPROXY_URL instead
  if [[ -n "$DEFAULT_PROXY" && -z "$SHELLPROXY_URL" ]]; then
    echo >&2 "proxy: DEFAULT_PROXY is deprecated, use SHELLPROXY_URL instead"
    SHELLPROXY_URL="$DEFAULT_PROXY"
    unset DEFAULT_PROXY
  fi

  # deprecate CONFIG_PROXY, use SHELLPROXY_CONFIG instead
  if [[ -n "$CONFIG_PROXY" && -z "$SHELLPROXY_CONFIG" ]]; then
    echo >&2 "proxy: CONFIG_PROXY is deprecated, use SHELLPROXY_CONFIG instead"
    SHELLPROXY_CONFIG="$CONFIG_PROXY"
    unset CONFIG_PROXY
  fi

  # the proxy.py script is in the same directory as this function
  local proxy="${functions_source[$0]:A:h}/proxy.py"

  # capture the output of the proxy script and bail out if it fails
  local output
  output="$(SHELLPROXY_URL="$SHELLPROXY_URL" SHELLPROXY_CONFIG="$SHELLPROXY_CONFIG" "$proxy" "$1")" ||
    return $?

  # evaluate the output generated by the proxy script
  source <(echo "$output")
}

_proxy() {
  local -r commands=('enable' 'disable' 'status')
  compset -P '*,'
  compadd -S '' "${commands[@]}"
}

compdef _proxy proxy