-
Notifications
You must be signed in to change notification settings - Fork 0
/
customFunctions
executable file
·297 lines (253 loc) · 8.45 KB
/
customFunctions
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# vim: ft=sh
function extract() # Handy Extract Program
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
# Creates an archive (*.tar.gz) from given directory.
function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
# Create a ZIP archive of a file or folder.
function makezip() { zip -r "${1%%/}.zip" "$1" ; }
# Make your directories and files access rights sane.
function sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
# Function calls cht.sh (awesome cheatsheets!)
function cheat() {
curl cht.sh/$1
}
# Install (one or multiple) selected application(s)
# using "brew search" as source input
# mnemonic [B]rew [I]nstall [P]lugin
bip() {
local inst=$(brew search | fzf -m)
if [[ $inst ]]; then
for prog in $(echo $inst);
do; brew install $prog; done;
fi
}
# Update (one or multiple) selected application(s)
# mnemonic [B]rew [U]pdate [P]lugin
bup() {
local upd=$(brew leaves | fzf -m)
if [[ $upd ]]; then
for prog in $(echo $upd);
do; brew upgrade $prog; done;
fi
}
# Delete (one or multiple) selected application(s)
# mnemonic [B]rew [C]lean [P]lugin (e.g. uninstall)
bcp() {
local uninst=$(brew leaves | fzf -m)
if [[ $uninst ]]; then
for prog in $(echo $uninst);
do; brew uninstall $prog; done;
fi
}
# ==[ Kube stuff ]==
kproxy() {
# Forward the ports to connect to the akorda proxy
kubectl port-forward svc/proxy-svc 8080:8080
}
kc() {
kubectl config get-contexts | tail -n +2 | fzf | cut -c 2- | awk '{print $1}' | xargs kubectl config use-context
}
kimage() {
kubectl get pod $1 -n $2 -o jsonpath='{ .spec.containers[*].image }'
}
# ==[ MISC ]==
conda_activate() {
local selected_env
selected_env=$(ls ~/miniconda3/envs | fzf)
if [ -n "$selected_env" ]; then
source activate $selected_env
fi
}
topcmd() {
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
}
# From https://www.usenix.org/sites/default/files/conference/protected-files/lisa19_maheshwari.pdf
mcd() { mkdir -p $1; cd $1 }
cdl() { cd $1; ls}
backup() { cp "$1"{,.bak};} #test first
gfind() { find / -iname $@ 2>/dev/null }
lfind() { find . -iname $@ 2>/dev/null }
rtfm() { help $@ || man $@ || open "http://www.google.com/search?q=$@"; }
## ==[ 1 Password ]==
### ref: https://austincloud.guru/2018/11/27/1password-cli-tricks/
opon() {
if [[ -z $OP_SESSION_my ]]; then
eval $(op signin my)
fi
}
opoff() {
op signout
unset OP_SESSION_my
}
getpwd() {
opon
op get item "$1" |jq -r '.details.fields[] |select(.designation=="password").value'
}
## ==[ Jira ]==
function create_branch() {
local jq_template query username password branch_name
jq_template='"'\ '\(.key). \(.fields.summary)'\ '\t'\ 'Reporter: \(.fields.reporter.displayName)\n'\ '\(.fields.description)'\ '"'
query='project=ACM AND assignee=currentUser() AND sprint in openSprints()'
username="alberto@akorda.com"
password=$(getpwd jira-api)
branch_name=$(
curl \
--data-urlencode "jql=$query" \
--get \
--user "$username:$password" \
--silent \
--compressed \
'https://akorda.atlassian.net/rest/api/3/search' |
jq -r '.issues[] | "\(.key)\t\(.fields.issuetype.name)\t\(.fields.summary)"' |
fzf |
awk '{printf "%s/%s", tolower($2), tolower($1)}' |
sed -e 's/story/feat/' -e 's/bug/fix/' -e 's/task/feat/'
)
if [ -n "$branch_name" ]; then
git checkout -b "$branch_name"
fi
}
#### TMUX FUNCTIONS
sdev() {
SESSION='dev'
kubectl config use-context "dev"
tmux new-session -s $SESSION -d
tmux new-window -t $SESSION
tmux rename-window -t $SESSION proxy-db-connections
tmux send-keys -t $SESSION "kproxy" C-m
tmux split-window -v
tmux send-keys -t $SESSION "kubectl -n dev port-forward svc/cloudsql-proxy 3306:3306" C-m
tmux split-window -v
tmux send-keys -t $SESSION "kubectl -n qa port-forward svc/cloudsql-proxy 3307:3306" C-m
tmux new-window -t $SESSION
tmux rename-window -t $SESSION es-connections
tmux send-keys -t $SESSION "kubectl -n dev port-forward elasticsearch-master-0 9200:9200" C-m
tmux split-window -v
tmux send-keys -t $SESSION "kubectl -n qa port-forward elasticsearch-master-0 9300:9200" C-m
tmux new-window -t $SESSION
tmux rename-window -t $SESSION temporal-connections
tmux send-keys -t $SESSION "kubectl port-forward -n default svc/temporal-frontend 7233:7233" C-m
tmux new-window -t $SESSION
tmux attach -t $SESSION
}
sprod() {
SESSION='prod'
kubectl config use-context "prod"
tmux new-session -s $SESSION -d
tmux new-window -t $SESSION
tmux rename-window -t $SESSION connections
tmux send-keys -t $SESSION "kproxy" C-m
tmux split-window -v
tmux send-keys -t $SESSION "kubectl port-forward svc/cloudsql-proxy 3306:3306" C-m
tmux split-window -h
tmux send-keys -t $SESSION "kubectl port-forward elasticsearch-master-0 9200:9200" C-m
tmux new-window -t $SESSION
tmux rename-window -t $SESSION temporal-connections
tmux send-keys -t $SESSION "kubectl port-forward -n default svc/temporal-prod-sought-satyr-frontend 7233:7233" C-m
tmux split-window -v
tmux send-keys -t $SESSION "kubectl port-forward -n default svc/temporal-prod-sought-satyr-web 8088:8088" C-m
tmux new-window -t $SESSION
tmux attach -t $SESSION
}
start_session() {
SESSION=$1
if tmux has-session -t $SESSION 2> /dev/null; then
tmux attach -t $SESSION
elif [ "$SESSION" == "dev" ]; then
sdev
elif [ "$SESSION" == "prod" ]; then
sprod
else
tmux new-session -s $SESSION -d
tmux attach -t $SESSION
fi
}
# -----------------------------------------------------------------------------
# AI-powered Git Commit Function
# Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. It:
# 1) gets the current staged changed diff
# 2) sends them to an LLM to write the git commit message
# 3) allows you to easily accept, edit, regenerate, cancel
# But - just read and edit the code however you like
# the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/
gcm() {
# Function to generate commit message
generate_commit_message() {
git diff --cached | llm -m "4o" "
Below is a diff of all staged changes, coming from the command:
\`\`\`
git diff --cached
\`\`\`
Please generate a concise, one-line commit message for these changes."
}
# Function to read user input compatibly with both Bash and Zsh
read_input() {
if [ -n "$ZSH_VERSION" ]; then
echo -n "$1"
read -r REPLY
else
read -p "$1" -r REPLY
fi
}
# Main script
echo "Generating AI-powered commit message..."
commit_message=$(generate_commit_message)
while true; do
echo -e "\nProposed commit message:"
echo "$commit_message"
read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
choice=$REPLY
case "$choice" in
a|A )
if git commit -m "$commit_message"; then
echo "Changes committed successfully!"
return 0
else
echo "Commit failed. Please check your changes and try again."
return 1
fi
;;
e|E )
read_input "Enter your commit message: "
commit_message=$REPLY
if [ -n "$commit_message" ] && git commit -m "$commit_message"; then
echo "Changes committed successfully with your message!"
return 0
else
echo "Commit failed. Please check your message and try again."
return 1
fi
;;
r|R )
echo "Regenerating commit message..."
commit_message=$(generate_commit_message)
;;
c|C )
echo "Commit cancelled."
return 1
;;
* )
echo "Invalid choice. Please try again."
;;
esac
done
}