-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgshell
More file actions
executable file
·245 lines (209 loc) · 8.15 KB
/
gshell
File metadata and controls
executable file
·245 lines (209 loc) · 8.15 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
VERSION="1.0.0"
ensure_gcloud() {
if ! command -v gcloud >/dev/null 2>&1; then
echo "gcloud not found. Install Google Cloud SDK: https://cloud.google.com/sdk/docs/install"
return 1
fi
# Check cloud-shell subcommand availability
if ! gcloud alpha cloud-shell --help >/dev/null 2>&1; then
echo "Cloud Shell 'alpha' subcommand not available."
read -p "Install alpha components now? [y/N] " ans
if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
echo "Will run: gcloud components install alpha (in background)"
gcloud components install alpha >/dev/null 2>&1 &
echo "Installation started in background. Re-run this command after installation completes."
else
echo "You can enable it with: gcloud components install alpha"
return 1
fi
fi
}
get_md5_local() {
local f="$1"
if command -v md5sum >/dev/null 2>&1; then
md5sum "$f" 2>/dev/null | awk '{print $1}'
elif command -v md5 >/dev/null 2>&1; then
md5 -q "$f" 2>/dev/null
else
# fallback: size:mtime
if stat -c%s "$f" >/dev/null 2>&1; then
printf "%s:%s" "$(stat -c%s "$f")" "$(stat -c%Y "$f")"
else
printf "%s:%s" "$(stat -f%z "$f" 2>/dev/null)" "$(stat -f%m "$f" 2>/dev/null)"
fi
fi
}
get_md5_remote() {
local rel="$1"
gcloud alpha cloud-shell ssh --authorize-session --command \
"if command -v md5sum >/dev/null 2>&1; then md5sum \"~/$rel\" 2>/dev/null | awk '{print \\\$1}'; elif command -v md5 >/dev/null 2>&1; then md5 -q \"~/$rel\" 2>/dev/null; else if stat -c%s \"~/$rel\" >/dev/null 2>&1; then echo \"\$(stat -c%s \"~/$rel\"):\$(stat -c%Y \"~/$rel\")\"; else echo \"\$(stat -f%z \"~/$rel\" 2>/dev/null):\$(stat -f%m \"~/$rel\" 2>/dev/null)\"; fi; fi" 2>/dev/null || true
}
DRY_RUN=0
ensure_gcloud || true
case "$1" in
"-h"|"--help"|"help")
echo ""
echo " gshell — a wrapper around gcloud alpha cloud-shell"
echo ""
echo " Usage:"
echo " gshell Enter Cloud Shell (interactive SSH)"
echo " gshell send [-r] <file|folder> Upload file or folder to Cloud Shell"
echo " gshell get [-r] <file|folder> Download file or folder from Cloud Shell"
echo " gshell sync [-r] <file|folder> Sync only changed files to Cloud Shell"
echo " gshell run <command> Run a command on Cloud Shell"
echo " gshell open Open Cloud Shell in browser"
echo " gshell status Show current gcloud project & account info"
echo " gshell -v | --version Show version"
echo " gshell -h | --help Show this help message"
echo ""
;;
"-v"|"--version"|"version")
echo "gshell v$VERSION"
;;
"send")
shift
RECURSE=""
DRY_RUN=0
while [ "${1:0:1}" = "-" ]; do
case "$1" in
-r) RECURSE="--recurse"; shift ;;
-n|--dry-run) DRY_RUN=1; shift ;;
*) break ;;
esac
done
ITEM=$1
if [ -z "$ITEM" ]; then
echo "Usage: gshell send [-r] [-n|--dry-run] <file_or_folder>"
exit 1
fi
REL_PATH="${ITEM#./}"
REMOTE_DIR=$(dirname "$REL_PATH")
BASENAME=$(basename "$REL_PATH")
echo "Uploading $ITEM to Cloud Shell..."
if [ "$DRY_RUN" -eq 1 ]; then
echo "DRY RUN: gcloud alpha cloud-shell scp $RECURSE \"localhost:$REL_PATH\" \"cloudshell:~/\""
else
gcloud alpha cloud-shell scp $RECURSE \
"localhost:$REL_PATH" \
"cloudshell:~/"
fi
if [ "$DRY_RUN" -eq 0 ]; then
gcloud alpha cloud-shell ssh --authorize-session --command "
if [ \"$REMOTE_DIR\" != \".\" ]; then
mkdir -p \"~/$(printf '%s' "$REMOTE_DIR")\"
if [ ! -e \"~/$(printf '%s' "$REL_PATH")\" ]; then
mv \"~/$(printf '%s' "$BASENAME")\" \"~/$(printf '%s' "$REL_PATH")\"
fi
fi
"
fi
;;
"get")
shift
RECURSE=""
DRY_RUN=0
while [ "${1:0:1}" = "-" ]; do
case "$1" in
-r) RECURSE="--recurse"; shift ;;
-n|--dry-run) DRY_RUN=1; shift ;;
*) break ;;
esac
done
ITEM=$1
if [ -z "$ITEM" ]; then
echo "Usage: gshell get [-r] [-n|--dry-run] <remote_item>"
exit 1
fi
REL_PATH="${ITEM#./}"
LOCAL_DIR=$(dirname "$REL_PATH")
echo "Downloading $ITEM from Cloud Shell..."
mkdir -p "$LOCAL_DIR"
if [ "$DRY_RUN" -eq 1 ]; then
echo "DRY RUN: gcloud alpha cloud-shell scp $RECURSE \"cloudshell:~/$REL_PATH\" \"localhost:$REL_PATH\""
else
gcloud alpha cloud-shell scp $RECURSE \
"cloudshell:~/$REL_PATH" \
"localhost:$REL_PATH"
fi
;;
"status")
echo "Cloud Shell Info:"
echo "-----------------"
echo "Account : $(gcloud config get-value core/account 2>/dev/null)"
echo "Project : $(gcloud config get-value core/project 2>/dev/null)"
echo "Region : $(gcloud config get-value compute/region 2>/dev/null || echo 'not set')"
echo "Zone : $(gcloud config get-value compute/zone 2>/dev/null || echo 'not set')"
;;
"open")
echo "Opening Cloud Shell in browser..."
open "https://shell.cloud.google.com" 2>/dev/null \
|| xdg-open "https://shell.cloud.google.com" 2>/dev/null \
|| echo "Visit: https://shell.cloud.google.com"
;;
"run")
shift
if [ -z "$1" ]; then
echo "Usage: gshell run <command>"
exit 1
fi
echo "Running command on Cloud Shell: $@"
gcloud alpha cloud-shell ssh --authorize-session --command "$*"
;;
"sync")
shift
RECURSE=""
if [ "$1" == "-r" ]; then RECURSE=1; shift; fi
FOLDER=$1
if [ -z "$FOLDER" ]; then
echo "Usage: gshell sync [-r] <file_or_folder>"
exit 1
fi
FOLDER="${FOLDER%/}" # strip trailing slash
echo "Syncing $FOLDER to Cloud Shell..."
sync_file() {
LOCAL_FILE=$1
REL="${LOCAL_FILE#./}"
LOCAL_MD5=$(get_md5_local "$LOCAL_FILE")
REMOTE_MD5=$(get_md5_remote "$REL")
if [ "$LOCAL_MD5" != "$REMOTE_MD5" ]; then
echo " Uploading: $REL"
REMOTE_DIR=$(dirname "$REL")
BASENAME=$(basename "$REL")
if [ "$DRY_RUN" -eq 1 ]; then
echo " DRY RUN: gcloud alpha cloud-shell scp \"localhost:$LOCAL_FILE\" \"cloudshell:~/$BASENAME\""
else
gcloud alpha cloud-shell scp \
"localhost:$LOCAL_FILE" \
"cloudshell:~/$BASENAME"
gcloud alpha cloud-shell ssh --authorize-session --command "
if [ \"$REMOTE_DIR\" != \".\" ]; then
mkdir -p \"~/$(printf '%s' "$REMOTE_DIR")\"
mv \"~/$(printf '%s' "$BASENAME")\" \"~/$(printf '%s' "$REL")\"
fi
"
fi
fi
}
if [ -n "$RECURSE" ]; then
if [ ! -d "$FOLDER" ]; then
echo "Error: $FOLDER is not a valid folder"
exit 1
fi
mapfile -t FILES < <(find "$FOLDER" -type f)
for LOCAL_FILE in "${FILES[@]}"; do
sync_file "$LOCAL_FILE"
done
else
if [ ! -f "$FOLDER" ]; then
echo "Error: $FOLDER is not a valid file. Use -r for folders."
exit 1
fi
sync_file "$FOLDER"
fi
echo "Sync complete!"
;;
*)
gcloud alpha cloud-shell ssh --authorize-session "$@"
;;
esac