1
1
#! /usr/bin/env bash
2
-
2
+ # insert_proxy_config.sh - run this after openapi-generator release.sh
3
3
CONFIG_PATH=" ../python_kubernetes/kubernetes/client"
4
4
5
+ # Compute the full file path
5
6
CONFIG_FILE=" $CONFIG_PATH /configuration.py"
6
7
7
- # Normalize
8
+ # --- Normalize Windows-style backslashes to Unix forward slashes ---
8
9
CONFIG_FILE=" $( echo " $CONFIG_FILE " | sed ' s|\\|/|g' ) "
9
10
10
-
11
+ # --- Ensure the target file exists and is writable ---
11
12
if [ ! -f " $CONFIG_FILE " ] || [ ! -w " $CONFIG_FILE " ]; then
12
13
echo " Error: $CONFIG_FILE does not exist or is not writable." >&2
13
14
exit 1
14
15
fi
15
16
16
- # Import OS
17
+ # --- Step 1: Ensure 'import os' follows existing imports (idempotent) ---
17
18
if ! grep -qE ' ^import os$' " $CONFIG_FILE " ; then
18
19
LAST_IMPORT=$( grep -nE ' ^(import |from )' " $CONFIG_FILE " | tail -n1 | cut -d: -f1)
19
20
if [ -n " $LAST_IMPORT " ]; then
30
31
echo " 'import os' already present; no changes made."
31
32
fi
32
33
33
- # Ensure proxy and no_proxy
34
+ # --- Step 2: Insert proxy & no_proxy environment code (idempotent) ---
34
35
if ! grep -q ' os.getenv("HTTPS_PROXY"' " $CONFIG_FILE " ; then
35
36
LINE=$( grep -n " self.proxy = None" " $CONFIG_FILE " | cut -d: -f1)
36
37
if [ -n " $LINE " ]; then
37
38
INDENT=$( sed -n " ${LINE} s/^\( *\).*/\1/p" " $CONFIG_FILE " )
38
39
40
+ # Build the insertion block with correct indentation
39
41
40
42
BLOCK+=" ${INDENT} # Load proxy from environment variables (if set)\n"
41
43
BLOCK+=" ${INDENT} if os.getenv(\" HTTPS_PROXY\" ): self.proxy = os.getenv(\" HTTPS_PROXY\" )\n"
@@ -47,6 +49,7 @@ if ! grep -q 'os.getenv("HTTPS_PROXY"' "$CONFIG_FILE"; then
47
49
BLOCK+=" ${INDENT} if os.getenv(\" NO_PROXY\" ): self.no_proxy = os.getenv(\" NO_PROXY\" )\n"
48
50
BLOCK+=" ${INDENT} if os.getenv(\" no_proxy\" ): self.no_proxy = os.getenv(\" no_proxy\" )"
49
51
52
+ # Insert the block after the proxy default line
50
53
sed -i " ${LINE} a $BLOCK " " $CONFIG_FILE "
51
54
echo " Proxy and no_proxy environment code inserted into $CONFIG_FILE ."
52
55
else
0 commit comments