First commit

This commit is contained in:
Denis Isidoro 2019-09-20 10:38:58 -03:00
commit 3fd0e3f5cb
16 changed files with 351 additions and 0 deletions

4
sheets/awk.cheat Normal file
View file

@ -0,0 +1,4 @@
% awk, text processing, string
# Print nth column
awk '{print $<n>}

7
sheets/crontab.cheat Normal file
View file

@ -0,0 +1,7 @@
% crontab, scheduling
# List cron jobs
crontab -l
# Edit cron job
crontab -e

40
sheets/docker.cheat Normal file
View file

@ -0,0 +1,40 @@
% docker, container
# Remove an image
docker image rm <image_id>
# Delete an image from the local image store
docker rmi <image_id>
# List all images that are locally stored with the Docker engine
docker images
# Build an image from the Dockerfile in the current directory and tag the image
docker build -t <image>:<version> .
# Pull an image from a registry
docker pull <image>:<version>
# Stop a running container through SIGTERM
docker stop <container_id>
# Stop a running container through SIGKILL
docker kill <container_id>
# List the networks
docker network ls
# List the running containers
docker ps
# Delete all running and stopped containers
docker rm -f $(docker ps -aq)
# Create a new bash process inside the container and connect it to the terminal
docker exec -it <container_id> bash
# Print the last lines of a containers logs
docker logs --tail 100 <container_id> | less
$ image_id: docker images --- --headers 1 --column 3
$ container_id: docker ps --- --headers 1 --column 1

13
sheets/git.cheat Normal file
View file

@ -0,0 +1,13 @@
% git
# Clear everything
git clean -dxf
# Sign all commits in a branch based on master
git rebase master -S -f
# Checkout to branch
# Change branch
git checkout <branch>
$ branch: git branch --format='%(refname:short)'

7
sheets/kubernetes.cheat Normal file
View file

@ -0,0 +1,7 @@
% kubernetes, k8s
# Edit deployment
kubectl edit deployment <deployment>
# Get pods
kubectl get pods

10
sheets/mysql.cheat Normal file
View file

@ -0,0 +1,10 @@
% mysql, database, db
# Create database
mysql -u <user> -p -e "create database $database character set UTF8mb4 collate utf8mb4_bin"
# Export databse
mysqldump -u <user> -p <database> > <path>
# Import database
mysql -u <user> -p <database> <path>

13
sheets/network.cheat Normal file
View file

@ -0,0 +1,13 @@
% network
# Kill a process running on a given port
lsof -i :<port> | awk '{l=$2} END {print l}' | xargs kill
# List IP addresses connected on a given port
netstat -tn 2>/dev/null | grep :<port> | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
# Find external, public IP address
dig +short myip.opendns.com @resolver1.opendns.com
# Find primary, local IP address
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

24
sheets/tar.cheat Normal file
View file

@ -0,0 +1,24 @@
% tar, zip, gzip, compression
# Create a tar containing files
tar cf <name>.tar <files>
# Extract the files from a tar
tar xf <tar_file>
# Create a tar with Gzip compression
tar czf <name>.tar.gz <files>
# Extract a tar using Gzip
tar xzf <targz_file>
# Compress file and appends .gz to its name
gzip <path>
# Decompress compressed file
gzip -d <gz_file>
$ path: ls
$ tar_file: ls *.tar
$ targz_file: ls *.tar.gz
$ gz_file: ls *.gz

54
src/arg.sh Normal file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env bash
arg::fn() {
awk -F'---' '{print $1}'
}
arg::opts() {
awk -F'---' '{print $2}'
}
arg::interpolate() {
local readonly arg="$1"
local readonly value="$2"
sed "s|<${arg}>|\"${value}\"|g"
}
arg::next() {
grep -Eo '<[0-9a-zA-Z\-_]+>' \
| head -n1 \
| tr -d '<' \
| tr -d '>'
}
arg::pick() {
local readonly arg="$1"
local readonly cheat="$2"
local readonly prefix="$ ${arg}:"
local readonly length="$(echo "$prefix" | str::length)"
local readonly arg_description="$(grep "$prefix" "$cheat" | str::sub $((length + 1)))"
local readonly fn="$(echo "$arg_description" | arg::fn)"
local readonly args_str="$(echo "$arg_description" | arg::opts | tr ' ' '\n' || echo "")"
local arg_name=""
for arg_str in $args_str; do
if [ -z $arg_name ]; then
arg_name="$(echo "$arg_str" | str::sub 2)"
else
eval "local $arg_name"='$arg_str'
arg_name=""
fi
done
if [ -n "$fn" ]; then
eval "$fn" | ui::pick --prompt "$arg: " --header-lines "${headers:-0}" | str::column "${column:-}"
else
printf "\033[0;36m${arg}:\033[0;0m " > /dev/tty
read value
ui::clear_previous_line > /dev/tty
printf "$value"
fi
}

