From 3aa127e7e6c104199cbed6210674ba60f1f496b6 Mon Sep 17 00:00:00 2001 From: thek4n Date: Thu, 19 Dec 2024 12:11:40 +0300 Subject: [PATCH] chore: move rarely used zsh functions to files --- TARGETS.sh | 1 + home/user/.config/zsh/aliases | 5 - home/user/.config/zsh/functions | 136 --------------------- home/user/.local/bin/destroy | 23 ++++ home/user/.local/bin/django-create-project | 15 +++ home/user/.local/bin/extract | 41 +++++++ home/user/.local/bin/genpass | 5 + home/user/.local/bin/gensalt | 5 + home/user/.local/bin/mirror-site | 26 ++++ home/user/.local/bin/myip | 6 + home/user/.local/bin/split-file | 15 +++ 11 files changed, 137 insertions(+), 141 deletions(-) create mode 100755 home/user/.local/bin/destroy create mode 100755 home/user/.local/bin/django-create-project create mode 100755 home/user/.local/bin/extract create mode 100755 home/user/.local/bin/genpass create mode 100755 home/user/.local/bin/gensalt create mode 100755 home/user/.local/bin/mirror-site create mode 100755 home/user/.local/bin/myip create mode 100755 home/user/.local/bin/split-file diff --git a/TARGETS.sh b/TARGETS.sh index 5888235..730485b 100644 --- a/TARGETS.sh +++ b/TARGETS.sh @@ -20,4 +20,5 @@ docker:.docker/cli-plugins ipython:.ipython/profile_default/ipython_config.py gdb:.config/gdb sandbox: +utils: .local/bin/myip .local/bin/genpass .local/bin/gensalt .local/bin/django-create-project .local/bin/mirror-site .local/bin/split-file .local/bin/extract .local/bin/destroy all:%less %t %tmux %zsh %nvim %git" \ No newline at end of file diff --git a/home/user/.config/zsh/aliases b/home/user/.config/zsh/aliases index c3c79d6..0d690dc 100644 --- a/home/user/.config/zsh/aliases +++ b/home/user/.config/zsh/aliases @@ -57,9 +57,6 @@ alias c='clear' alias h='history 0' alias j='jobs -l' -# utils -alias genpass="openssl rand -base64 12" -alias gensalt="dd if=/dev/urandom count=16 2>/dev/null | sha256sum | head -c 64" if command -v colordiff &>/dev/null; then alias diff='colordiff' @@ -81,8 +78,6 @@ alias svi="sudo --preserve-env nvim" # net alias ports='ss -tlnp' alias wget='wget -c' -# alias myip='curl ipinfo.io/ip' -alias myip='dig +short myip.opendns.com @resolver1.opendns.com' alias ip='ip -c' alias fastping='ping -c 100 -i 0.1' diff --git a/home/user/.config/zsh/functions b/home/user/.config/zsh/functions index 453e22d..9dd6d24 100644 --- a/home/user/.config/zsh/functions +++ b/home/user/.config/zsh/functions @@ -1,29 +1,5 @@ # vim: ft=zsh -destroy() { - local -r filename="${1}" - - if [[ ! -f "${filename}" ]]; then - echo "destroy: File '${filename}' not found" >&2 - return 1 - fi - - local filesize - filesize="$(du -hs "${filename}" | awk '{printf $1}')" - readonly filesize - - echo -n "Sure want to destroy file '${filename}' with size ${filesize} [y/N] " - - local response - read -r response - readonly response - if [[ ! "${response}" == [yY] ]]; then - return 1 - fi - - shred -zun 3 "${filename}" -} - py() { if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then ipython -i -c "q = exit" @@ -65,46 +41,6 @@ va() { fi } - -extract() ( - if [ -z "${1}" ]; then - echo "extract: error: usage: extract ." - return 2 - fi - - if ! [ -f "${1}" ]; then - echo "extract: error: '${1}' file does not exist" >&2 - return 1 - fi - - local -r name="${1%%.*}" # removes extension from filename - if [ -e "${name}" ]; then - echo "extract: error: '${name}' exists" >&2 - return 1 - fi - - mkdir "${name}" && cd "${name}" || return 1 - - case $1 in - *.tar.bz2) tar xjf ../"${1}" ;; - *.tar.gz) tar xzf ../"${1}" ;; - *.tar.xz) tar xJf ../"${1}" ;; - *.lzma) unlzma ../"${1}" ;; - *.bz2) bunzip2 ../"${1}" ;; - *.rar) unrar x -ad ../"${1}" ;; - *.gz) gunzip ../"${1}" ;; - *.tar) tar xf ../"${1}" ;; - *.tbz2) tar xjf ../"${1}" ;; - *.tgz) tar xzf ../"${1}" ;; - *.zip) unzip ../"${1}" ;; - *.Z) uncompress ../"${1}" ;; - *.7z) 7z x ../"${1}" ;; - *.xz) unxz ../"${1}" ;; - *.exe) cabextract ../"${1}" ;; - *) echo "extract: error: '${1}' - unknown archive method" >&2 ;; - esac -) - mcd() { if [ -z "${1}" ]; then cd "$(mktemp -td "${USER:-user}.XXXX")" @@ -118,19 +54,6 @@ open() { nohup xdg-open "${1}" 1>/dev/null 2>&1 & } -split-file() { - if [[ ! -e "${1}" ]]; then - echo "file '${1}' not found" >&2 - return 1 - fi - - local -r size="${2:-1G}" - - mkdir "${1}.splitted" - cd "${1}.splitted" - split -d -b "${size}" "../${1}" -} - json() { if [ -t 0 ]; then python -m json.tool <<< "$*" @@ -139,21 +62,6 @@ json() { fi } -rmt() { - local -r trash="${TRASH:-"${HOME}/.trash"}" - - local filename - for filename in "$@" - do - local filename_out_path - filename_out_path="${trash}$(realpath "${filename}")" - readonly filename_out_path - - mkdir -p "$(dirname "${filename_out_path}")" - mv "${filename}" "${filename_out_path}_$(date +%s)" - done -} - showtips() { local -r tips_dir="${HOME}/.tips" @@ -171,23 +79,6 @@ showtips() { return 0 } -most-often-commands() { - history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn -} - -django-create-project() ( - set -ue - local -r project_name="${1}" - - mkdir "${project_name}" - cd "${project_name}" - python3 -m virtualenv venv - . venv/bin/activate - pip install django - django-admin startproject core . - git init -) - cleanup-directory() { local -r directory="${1}" @@ -242,33 +133,6 @@ bak() { done } -mirror-site() ( - set -eu - - local -r name="${1}"; shift - - mkdir -p "${name}" && cd "${name}" - - local -r user_agent="Mozilla/5.0 ..." - wget \ - --page-requisites \ - --convert-links \ - --adjust-extension \ - --restrict-file-names=ascii \ - --span-hosts \ - --random-wait \ - --execute robots=off \ - --recursive \ - --timestamping \ - -l inf \ - --no-remove-listing \ - --no-parent \ - --user-agent "${user_agent}" \ - --reject '*.woff*,*.ttf,*.eot,*.js' \ - --tries 10 \ - $@ -) - sha() { if [ -v 2 ]; then shasum -a 256 "${@}" diff --git a/home/user/.local/bin/destroy b/home/user/.local/bin/destroy new file mode 100755 index 0000000..f7d6169 --- /dev/null +++ b/home/user/.local/bin/destroy @@ -0,0 +1,23 @@ +#!/bin/sh + +set -eu + +readonly filename="${1}" + +if [ ! -f "${filename}" ]; then + echo "destroy: File '${filename}' not found" >&2 + exit 1 +fi + +filesize="$(du -hs "${filename}" | awk '{printf $1}')" +readonly filesize + +printf "Sure want to destroy file '%s' with size %s [y/N] " "${filename}" "${filesize}" + +read -r response +readonly response +if [ ! "${response}" = "y" ]; then + exit 1 +fi + +shred -zun 3 "${filename}" \ No newline at end of file diff --git a/home/user/.local/bin/django-create-project b/home/user/.local/bin/django-create-project new file mode 100755 index 0000000..0011d70 --- /dev/null +++ b/home/user/.local/bin/django-create-project @@ -0,0 +1,15 @@ +#!/bin/sh + + +set -ue + + +readonly project_name="${1}" + +mkdir "${project_name}" +cd "${project_name}" +python3 -m virtualenv venv +. venv/bin/activate +pip install django +django-admin startproject core . +git init \ No newline at end of file diff --git a/home/user/.local/bin/extract b/home/user/.local/bin/extract new file mode 100755 index 0000000..906189c --- /dev/null +++ b/home/user/.local/bin/extract @@ -0,0 +1,41 @@ +#!/bin/sh + +set -eu + + +if [ -z "${1}" ]; then + echo "extract: error: usage: extract ." + exit 2 +fi + +if ! [ -f "${1}" ]; then + echo "extract: error: '${1}' file does not exist" >&2 + exit 1 +fi + +readonly name="${1%%.*}" # removes extension from filename +if [ -e "${name}" ]; then + echo "extract: error: '${name}' exists" >&2 + exit 1 +fi + +mkdir "${name}" && cd "${name}" || exit 1 + +case "${1}" in + *.tar.bz2) tar xjf ../"${1}" ;; + *.tar.gz) tar xzf ../"${1}" ;; + *.tar.xz) tar xJf ../"${1}" ;; + *.lzma) unlzma ../"${1}" ;; + *.bz2) bunzip2 ../"${1}" ;; + *.rar) unrar x -ad ../"${1}" ;; + *.gz) gunzip ../"${1}" ;; + *.tar) tar xf ../"${1}" ;; + *.tbz2) tar xjf ../"${1}" ;; + *.tgz) tar xzf ../"${1}" ;; + *.zip) unzip ../"${1}" ;; + *.Z) uncompress ../"${1}" ;; + *.7z) 7z x ../"${1}" ;; + *.xz) unxz ../"${1}" ;; + *.exe) cabextract ../"${1}" ;; + *) echo "extract: error: '${1}' - unknown archive method" >&2 ;; +esac \ No newline at end of file diff --git a/home/user/.local/bin/genpass b/home/user/.local/bin/genpass new file mode 100755 index 0000000..4398f2c --- /dev/null +++ b/home/user/.local/bin/genpass @@ -0,0 +1,5 @@ +#!/bin/sh + +set -ue + +openssl rand -base64 12 \ No newline at end of file diff --git a/home/user/.local/bin/gensalt b/home/user/.local/bin/gensalt new file mode 100755 index 0000000..bbbc407 --- /dev/null +++ b/home/user/.local/bin/gensalt @@ -0,0 +1,5 @@ +#!/bin/sh + +set -ue + +dd if=/dev/urandom count=16 2>/dev/null | sha256sum | head -c 64 \ No newline at end of file diff --git a/home/user/.local/bin/mirror-site b/home/user/.local/bin/mirror-site new file mode 100755 index 0000000..493b66c --- /dev/null +++ b/home/user/.local/bin/mirror-site @@ -0,0 +1,26 @@ +#!/bin/sh + +set -eu + +readonly name="${1}"; shift + +mkdir -p "${name}" && cd "${name}" + +readonly user_agent="Mozilla/5.0 ..." +wget \ + --page-requisites \ + --convert-links \ + --adjust-extension \ + --restrict-file-names=ascii \ + --span-hosts \ + --random-wait \ + --execute robots=off \ + --recursive \ + --timestamping \ + -l inf \ + --no-remove-listing \ + --no-parent \ + --user-agent "${user_agent}" \ + --reject '*.woff*,*.ttf,*.eot,*.js' \ + --tries 10 \ + "$@" \ No newline at end of file diff --git a/home/user/.local/bin/myip b/home/user/.local/bin/myip new file mode 100755 index 0000000..35b4fb1 --- /dev/null +++ b/home/user/.local/bin/myip @@ -0,0 +1,6 @@ +#!/bin/sh + + +curl ipinfo.io/ip +echo +dig +short myip.opendns.com @resolver1.opendns.com \ No newline at end of file diff --git a/home/user/.local/bin/split-file b/home/user/.local/bin/split-file new file mode 100755 index 0000000..f1bfb51 --- /dev/null +++ b/home/user/.local/bin/split-file @@ -0,0 +1,15 @@ +#!/bin/sh + +set -eu + + +if [ ! -e "${1}" ]; then + echo "file '${1}' not found" >&2 + exit 1 +fi + +readonly size="${2:-1G}" + +mkdir "${1}.splitted" +cd "${1}.splitted" +split -d -b "${size}" "../${1}" \ No newline at end of file