Archived
1
0
Fork 0
This repository has been archived on 2024-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/simpleDots/functions

212 lines
5.2 KiB
Text
Raw Permalink Normal View History

#!/bin/bash
# Trap RC != 0 and display
EC() {
echo -e '\e[1;31m'code $?'\e[m\n'
}
trap EC ERR
# Create a new directory and enter it
mkd() {
mkdir -p "$@" && cd "$@"
}
# Make a temporary directory and enter it
tmpd() {
if [ $# -eq 0 ]; then
dir=`mktemp -d` && cd $dir
else
dir=`mktemp -d -t $1.XXXXXXXXXX` && cd $dir
fi
}
2024-10-28 21:35:26 +01:00
# cd and list
cl() {
local dir="$1"
local dir="${dir:=$HOME}"
if [[ -d "$dir" ]]; then
cd "$dir" >/dev/null; ls -l
else
2024-10-28 21:35:26 +01:00
echo "bash: cl: $dir: Directory not found"
fi
}
# Determine size of a file or total size of a directory
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg .[^.]* *
fi
}
# Start an HTTP server from a directory, optionally specifying the port
server() {
local port="${1:-8000}"
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesnt break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# Compare original and gzipped file size
gz() {
local origsize=$(wc -c < "$1")
local gzipsize=$(gzip -c "$1" | wc -c)
local ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l)
printf "orig: %d bytes\n" "$origsize"
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"
}
# Run `dig` and display the most useful info
digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
# Show all the names (CNs and SANs) listed in the SSL certificate
# for a given domain
getcertnames() {
if [ -z "${1}" ]; then
echo "ERROR: No domain specified."
return 1
fi
local domain="${1}"
echo "Testing ${domain}…"
echo ""; # newline
local tmp=$(echo -e "GET / HTTP/1.0\nEOT" \
| openssl s_client -connect "${domain}:443" 2>&1)
if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then
local certText=$(echo "${tmp}" \
| openssl x509 -text -certopt "no_header, no_serial, no_version, \
no_signame, no_validity, no_issuer, no_pubkey, no_sigdump, no_aux")
echo "Common Name:"
echo ""; # newline
echo "${certText}" | grep "Subject:" | sed -e "s/^.*CN=//"
echo ""; # newline
echo "Subject Alternative Name(s):"
echo ""; # newline
echo "${certText}" | grep -A 1 "Subject Alternative Name:" \
| sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2
return 0
else
echo "ERROR: Certificate not found."
return 1
fi
}
# `o` with no arguments opens the current directory, otherwise opens the given
# location
o() {
if [ $# -eq 0 ]; then
xdg-open . > /dev/null 2>&1
else
xdg-open "$@" > /dev/null 2>&1
fi
}
# Get colors in manual pages
man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
# Use feh to nicely view images
openimage() {
local types='*.jpg *.JPG *.png *.PNG *.gif *.GIF *.jpeg *.JPEG'
cd $(dirname "$1")
local file=$(basename "$1")
feh -q $types --auto-zoom \
--sort filename --borderless \
--scale-down --draw-filename \
--image-bg black \
--start-at "$file"
}
# get the name of a x window
xname(){
local window_id=$1
if [[ -z $window_id ]]; then
echo "Please specifiy a window id, you find this with 'xwininfo'"
return 1
fi
local match_string='".*"'
local match_int='[0-9][0-9]*'
local match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference
# get the name
xprop -id $window_id | \
sed -nr \
-e "s/^WM_CLASS\(STRING\) = ($match_qstring), ($match_qstring)$/instance=\1\nclass=\3/p" \
-e "s/^WM_WINDOW_ROLE\(STRING\) = ($match_qstring)$/window_role=\1/p" \
-e "/^WM_NAME\(STRING\) = ($match_string)$/{s//title=\1/; h}" \
-e "/^_NET_WM_NAME\(UTF8_STRING\) = ($match_qstring)$/{s//title=\1/; h}" \
-e '${g; p}'
}
# Docker
d_stop(){
docker stop $(docker ps -q)
}
d_clean(){
docker rm $(docker ps -q -f status=exited)
docker rmi $(docker images -q -f dangling=true)
docker volume rm $(docker volume ls -f dangling=true)
}
d_reset(){
2024-02-28 15:54:52 +01:00
docker stop $(docker ps -q) 2>/dev/null
docker rm -f $(docker ps -aq) 2>/dev/null
docker rmi -f $(docker images -qa) 2>/dev/null
docker volume rm $(docker volume ls -q) 2>/dev/null
}
## Dump SSL Cert from host
2020-12-15 19:56:49 +01:00
## sslcheck example.com:25 -starttls smtp
## sslcheck example.com:443
sslcheck(){
echo -n | openssl s_client -connect $* | tee /dev/tty | openssl x509 -text -noout
}
2020-12-22 14:08:33 +01:00
## Kubernetes Goodies
k(){
kubectl $*
}
k-sel(){
kubeconfig=$(find ~/.kube/kubesel -type f | rev | cut -d "/" -f 1 | rev | sort | fzf --height=25%)
export KUBECONFIG=~/.kube/kubesel/$kubeconfig
echo
kubectl config get-contexts
}
k-ns(){
2022-02-18 00:28:49 +01:00
kubectl config set-context \
2020-12-22 14:08:33 +01:00
--current \
--namespace=$(kubectl get namespace | grep Active \
| cut -d " " -f1 \
| fzf --height=25%
)
echo
kubectl config get-contexts
}