-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextra_shrc
287 lines (246 loc) · 8.41 KB
/
extra_shrc
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
#!/usr/bin/env bash
#makes some shortcuts available for vim instead for shell
stty -ixon
command -v bat >/dev/null 2>&1 && alias cat=bat
# General, safe setup for bash/zsh acc. to https://direnv.net/ (section Setup)
command -v direnv >/dev/null 2>&1 && eval "$(direnv hook "$(basename "$SHELL")")"
command -v thefuck >/dev/null 2>&1 && eval "$(thefuck --alias)"
for dir in ~/.local/pipx/venvs; do
file="$dir"/virtualenvwrapper/bin/virtualenvwrapper.sh
if [ -f "$file" ]; then
. "$file"
break
fi
done
#http://askubuntu.com/questions/362280/enter-ssh-passphrase-once
SSH_ENV=$HOME/.ssh/env
mkdir -p ~/.ssh
start_agent() {
# spawn ssh-agent
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}
_ssh_cleanup() {
pkill ssh-agent
rm "$SSH_ENV"
}
trap _ssh_cleanup SIGINT
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent
fi
trap - SIGINT
man() {
# Colored man pages
# https://github.com/Bash-it/bash-it/blob/867b0fd11c6a3ee8c399d963ccfbb942bac74bb6/plugins/available/man.plugin.bash
LESS_TERMCAP_mb=$'\e[1;32m' \
LESS_TERMCAP_md=$'\e[1;32m' \
LESS_TERMCAP_me=$'\e[0m' \
LESS_TERMCAP_se=$'\e[0m' \
LESS_TERMCAP_so=$'\e[01;33m' \
LESS_TERMCAP_ue=$'\e[0m' \
LESS_TERMCAP_us=$'\e[1;4;31m' \
/usr/bin/man \
"$@"
}
installed() {
# check for installed package; all options passed to fzf
dpkg --list | fzf --multi "$@"
}
backup_reminder() {
# nag me if last backup was more than two weeks ago
local backup_dates_file=~/.config/borg/backup_dates
local today=$(date +%s)
if [[ -e $backup_dates_file ]]; then
local last_backup=$(tail -n1 $backup_dates_file)
if [[ $(($today - $last_backup)) -gt 1209600 ]]; then
echo -e "\033[0;31m!!! >>> TIME TO BACKUP <<< !!!\n\033[0m"
fi
fi
}
backup_reminder
wt() {
# Watson log of Today
watson log --day --no-pager --current "$@"
}
wy() {
# Watson log of Yesterday
local yesterdate=$(date --date yesterday +%F)
watson log --from $yesterdate --to $yesterdate --no-pager
}
wb() {
# Watson report of Boxtribute activities
local month lastday
month=${1:-$(date +%m)}
year=${2:-$(date +%Y)}
lastday=$(date -d "$month/1 + 1 month - 1 day" "+%d")
watson report --no-pager --from "$year"-"$month"-01 --to "$year"-"$month-$lastday" --project boxtribute --tag 2024
printf "\n"
watson report --no-pager --json --from "$year"-"$month"-01 --to "$year"-"$month-$lastday" --project boxtribute \
--tag general-consolidation \
--tag assort \
--tag aid-sending \
--tag viz | jq -r '.projects[].tags[] | "\(.name)=\(.time / 36 | round * 0.01)"'
printf "\n"
watson report --no-pager --json --from "$year"-"$month"-01 --to "$year"-"$month-$lastday" --project boxtribute \
--ignore-tag general-consolidation \
--ignore-tag assort \
--ignore-tag aid-sending \
--ignore-tag viz \
--ignore-tag dsee-hermine | jq -r '.projects[].tags[] | "\(.name)=\(.time / 36 | round * 0.01)"'
}
wa() {
# Watson Add frames for multiple full days
if [ $# -lt 2 ]; then
echo "Usage: wa holiday[+vacation|+name]|illness MONTH DAY [DAY...]" >&2
return 1
fi
local year project_tag project tag month date rc
year=$(date +%Y)
project_tag=$1
project=$(echo "$project_tag" | cut -d+ -f1)
tag=$(echo "$project_tag" | cut -d+ -f2)
if [ "$tag" = "$project_tag" ]; then
tag=""
else
tag=+"$tag"
fi
shift
month=$1
shift
rc=0
for day in "$@"; do
date=$year-$month-$day
watson add "$project" "$tag" --from "$date 09:00:00" --to "$date 15:00:00"
rc=$((rc + $?))
done
return $rc
}
dcleanup(){
# Cleanup unused Docker resources, https://stackoverflow.com/a/32723127/3865876
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
# ffc - fuzzy-find commit
ffc() {
local commits commit
commits=$(git log --all --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
echo "$commit" | sed "s/ .*//"
}
# some shortcuts for handling VMs
# docs: https://www.virtualbox.org/manual/ch08.html
vmon() {
if [[ "$#" -ne 1 ]]; then
echo "USAGE: vmon <vm>"
else
VBoxManage startvm "$1" --type headless
fi
}
vmoff() {
# http://stackoverflow.com/questions/31695600/bash-rematch-doesnt-capture
setopt KSH_ARRAYS BASH_REMATCH
if [[ "$#" -eq 0 ]]; then
running_vms=`VBoxManage list runningvms`
vm_regex='"(.*)" .*'
if [[ $running_vms =~ $vm_regex ]]; then
echo Stopping ${BASH_REMATCH[1]}...
VBoxManage controlvm "${BASH_REMATCH[1]}" savestate
else
echo "USAGE: vmoff <vm>"
fi
else
VBoxManage controlvm "$1" savestate
fi
unsetopt KSH_ARRAYS
}
scrot() {
/usr/bin/scrot --exec 'mv $f ~/Pictures' "$@"
}
merge_pr() {
# Merge a boxtribute Github pull request and move the associated trello card to
# 'Merged to master | v2-staging' column
# Dependencies: gh, jq, ripgrep, trl, trello-cli
# Arguments:
# 1. Github PR ID (optional)
local card_id
local pr_id
pr_id="$1"
# Display PR title
gh pr view "$pr_id" --json title | jq -r .title
echo
# Find card ID from PR comments (first match)
# matching cf. https://stackoverflow.com/a/67068668/3865876
card_id=$(gh pr view "$pr_id" --json comments | jq -r .comments[].body | rg --max-count 1 'trello.com/c/([a-zA-Z0-9]+)' -or '$1')
# Abort if no card found
trl card-details "$card_id" || return 1
read -p "Do you want to merge the PR and move the trello card (YyNn)? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Attempt to merge the PR; move trello card only when successful
gh pr merge "$pr_id" --delete-branch --merge --admin && trl move-card "$card_id" merged
fi
}
delete_gae_versions() {
# Delete Google App Engine versions of staging services if deploy date begins with PATTERN
# Requires previous configuration via `gcloud config set project <PROJECT>`
if [[ $# -ne 1 ]]; then
echo "Usage: delete_gae_versions PATTERN" >&2
return 1
fi
pattern="$1"
gcloud app versions delete --service v2-staging $(gcloud app versions list --service v2-staging | grep " $pattern" | cut -d' ' -f3 | xargs)
gcloud app versions delete --service api-staging $(gcloud app versions list --service api-staging | grep " $pattern" | cut -d' ' -f3 | xargs)
}
fj() {
# Find Jira items. Any command line options are passed to 'jira list'
# Requires: go-jira, fzf, bat
case "$1" in
''|-h|--help|help )
jira list --help; return 0 ;;
esac
local item
item="$(jira list "$@" | \
fzf --preview 'jira view $(echo {} | cut -d: -f1) | bat --color=always --plain --language YAML' \
--preview-window=:$([ $(tput cols) -gt $(($(tput lines) * 2)) ] && echo right || echo down)
)"
if [ "$item" != "" ]; then
jira browse $(echo "$item" | cut -d: -f1)
fi
}
bbpp() {
# Select and run target from Bitbucket pipelines config
# Requires: yq, fzf, bibu
local gitroot pipeline ppconfig
if ! gitroot="$(git rev-parse --show-toplevel 2>/dev/null)"; then
echo "Can only run from inside git repo!" >&2
return 1
fi
ppconfig="$gitroot"/bitbucket-pipelines.yml
if [ ! -f "$ppconfig" ]; then
echo "Repo root does not contain bitbucket-pipelines.yml" >&2
return 1
fi
# Select custom pipeline using fzf
pipeline=$(yq -r ".pipelines.custom | keys[]" "$ppconfig" | fzf --no-multi --height=30%)
# Quit if user aborted search
[ "$pipeline" = "" ] && return 0
bibu pipeline run -n "$pipeline"
}
observe() {
# Print lines in growing CSV file
# Usage:
# observe FILE [COLUMN-SELECTION]
[ $# -lt 1 ] && return 1
local selection
selection=${2:-3,5}
# https://github.com/BurntSushi/xsv/issues/192#issuecomment-778585631
tail -f "$1" | (while read -r line; do echo "$line" | xsv select -d \; "$selection"; done )
}
[ -e ~/.files/liquidprompt ] && . ~/.files/liquidprompt || true