-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.shell_functions
More file actions
263 lines (216 loc) · 6.71 KB
/
.shell_functions
File metadata and controls
263 lines (216 loc) · 6.71 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
update_dotfiles() {
cd ~/.dotfiles
make update
cd -
}
# Extract most know archives with one command
extract()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' can not be extract by ex()" ;;
esac
else
echo "'$1' invalid file"
fi
}
# Create a directory and change to it
mkcd(){
mkdir $1 && cd $1
}
# Create a directory and touch a file in one command
mktouch() { mkdir -p $(dirname $1) && touch $1; }
# Perform "ls" after "cd" if successful
cdls() {
builtin cd "$*"
RESULT=$?
if [ "$RESULT" -eq 0 ]; then
ls
fi
}
alias cd=cdls
# Add tab completion for SSH hostnames based on ~/.ssh/config, ignoring wildcards
function _ssh_reload_autocomplete() {
[ -e "~/.ssh/config" ] && complete -o "default" -o "nospace" -W "$(grep "^Host" ~/.ssh/config | grep -v "[?*]" | cut -d " " -f2)" scp sftp ssh
}
_ssh_reload_autocomplete
###> NGTV related ###
ssh-pedro() {
if [ -z "$1" ]; then
echo "Sport center reference is required"
else
ssh -o 'StrictHostKeyChecking no' -o "Port=$((46000+$1))" -L 30098:localhost:8998 -L 30080:localhost:8000 -L 9002:localhost:5900 pedro
fi
}
alias pedro="ssh-pedro"
getList() {
ssh pedrolito /home/ubuntu/getSportCenters.sh
}
alias list="getList"
###< NGTV related ###
###> GH Copilot related ###
ghcs() {
FUNCNAME="$funcstack[1]"
TARGET="shell"
local GH_DEBUG="$GH_DEBUG"
local GH_HOST="$GH_HOST"
read -r -d '' __USAGE <<-EOF
Wrapper around \`gh copilot suggest\` to suggest a command based on a natural language description of the desired output effort.
Supports executing suggested commands if applicable.
USAGE
$FUNCNAME [flags] <prompt>
FLAGS
-d, --debug Enable debugging
-h, --help Display help usage
--hostname The GitHub host to use for authentication
-t, --target target Target for suggestion; must be shell, gh, git
default: "$TARGET"
EXAMPLES
- Guided experience
$ $FUNCNAME
- Git use cases
$ $FUNCNAME -t git "Undo the most recent local commits"
$ $FUNCNAME -t git "Clean up local branches"
$ $FUNCNAME -t git "Setup LFS for images"
- Working with the GitHub CLI in the terminal
$ $FUNCNAME -t gh "Create pull request"
$ $FUNCNAME -t gh "List pull requests waiting for my review"
$ $FUNCNAME -t gh "Summarize work I have done in issues and pull requests for promotion"
- General use cases
$ $FUNCNAME "Kill processes holding onto deleted files"
$ $FUNCNAME "Test whether there are SSL/TLS issues with github.com"
$ $FUNCNAME "Convert SVG to PNG and resize"
$ $FUNCNAME "Convert MOV to animated PNG"
EOF
local OPT OPTARG OPTIND
while getopts "dht:-:" OPT; do
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
debug | d)
GH_DEBUG=api
;;
help | h)
echo "$__USAGE"
return 0
;;
hostname)
GH_HOST="$OPTARG"
;;
target | t)
TARGET="$OPTARG"
;;
esac
done
# shift so that $@, $1, etc. refer to the non-option arguments
shift "$((OPTIND-1))"
TMPFILE="$(mktemp -t gh-copilotXXXXXX)"
trap 'rm -f "$TMPFILE"' EXIT
if GH_DEBUG="$GH_DEBUG" GH_HOST="$GH_HOST" gh copilot suggest -t "$TARGET" "$@" --shell-out "$TMPFILE"; then
if [ -s "$TMPFILE" ]; then
FIXED_CMD="$(cat $TMPFILE)"
print -s -- "$FIXED_CMD"
echo
eval -- "$FIXED_CMD"
fi
else
return 1
fi
}
ghce() {
FUNCNAME="$funcstack[1]"
local GH_DEBUG="$GH_DEBUG"
local GH_HOST="$GH_HOST"
read -r -d '' __USAGE <<-EOF
Wrapper around \`gh copilot explain\` to explain a given input command in natural language.
USAGE
$FUNCNAME [flags] <command>
FLAGS
-d, --debug Enable debugging
-h, --help Display help usage
--hostname The GitHub host to use for authentication
EXAMPLES
# View disk usage, sorted by size
$ $FUNCNAME 'du -sh | sort -h'
# View git repository history as text graphical representation
$ $FUNCNAME 'git log --oneline --graph --decorate --all'
# Remove binary objects larger than 50 megabytes from git history
$ $FUNCNAME 'bfg --strip-blobs-bigger-than 50M'
EOF
local OPT OPTARG OPTIND
while getopts "dh-:" OPT; do
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
debug | d)
GH_DEBUG=api
;;
help | h)
echo "$__USAGE"
return 0
;;
hostname)
GH_HOST="$OPTARG"
;;
esac
done
# shift so that $@, $1, etc. refer to the non-option arguments
shift "$((OPTIND-1))"
GH_DEBUG="$GH_DEBUG" GH_HOST="$GH_HOST" gh copilot explain "$@"
}
###< GH Copilot related ###
###> SOCKS proxy related ###
SSH_SOCKS_ROOT_TARGET="${SSH_SOCKS_ROOT_TARGET:-root@2001:bc8:1d90:256d:dc00:ff:fe2c:4e7d}"
SSH_SOCKS_ROOT_PORT="${SSH_SOCKS_ROOT_PORT:-7000}"
SSH_SOCKS_ROOT_SERVICE="${SSH_SOCKS_ROOT_SERVICE:-Wi-Fi}"
SSH_SOCKS_ROOT_SOCKET="${SSH_SOCKS_ROOT_SOCKET:-${TMPDIR:-/tmp}/ssh-socks-root-${SSH_SOCKS_ROOT_PORT}.sock}"
ssh-socks-root-running() {
[ -S "$SSH_SOCKS_ROOT_SOCKET" ] && ssh -S "$SSH_SOCKS_ROOT_SOCKET" -O check "$SSH_SOCKS_ROOT_TARGET" >/dev/null 2>&1
}
ssh-socks-root() {
local started_tunnel=0
if ! ssh-socks-root-running; then
ssh -fN \
-M \
-S "$SSH_SOCKS_ROOT_SOCKET" \
-o ExitOnForwardFailure=yes \
-D "$SSH_SOCKS_ROOT_PORT" \
"$SSH_SOCKS_ROOT_TARGET" || return 1
started_tunnel=1
fi
if ! networksetup -setsocksfirewallproxy "$SSH_SOCKS_ROOT_SERVICE" 127.0.0.1 "$SSH_SOCKS_ROOT_PORT"; then
if [ "$started_tunnel" -eq 1 ]; then
ssh -S "$SSH_SOCKS_ROOT_SOCKET" -O exit "$SSH_SOCKS_ROOT_TARGET" >/dev/null 2>&1
fi
return 1
fi
networksetup -setsocksfirewallproxystate "$SSH_SOCKS_ROOT_SERVICE" on
}
alias socksroot="ssh-socks-root"
ssh-socks-root-off() {
local exit_code=0
networksetup -setsocksfirewallproxystate "$SSH_SOCKS_ROOT_SERVICE" off || exit_code=$?
if ssh-socks-root-running; then
ssh -S "$SSH_SOCKS_ROOT_SOCKET" -O exit "$SSH_SOCKS_ROOT_TARGET" >/dev/null 2>&1 || exit_code=$?
fi
return $exit_code
}
alias unsocksroot="ssh-socks-root-off"
###< SOCKS proxy related ###