-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shell_functions
125 lines (107 loc) · 3.21 KB
/
.shell_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
#!/usr/bin/env zsh
pinfo () { # path environment info
echo -e "PATH:$PATH" | tr ":" "\n"
}
function pginfo () { # postgres info
echo -e "PGHOST: $PGHOST"
echo -e "PGPORT: $PGPORT"
echo -e "PGDATABASE: $PGDATABASE"
echo -e "PGUSER: $PGUSER"
if [[ -z "$PGPASSWORD" ]]; then
echo -e "PGPASSWORD: <NOT set>"
else
echo -e "PGPASSWORD: <set>"
fi
}
# print external ip address
ip () {
dig +short myip.opendns.com @resolver1.opendns.com
}
# print internal ip address
lip () {
ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
}
# make a file of x MB
fakefile () {
perl -e "print '0' x 1024 x 1024 x $1" > "$1-MB-fake-file.txt"
}
rep () { # to be able to output stuff to a file that's being used to generate the input
# for example `cat file.txt | wc -l | rep file.txt`
# since this would fail `cat file.txt | wc -l > file.txt`
# rep = replace
TMPFILELOCATION=$(mktemp -q /tmp/mpXXXXXXXXXXXXXXXX)
cat >> "$TMPFILELOCATION"
mv "$TMPFILELOCATION" "$1"
}
st () {
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
git status
else
echo 'not in a git repo'
fi
}
# if in git repo, return the branch. I currently only use
#this for the zsh prompt, hence the parens.
get_git_branch () {
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
printf "(%s)" "$(git rev-parse --abbrev-ref HEAD | tr '\n' ' ' | trim)"
fi
}
# search for the parameter here, recursively, case-insensitively
gri () { grep -r -i "$*" .; }
# cd to a file goes to its directory
cd() {
if [ -f "$*" ]; then
builtin cd "$(dirname "$*")"
elif [ -z "$*" ]; then # zero length string
builtin cd
else
builtin cd "$*"
fi
}
# Show ports that are listening
listening() {
lsof -i :$1 | grep LISTEN
}
# Kill the process associated with a port being listened to
listeningkill() {
pid=$(listening $1 | awk '{print $2}')
echo "killing $pid..."
kill $pid
echo "ded."
}
# send TERM to the process associated with a port being listened to
listeningterm() {
pid=$(listening $1 | awk '{print $2}')
echo "sending TERM (15) to $pid..."
kill -15 $pid
echo "ded."
}
gha () { # open github actions for current branch
origin=$(git config --get remote.origin.url)
base_url="${origin/.git/}"
current_branch=$(git rev-parse --abbrev-ref HEAD)
encoded_branch=$(printf %s "$current_branch" | jq -sRr @uri)
actions_url="$base_url/actions?query=branch%3A$encoded_branch"
echo "Opening $actions_url"
open "$actions_url"
}
ghar () { # open github actions for most recent run for current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
runId=$(gh run list --branch $current_branch --limit 1 --json databaseId | jq ".[0].databaseId")
origin=$(git config --get remote.origin.url)
base_url="${origin/.git/}"
actions_url="$base_url/actions/runs/$runId"
echo "Opening $actions_url"
open "$actions_url"
}
ghpr () { # open github pull request for current branch
gh pr view --web
}
function awsid() {
echo "printing the aws account active"
aws sts get-caller-identity --output text
}
yaml2json() {
yq e -ojson "$1"
}