-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.sh
executable file
·279 lines (232 loc) · 10.1 KB
/
commands.sh
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
#!/usr/bin/env bash
# Dependencies: exa
#listFiles
alias listDirectoryContentsSortByAlphabeticallyAscend="exa -l --group-directories-first"
alias listDirectoryContentsSortBySizeAscend="exa -l --group-directories-first --sort=size"
alias listDirectoryContentsSortBySizeDescend="exa -l --group-directories-first --sort=size -r"
alias listDirectoryContentsSortByTimeAscend="exa -l --group-directories-first --sort=modified"
alias listDirectoryContentsSortByTimeDescend="exa -l --group-directories-first --sort=modified -r"
#!/usr/bin/env bash
#files
makeFilePermissionExecutable() { chmod +x "$@" ; }
makeAllShFilesPermissionExecutable() { chmod +x ./*.sh ; }
makeOwnedByMyself() { sudo chown "$USER":"$(id -g "$USER")" -R "$@" ; }
makeFilePermissionEverything() { sudo chmod 777 -R "$@" ; }
# shellcheck disable=SC2012
getLastModifiedFileInDirectory() { ls -t "$1" | head -n1 ; }
get_AbsolutePath_LastModifiedFileInDirectory() {
# remove trailing slash
dir=${1%/}
lmf=$(getLastModifiedFileInDirectory "$1")
echo "$dir/$lmf"
}
delete_EmptyFiles_Recursively() { find . -type f -empty -delete ; }
delete_Recursive() { rm -rf "$1" ; }
delete_EverythingInCurrentDir() { find . -delete; }
# get_firstFileWithExtension() { find -iname "*.$1" . | head -n 1 ; }
# directories
delete_EmptyDirectories_Recursively() { find . -type d -empty -delete ; }
file_getOctalPermission() { stat -c "%a %n" "$@" ; }
createFile() {
for p do
_dir="$(dirname -- "$p")"
mkdir -p -- "$_dir" && touch -- "$p"
done
}
vimLastDownloadedFile() { vim ~/Downloads/"$(getLastModifiedFileInDirectory ~/Downloads)" ; }
#!/usr/bin/env bash
git_cdRoot() { cd "$(git rev-parse --show-toplevel)" ; }
git_ExportRepoAsZip() { git archive -o "$1" HEAD ; }
git_AddModified_Commit_Push() { git commit -am "$1" && git push ; }
git_CurrentCommitHash() { git rev-parse HEAD ; }
git_CurrentCommitHash_ToClipboard() { git_CurrentCommitHash | xclip ; }
git_Branch() { git branch -v -v; }
git_Diff() {
git --no-pager diff --stat
echo
git --no-pager diff --color | diff-so-fancy
if ! git diff-index --cached --quiet HEAD --; then
echo
echo "*************************************************"
echo "**************** GIT DIFF CACHED ****************"
echo "*************************************************"
echo
git --no-pager diff --stat --cached
echo
git --no-pager diff --cached --color | diff-so-fancy
fi
}
git_Status() { git status | tac; }
git_Log() { git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit ; }
git_Log_NameOnly() { git log --name-only "$@" ; }
git_ResetToLatest() { git reset --hard HEAD ; }
git_checkoutPaths() { git checkout -- "$@" ; }
git_FirstCommitHash() { git rev-list --max-parents=0 HEAD ; }
#!/usr/bin/env bash
# compression
makeZip_CurrentDirectory() { zip -r "$(basename "$PWD" )" . ; }
makeZip_CurrentDiretory_WithoutHidden() { zip -r "$(basename "$PWD")" . -x '.*' ; }
makeGZip_CurrentDirectory() { tar -c --use-compress-program=pigz -f "$(basename "$PWD")".tar.gz . ; }
#search
findByFilename() { find . -name "$1" ; }
findByFilenameCaseInsensitive() { find . -iname "$1" ; }
isTextPresentInFilodir() { grep -q "$1" "$2" ; }
# git
# arch linux pacman
pacmanInstall () { sudo pacman -S "$1"; }
pacmanUpdate () { sudo pacman -Syu ; }
# systemd
alias startService="sudo systemctl start "
alias stopService="sudo systemctl stop "
# outputFiltering
alias filterExclude="rg -v"
alias filterOnly="rg"
alias filterExcludeEmptyLines="grep -v -e '^$'"
# misc
backup () { cp -a "$1" "$1".backup ; }
backup_appendWithDate () { cp -a "$1" "$1"."$(date_ddmmyyyy)".backup ; }
backup_appendWithTimeDate () { cp -a "$1" "$1"."$(date_hhmmssddmmyyyy)".backup ; }
restoreBackup () { cp -a "$1".backup "$1" ; }
clipboard_copyPath () { readlink --no-newline -f "$1" | xclip -selection clipboard ; }
clipboard_copyCurrentPath () { clipboard_copyPath "$(pwd)"; }
# misc (requires xclip)
clipboard_copyFileContents () { xclip < "$1"; }
sshSocksProxy() { ssh -D 8118 -f -C -q -N "$@" ; }
newEditFile() { touch "$1" && $EDITOR "$1" ; }
newEditExecutableFile() { touch "$1" && makeFilePermissionExecutable "$1" && $EDITOR "$1" ; }
clipboard_copySshPublicKey() { xclip < ~/.ssh/id_rsa.pub ; }
date_ddmmyyyy () { date '+%d-%m-%Y' ;}
date_hhmmssddmmyyyy () { date '+%H-%M-%S-%d-%m-%Y' ;}
wget_toStdOut() { wget -qO- "$@" ;}
wget_ignoreSslCert() { wget --no-check-certificate "$@" ;}
# file manipulation
file_delete_line() { sed -i "${2}d" "$1" ;}
print_everyPathLevel_ofPath() {
if [[ "$#" = 0 ]]; then
print_everyPathLevel_ofPath "$PWD"
else
if [ "$1" != "/" ];
then
print_everyPathLevel_ofPath "$(dirname "$1")"
fi
fi
}
text_sanitize_filesystem_name () { echo "$1" | sed -e "s/ /-/g" | tr -cd '[:alnum:]_-' ; }
#!/usr/bin/env bash
git_initAndCommitInitial() { git init; git add .; git commit -m "inital" ;}
git_DiffPager() { git_Diff | less ;}
git_searchCommitMessagesInAllBranches() { git log --all --grep="$1" ;}
git_forceClone() {
if [[ "$#" = 1 ]]; then
set -x
git init
git remote add origin "$1"
git fetch
git checkout -f -t origin/master
git checkout .
elif [[ "$#" = 2 ]]; then
echo "Hmmmmmmm"
set -x
git init
git remote add origin "$1"
git fetch
git checkout -f -t origin/"$2"
git checkout .
else
echo "Not enough parameters" 1>&2
exit 1
fi
}
#!/usr/bin/env bash
# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
(
set -e
projName="$(basename "$PWD")"
json=$(jq -n \
--arg name "$projName" \
--arg description "$1" \
'{"name":$name, "description":$description}')
curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
if [ -d .git ]; then
git remote add origin [email protected]:"$GITHUB_USER"/"$projName".git
git push origin master
else
git init
git remote add origin [email protected]:"$GITHUB_USER"/"$projName".git
git push origin master
fi;
)
};
#!/usr/bin/env bash
# hpack (requires misc.sh, jq, yaml, sponge, hpack)
hpack_AddDependencyNaively() {
if ! isTextPresentInFilodir package.yaml "$1";
then
backup package.yaml && (yaml2json package.yaml | jq ".dependencies += [\"${1}\"]" | json2yaml | sponge package.yaml ) && hpack
else
echo "That dependency already seems present"
return 1
fi
}
hoogle_searchAndCopy() { wget -qO- "https://hoogle.haskell.org/?hoogle=${1}&mode=json" ;}
#!/usr/bin/env bash
# (requires docker, docker-compose)
# shellcheck disable=SC2046
docker_stopAllContainers() { sudo docker stop $(sudo docker ps -q); }
docker_stopAllContainersNonGracefully() { sudo docker kill $(sudo docker ps -q); }
docker_deleteComposeVolumes() { sudo docker-compose down -v ; }
docker_showLatestCreatedContainer() { sudo docker ps -a --latest; }
docker_getLatestCreatedContainerId() { sudo docker ps -a --latest -q; }
docker_startLatestCreatedContainer() { sudo docker start "$(sudo docker ps -a --latest -q)" ;}
docker_listContainers() { sudo docker ps -a ; }
docker_listRunningContainers() { sudo docker ps; }
docker_listRunningContainerIps() { sudo docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(sudo docker ps -q) ; }
docker_enterContainerBash() { sudo docker exec -it "$(sudo docker ps --format "{{.Names}}" | fzf | cat)" /bin/bash ; }
docker_enterContainerSh() { sudo docker exec -it "$(sudo docker ps --format "{{.Names}}" | fzf | cat)" /bin/sh ; }
docker_runCommandInContainer() { sudo docker exec -it "$@"; }
docker_runCommandInContainerWithContainerPrompt() { sudo docker exec -it "$(docker_promptForRunningContainer)" "$@"; }
docker_promptForRunningContainer() { sudo docker ps --format "{{.Names}}" | fzf ; }
docker_dockerComposeUp() { sudo docker-compose up ; }
docker_dockerComposeStop() { sudo docker-compose stop ; }
docker_getImageHash_sha256() { sudo docker inspect --format='{{index .RepoDigests 0}}' "$@";}
#!/usr/bin/env bash
# (requires docker, docker-compose)
kubectl_describeLastPod() { kubectl describe pod "$(kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n -1 | awk '{print $1;}')" "$@" ; }
kubectl_describePod() { kubectl describe pod "$@" ; }
kubectl_getPods() { kubectl get pods "$@"; }
kubectl_getAllPods() { kubectl get pods --all-namespaces "$@"; }
kubectl_getDeployments() { kubectl get deployments "$@"; }
kubectl_getAllDeployments() { kubectl get deployments --all-namespaces "$@"; }
kubectl_describeService() { kubectl describe service "$@"; }
kubectl_getServices() { kubectl get services "$@"; }
kubectl_getAllServices() { kubectl get services --all-namespaces "$@"; }
kubectl_describeIngress() { kubectl describe ingress "$@"; }
kubectl_getIngress() { kubectl get ingress "$@"; }
kubectl_getAllIngress() { kubectl get ingress --all-namespaces "$@"; }
kubectl_getNodes() { kubectl get nodes "$@"; }
kubectl_describeNode() { kubectl describe node "$@" ; }
kubectl_logs() { kubectl logs "$@"; }
kubectl_enterContainerSh() { kubectl exec -it "$(kubectl get pods | tail -n +2 | cut -d' ' -f1 | fzf)" sh -c "$1" ; }
kubectl_enterContainerBash() { kubectl exec -it "$(kubectl get pods | tail -n +2 | cut -d' ' -f1 | fzf)" bash -c "$1" ; }
kubectl_create_byFile() { kubectl create -f "$@"; }
kubectl_apply_byFile() { kubectl apply -f "$@"; }
kubectl_replace_byFile() { kubectl replace -f "$@"; }
kubectl_delete_byFile() { kubectl delete -f "$@"; }
kubectl_getAll() {
kubectl get nodes "$@"
kubectl get deployments "$@"
kubectl get daemonsets "$@"
kubectl get pods "$@"
kubectl get rs "$@"
kubectl get services "$@"
kubectl get ingress "$@"
kubectl get secrets "$@"
kubectl get serviceaccounts "$@"
}
#!/usr/bin/env bash
network_findProcessUsingPort() { sudo netstat -tulpn | rg "$1" ; }
network_findProcessIdUsingPort() { sudo netstat -tlnp | awk '/:'"$1"' */ {split($NF,a,"/"); print a[1]}'; }