33
src/cheat.sh Normal file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
cheat::find() {
find "${cheat_folder:-"${DIR}/../sheets"}" -iname '*.cheat'
}
cheat::read_many() {
for cheat in $(cat); do
awk '
function color(c,s) {
printf("\033[%dm%s\033[0m",30+c,s)
}
/^%/ { tags=substr($0, 3); next }
/^#/ { print color(3, tags"^") color(4, $0); next }
/^\$/ { next }
NF { print color(3, tags"^") color(7, $0); next }' "$cheat"
done
}
cheat::from_selection() {
local readonly cheats="$1"
local readonly selection="$2"
local readonly tags="$(echo "$selection" | selection::tags)"
for cheat in $cheats; do
if grep -q "% $tags" "$cheat"; then
echo "$cheat"
break
fi
done
}

61
src/cheats Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env bash
set -euo pipefail
export DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
source "${DIR}/arg.sh"
source "${DIR}/cheat.sh"
source "${DIR}/docs.sh"
source "${DIR}/misc.sh"
source "${DIR}/selection.sh"
source "${DIR}/str.sh"
source "${DIR}/ui.sh"
##? Command cheatsheet tool
##?
##? Usage:
##? cheats [options]
##?
##? Options:
##? --print Prevent script execution [default: false]
##? --no-interpolation Prevent argument interpolation [default: false]
##? -c --cheat-folder <cheat-folder> Folder with cheatsheets
docs::eval "$@"
main() {
local readonly cheats="$(cheat::find)"
local readonly selection="$(ui::select "$cheats")"
local readonly cheat="$(cheat::from_selection "$cheats" "$selection")"
local cmd="$(selection::command "$selection" "$cheat")"
local arg value
if $no_interpolation; then
echo "$cmd"
exit 0
fi
while true; do
arg="$(echo "$cmd" | arg::next || echo "")"
if [ -z "$arg" ]; then
break
fi
value="$(arg::pick "$arg" "$cheat" || echo "")"
if [ -z "$value" ]; then
echo "$cmd"
exit 0
fi
eval "local $arg"='$value'
cmd="$(echo "$cmd" | arg::interpolate "$arg" "$value")"
done
if $print; then
echo "$cmd"
else
eval "$cmd"
fi
}
main "$@"

8
src/docs.sh Normal file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
docs::eval() {
print=false
no_interpolation=false
cheat_folder=""
}

6
src/misc.sh Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
# no-op hack to set dependency order resolution
dep() {
:
}

27
src/selection.sh Normal file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bash
selection::core() {
cut -d'^' -f2
}
selection::tags() {
cut -d'^' -f1
}
selection::core_is_comment() {
grep -qE '^#'
}
selection::command() {
local readonly selection="$1"
local readonly cheat="$2"
local readonly core="$(echo $selection | selection::core)"
if echo "$core" | selection::core_is_comment; then
grep "$core" "$cheat" -A999 \
| str::last_paragraph_line
else
echo "$core"
fi
}

27
src/str.sh Normal file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bash
str::length() {
awk '{print length}'
}
str::sub() {
local readonly start="${1:-0}"
local readonly finish="${2:-99999}"
cut -c "$((start + 1))-$((finish - 1))"
}
str::column() {
local readonly n="${1:-}"
if [ -n "$n" ]; then
awk "{print \$$n}"
else
cat
fi
}
str::last_paragraph_line() {
awk '(!NF) { exit } { print $0 }' \
| tail -n1
}

17
src/ui.sh Normal file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
ui::pick() {
fzf --inline-info "$@"
}
ui::select() {
local readonly cheats="$1"
echo "$cheats" \
| cheat::read_many \
| ui::pick -i --ansi --delimiter '\^' --with-nth 2
}
ui::clear_previous_line() {
tput cuu1 && tput el || true
}