e96b8bd52366b526d7ebb22d947b54d286d6da61

Author
fengkx <liangkx8237@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

refactor(shell-proxy)!: rename env vars to `SHELLPROXY_*` and add usage message (#10456)

BREAKING CHANGE: the `DEFAULT_PROXY` setting has been renamed to `SHELLPROXY_URL`,
and `CONFIG_PROXY` has been renamed to `SHELLPROXY_CONFIG`. See the plugin README
for more information.

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

  1diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt
  2index 76e9fdf318ed0b0f403af755ad59c3792bf51257..533ae49c09b189fcf786122f106d9dbfa6fe5a64 100644
  3--- a/.github/actions/spelling/expect.txt
  4+++ b/.github/actions/spelling/expect.txt
  5@@ -3456,6 +3456,7 @@ sheerun
  6 shellcheck
  7 shellinit
  8 shellperson
  9+SHELLPROXY
 10 shitload
 11 SHLVL
 12 shm
 13diff --git a/plugins/shell-proxy/README.md b/plugins/shell-proxy/README.md
 14index 6f2cd13e0685cb7b8d41009557444aa561fdb9ac..b19888c565289690b96e8f4b3e30c8775f921e71 100644
 15--- a/plugins/shell-proxy/README.md
 16+++ b/plugins/shell-proxy/README.md
 17@@ -1,42 +1,56 @@
 18-# Shell Proxy oh-my-zsh plugin
 19+# shell-proxy plugin
 20 
 21-This a pure user-space program, shell-proxy setter, written Python3 and Bash.
 22+This a pure user-space program, shell-proxy setter, written in Python3 and Zsh.
 23 
 24-100% only no side-effects, only effect **environment variables** and **aliases**
 25+To use it, add `shell-proxy` to the plugins array in your zshrc file:
 26 
 27-## Key feature
 28+```zsh
 29+plugins=(... shell-proxy)
 30+```
 31+
 32+## Key features
 33 
 34-- Support Ubuntu, Archlinux, etc (Linux)
 35-- Support macOS
 36-- Support git via based-`$GIT_SSH`
 37-- Support ssh, sftp, scp, slogin and ssh-copy-id via based-`alias`
 38-- Built-in Auto-complete
 39+- Supports macOS and Linux (Ubuntu, Archlinux, etc.)
 40+- Supports git via setting `$GIT_SSH`
 41+- Supports ssh, sftp, scp, slogin and ssh-copy-id via setting aliases
 42+- Built-in autocomplete
 43 
 44 ## Usage
 45 
 46-Method 1:
 47+### Method 1
 48 
 49-`$DEFAULT_PROXY` is the proxy URL you will set
 50+Set `SHELLPROXY_URL` environment variable to the URL of the proxy server:
 51 
 52-Method 2:
 53+```sh
 54+SHELLPROXY_URL="http://127.0.0.1:8123"
 55+proxy enable
 56+```
 57 
 58-Write a program to `$HOME/.config/proxy` in the file.
 59+### Method 2
 60 
 61-Example program:
 62+Write a program file in `$HOME/.config/proxy` so that the proxy URL is defined dynamically.
 63+Note that the program file must be executable.
 64 
 65-```bash
 66+Example:
 67+
 68+```sh
 69 #!/bin/bash
 70-# The file path: $HOME/.config/proxy
 71-if [[ "$OSTYPE" == "darwin"* ]]; then
 72+
 73+if [[ "$(uname)" = Darwin ]]; then
 74   echo "http://127.0.0.1:6152" # Surge Mac
 75 else
 76   echo "http://127.0.0.1:8123" # polipo
 77 fi
 78 ```
 79 
 80-Method 3:
 81+### Method 3
 82 
 83-The working path of **Method 2** can be changed via `$CONFIG_PROXY`
 84+Use [method 2](#method-2) but define the location of the program file by setting the
 85+`SHELLPROXY_CONFIG` environment variable:
 86+
 87+```sh
 88+SHELLPROXY_CONFIG="$HOME/.dotfiles/proxy-config"
 89+```
 90 
 91 ## Reference
 92 
 93@@ -45,8 +59,4 @@ The working path of **Method 2** can be changed via `$CONFIG_PROXY`
 94 
 95 ## Maintainer
 96 
 97-- <https://github.com/septs>
 98-
 99-## The oh-my-zsh plugin (shell-proxy)
100-
101-Public Domain
102+- [@septs](https://github.com/septs)
103diff --git a/plugins/shell-proxy/proxy.py b/plugins/shell-proxy/proxy.py
104index 97f4cf8738a51b68e8344267cdf5abadce1a4ec8..14f2944cc9707fc01dc44ee54e85d7e6bc71893e 100755
105--- a/plugins/shell-proxy/proxy.py
106+++ b/plugins/shell-proxy/proxy.py
107@@ -5,16 +5,22 @@ from subprocess import check_output, list2cmdline
108 
109 cwd = os.path.dirname(__file__)
110 ssh_agent = os.path.join(cwd, "ssh-agent.py")
111-user_proxy = os.environ.get("CONFIG_PROXY", os.path.expandvars("$HOME/.config/proxy"))
112+proxy_env = "SHELLPROXY_URL"
113+proxy_config = os.environ.get("SHELLPROXY_CONFIG") or os.path.expandvars("$HOME/.config/proxy")
114 
115+usage="""shell-proxy: no proxy configuration found.
116+
117+Set `{env}` or create a config file at `{config}`
118+See the plugin README for more information.""".format(env=proxy_env, config=proxy_config)
119 
120 def get_http_proxy():
121-    default_proxy = os.environ.get("DEFAULT_PROXY")
122+    default_proxy = os.environ.get(proxy_env)
123     if default_proxy:
124         return default_proxy
125-    if os.path.isfile(user_proxy):
126-        return check_output(user_proxy).decode("utf-8").strip()
127-    raise Exception("Not found, Proxy configuration")
128+    if os.path.isfile(proxy_config):
129+        return check_output(proxy_config).decode("utf-8").strip()
130+    print(usage, file=sys.stderr)
131+    sys.exit(1)
132 
133 
134 def make_proxies(url: str):
135@@ -53,8 +59,7 @@ class CommandSet:
136             cmdline("echo", _)
137 
138     def usage(self):
139-        cmdline("echo", "usage: proxy {enable,disable,status}")
140-        self.status()
141+        print("usage: proxy {enable,disable,status}", file=sys.stderr)
142 
143 
144 def cmdline(*items):
145@@ -65,7 +70,7 @@ def main():
146     command = CommandSet()
147     if len(sys.argv) == 1:
148         command.usage()
149-        sys.exit(-1)
150+        sys.exit(1)
151     getattr(command, sys.argv[1], command.usage)()
152 
153 
154diff --git a/plugins/shell-proxy/shell-proxy.plugin.zsh b/plugins/shell-proxy/shell-proxy.plugin.zsh
155index 315ade665c15a7ab899728ab5dd10095e66d1ffa..9d45b52691d756fc726442227916ab54e7faf562 100644
156--- a/plugins/shell-proxy/shell-proxy.plugin.zsh
157+++ b/plugins/shell-proxy/shell-proxy.plugin.zsh
158@@ -1,16 +1,37 @@
159 #!/usr/bin/bash
160-# shellcheck disable=SC1090
161-
162-__PROXY__="${0:A:h}/proxy.py"
163+# shellcheck disable=SC1090,SC2154
164 
165 proxy() {
166-	source <(env "DEFAULT_PROXY=$DEFAULT_PROXY" "$__PROXY__" "$1")
167+  # deprecate $DEFAULT_PROXY, use SHELLPROXY_URL instead
168+  if [[ -n "$DEFAULT_PROXY" && -z "$SHELLPROXY_URL" ]]; then
169+    echo >&2 "proxy: DEFAULT_PROXY is deprecated, use SHELLPROXY_URL instead"
170+    SHELLPROXY_URL="$DEFAULT_PROXY"
171+    unset DEFAULT_PROXY
172+  fi
173+
174+  # deprecate CONFIG_PROXY, use SHELLPROXY_CONFIG instead
175+  if [[ -n "$CONFIG_PROXY" && -z "$SHELLPROXY_CONFIG" ]]; then
176+    echo >&2 "proxy: CONFIG_PROXY is deprecated, use SHELLPROXY_CONFIG instead"
177+    SHELLPROXY_CONFIG="$CONFIG_PROXY"
178+    unset CONFIG_PROXY
179+  fi
180+
181+  # the proxy.py script is in the same directory as this function
182+  local proxy="${functions_source[$0]:A:h}/proxy.py"
183+
184+  # capture the output of the proxy script and bail out if it fails
185+  local output
186+  output="$(SHELLPROXY_URL="$SHELLPROXY_URL" SHELLPROXY_CONFIG="$SHELLPROXY_CONFIG" "$proxy" "$1")" ||
187+    return $?
188+
189+  # evaluate the output generated by the proxy script
190+  source <(echo "$output")
191 }
192 
193 _proxy() {
194-	local -r commands=('enable' 'disable' 'status')
195-	compset -P '*,'
196-	compadd -S '' "${commands[@]}"
197+  local -r commands=('enable' 'disable' 'status')
198+  compset -P '*,'
199+  compadd -S '' "${commands[@]}"
200 }
201 
202-compdef '_proxy' 'proxy'
203+compdef _proxy proxy