-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_bash_aliases.tmpl
271 lines (223 loc) · 6.99 KB
/
dot_bash_aliases.tmpl
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
yl() {
yq -C "$@"|less -R
}
jl() {
jq -C "$@"|less -R
}
{{- if eq .chezmoi.hostname "work" }}
find_yesterday() {
local days=1 file= i=0 max=365
while i=$((i+1)); [ $i -le $max ] ; do
printf '.' >&2
file=$(date -d "$days days ago" -I)
ls ../$file >/dev/null 2>&1 && break
days=$((days+1))
done
[ $i -ge $max ] && {
echo cannot find yesterday >&2
return 1
}
echo $file
}
work() {
local work_dir="$HOME/wrk/$(date -I)"
pushd $work_dir 2>/dev/null || {
echo not found. creating. >&2
mkdir $work_dir || return 1
pushd $work_dir
printf "find yesterday." >&2
yesterday=$(find_yesterday) && {
printf "found.\n"
ln "../$yesterday" ./y -s
} || {
printf "missed.\n"
}
}
}
mlf() {
f=$(find ~/lab/ -type d -name "*$@*" )
[ $(echo "$f"|wc -l) -gt 1 ] && {
select f in $f;
do
pushd $f
break
done
return
} || [ "$f" != "" ] && pushd $f || echo "Nothing found."
}
lab() {
cd ~/lab
}
mlab() {
cd ~/lab/ndrde/mdr
}
playground() {
cd ~/lab/playground
}
{{- end }}
gitapiitoken() {
export GITLAB_HOST=gitlab.ida-dev.de
export GITLAB_USER="operations-mcs"
export GITLAB_TOKEN=$(secret-tool lookup service gitapi user operations-mcs)
export GITLAB_API_TOKEN=$GITLAB_TOKEN
echo set GITLAB_USER and GITLAB_TOKEN/GITLAB_API_TOKEN accordingly
}
gitapitoken() {
export GITLAB_HOST=gitlab.com
export GITLAB_USER="karl.liebich-mcs-team"
export GITLAB_TOKEN=$(secret-tool lookup service gitapi user karl.liebich-mcs-team)
export GITLAB_API_TOKEN=$GITLAB_TOKEN
echo set GITLAB_USER and GITLAB_TOKEN/GITLAB_API_TOKEN accordingly
}
# GitLab REST api
gapi() {
# gitapitoken >/dev/null
local path
path=$1
shift
curl -s -H "Private-Token: $GITLAB_TOKEN" https://$GITLAB_HOST/api/v4$path "$@"
}
# GitLab GraphQL api
glapi() {
[ "${SHELL##*/}" != "bash" ] && {
echo "bash only — but shell is $SHELL"
return 1
}
[ -z "$@" ] && {
echo "usage: ${FUNCNAME[0]##*/} [query]" # See bash manual
return 1
}
(
set -e # in a subshell
[ -z "$GITLAB_TOKEN" ] && {
read -s -p 'Please provide GitLab Token or set $GITLAB_TOKEN ' GITLAB_TOKEN
}
local query
query="$(echo $1|sed 's.".\\".g')"
shift
printf 'Query: \n %s' "$query" >&2
curl -s https://gitlab.com/api/graphql \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GITLAB_TOKEN" \
-d '{"query": "'"$query"'"}' \
"$@"
)
}
terraform-gitlab-init() {
[ $# -lt 2 ] && {
echo "usage: terraform-gitlab-init <project_id> <state_name> [Terraform options]"
} || {
[ -z "$GITLAB_USER" ] && read -p "Input GitLab User " GITLAB_USER
[ -z "$GITLAB_TOKEN" ] && read -s -p "Input GitLab Token " GITLAB_TOKEN
local host='gitlab.ida-dev.de'
local project_id="${1}"
local state_name="${2}"
echo $state_name
shift 2
terraform init \
-backend-config="address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}" \
-backend-config="lock_address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}/lock" \
-backend-config="unlock_address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}/lock" \
-backend-config="username=${GITLAB_USER}" \
-backend-config="password=${GITLAB_TOKEN}" \
-backend-config="lock_method=POST" \
-backend-config="unlock_method=DELETE" \
-backend-config="retry_wait_min=5" \
"$@"
}
}
gitlab-set-host () {
[ -z "$GITLAB_HOST" ] && {
select GITLAB_HOST in gitlab.com gitlab.ida-dev.de
do
export GITLAB_HOST
break
done
}
}
gitlab-terraform-init() {
gitlab-set-host
[ $# -lt 2 ] && {
echo "usage: terraform-gitlab-init <project_id> <state_name> [Terraform options]"
} || {
[ -z "$GITLAB_USER" ] && read -p "Input GitLab User " GITLAB_USER
[ -z "$GITLAB_TOKEN" ] && read -s -p "Input GitLab Token " GITLAB_TOKEN
local host=${GITLAB_HOST:-gitlab.com}
local project_id="${1}"
local state_name="${2}"
shift 2
terraform init \
-backend-config="address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}" \
-backend-config="lock_address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}/lock" \
-backend-config="unlock_address=https://${host}/api/v4/projects/${project_id}/terraform/state/${state_name}/lock" \
-backend-config="username=${GITLAB_USER}" \
-backend-config="password=${GITLAB_TOKEN}" \
-backend-config="lock_method=POST" \
-backend-config="unlock_method=DELETE" \
-backend-config="retry_wait_min=5" \
"$@"
}
}
mr() {
[ -z "$GITLAB_TOKEN" ] && gitapitoken >/dev/null
current="$(git branch --show-current)" || { echo could not get current branch; return; }
title="$(git show -s --format='%s')" || { echo could not get current title; return; }
description="$(git show -s --format='%b')" || { echo could not get current body; return; }
. .project_id && echo .project_id has been sourced || echo no .project_id found
[ -z "$PROJECT_ID" ] && {
read -p "project id? " PROJECT_ID
echo "PROJECT_ID=$PROJECT_ID" >> .project_id
echo added project id to .project_id
}
url="https://$GITLAB_HOST/api/v4/projects/$PROJECT_ID/merge_requests"
printf '\nselect target\n\n'
select target in $(git branch|awk '{if ($1 == "*"){ print $2 } else { print $1} }')
do
tmp=$(mktemp)
printf "%s\n\n%s" "$title" "$description" > $tmp
vim $tmp
title="$(head -n1 $tmp)"
description="$(tail -n+3 $tmp)"
rm "$tmp"
request="{\"source_branch\": \"$current\", \"target_branch\": \"$target\","
request="$request \"title\": \"$title\", \"description\": \"$description\","
request="$request"'"squash": false, "remove_source_branch": true}'
# escape newlines in commit message, see https://superuser.com/questions/955935/how-can-i-replace-a-newline-with-its-escape-sequence
request=${request//$'\n'/\\n}
echo "$request" | jq || return $?
read -p "OK? " -n1
# jq in curl sanitizes the data in case there are invisible chars messing up the request
case "$REPLY" in
""|[Yy]) response=$(curl "$url" -H 'Content-Type: application/json' -H "PRIVATE-TOKEN: $GITLAB_TOKEN" -d "$request") || echo error
mr_url=$(echo "$response"|jq -r '.web_url')
echo "$response"|jq
printf '%s\n' "$mr_url" "$mr_url/edit"
#sensible-browser "$mr_url"
flatpak run com.brave.Browser "$mr_url"
;;
*) printf "\n\nabort\n" ;;
esac
break
done
unset PROJECT_ID
}
gds() {
echo "Delete Stale…"
stale_branches=$(git branch | grep -v main | sed 's/^*//')
[ -n "$stale_branches" ] && {
git branch -d $stale_branches || {
read -p 'Delete anyway? (y/n)' -n1 && echo
case $REPLY in
[yY]) git branch -D $(git branch | grep -v main)
;;
*) echo abort…
;;
esac
}
}
}
alias p='python3'
alias tmp='pushd $(mktemp -d)'
alias tfmt='tofu fmt -recursive'
# vi: set ai ft=sh: