-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_functions
239 lines (196 loc) · 7.32 KB
/
dot_functions
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
###############################################################################
# #
# This file is managed by Chezmoi. Please edit with `chezmoi edit ~/.zshrc` #
# OR #
# `chezmoi edit --apply-template ~/.zshrc` #
# OR #
# directly in the git repo #
# #
###############################################################################
# functions
# Change cluster and namespace. Although this is better handled by kubecm nowadays.
function ktx {
kubectl config use-context $(kubectl config get-contexts -o name | fzf --select-1 --exit-0 --prompt "Select cluster: ") && kns
}
# Change namespace
function kns {
kubectl config set-context $(kubectl config current-context) \
--namespace=$((kubectl get namespaces -o name | sed 's-namespace/--') \
| fzf -1 -0 --prompt "Select namespace: " --preview="kubectl get pods --namespace={}" --preview-window=down:40)
}
# Collect stats from nodes
function kstats {
kubectl get nodes \
| awk 'FNR > 1 {print $1}' \
| xargs -I {} sh -c 'echo {} ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo '
}
# Connect to a Cisco Anyconnect vpn with fields retrieved from 1Password
# In 1Password create a new item with the following fields:
# - username
# - password
# - authgroup
# - server
# Call the function with the name of the 1Password item as argument
function oc {
$(op item get hoekit --format json |jq -r '.fields[] |("export " + (.label|ascii_downcase) + "=" + .value)')
echo "Touch your Yubikey:"
read "PASSWORD2"
sudo -p "sudo: " openconnect -u ${username} --authgroup=${authgroup} --form-entry "main:password=${password}" --form-entry "main:secondary_password=${PASSWORD2}" ${server}
}
# If your IT department sends a profile with the vpn connection,
# openconnect might be a bit too open. In that case, use the
# official anyconnect client. You have to start the daemon first:
# there are two options:
# - run with sudo from the command line (or use the acd function)
# sudo /opt/cisco/anyconnect/bin/vpnagentd
# - enable vpnagentd as a startup item in your MacOS login items
# there is more than one available item for anyconnect. Pick the
# one that affects all users.
function acc {
$(op item get $1 --format json |jq -r '.fields[] |("export " + (.label|ascii_downcase) + "=" + .value)')
echo $password|pbcopy
echo "================================================================="
echo "= Your group is $authgroup and your username is $username,"
echo "= after the first run, these will be prefilled in the form ="
echo "= I have copied your password. You can paste it on the prompt ="
echo "= For the second password, touch your yubikey ="
echo "================================================================="
/opt/cisco/anyconnect/bin/vpn -s connect $server
echo ""|pbcopy
}
function acd {
sudo /opt/cisco/anyconnect/bin/vpn disconnect
}
# Fix mdfind output
function mdfind() {
/usr/bin/mdfind $@ 2> >(grep --invert-match ' \[UserQueryParser\] ' >&2)
}
function bwu() {
export BW_SESSION=$(bw unlock --raw)
}
function javacerts {
echo $JAVA_HOME
keytool \
-importkeystore \
-srckeystore ~/.local/share/java/cacerts \
-srcstorepass changeit \
--deststorepass changeit \
-destkeystore $JAVA_HOME/lib/security/cacerts \
-noprompt
}
# Github Copilot
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 "$@"
}