From 3389fe8cc713c6afa479df9cdad1fae9600ae078 Mon Sep 17 00:00:00 2001 From: pika Date: Sat, 5 Jul 2025 13:09:20 +0200 Subject: [PATCH] addet some needed functions/aliase --- .bash_aliases | 373 +++++++++++++++++++++++--------------------------- 1 file changed, 170 insertions(+), 203 deletions(-) diff --git a/.bash_aliases b/.bash_aliases index 14a3ab3..218af08 100644 --- a/.bash_aliases +++ b/.bash_aliases @@ -36,13 +36,16 @@ echo_info() { _MESSAGES[info]+="${CYAN}ℹ️ $@${NC}\n" } +# INFO: +# ╭──────────╮ +# │ defaults │ +# ╰──────────╯ alias sudo="sudo -E" alias please="sudo" -alias whoami="command echo 'Ghost in a shell..'" - # ─< easier dir up >──────────────────────────────────────────────────────────────────────── alias ..="cd .." +alias ...="cd ../.." # ─< colored ip >─────────────────────────────────────────────────────────────────── alias ip="ip --color=always" @@ -68,6 +71,14 @@ else alias hl="grep --passthrough" fi +hm() { + find -type f -name "${1}" | wc -l +} + +if command_exists dog; then + alias dig="dog" +fi + # ─< define copy command >──────────────────────────────────────────────────────────────── if command_exists wl-copy; then copy() { @@ -122,6 +133,7 @@ choose_nvim() { # Default to nvim echo "command nvim" } + # Set up Neovim aliases based on environment if command_exists nvim; then alias cnvim="command nvim" @@ -253,82 +265,10 @@ __docker__() { alias dr="docker run --rm -it" alias drs="docker compose down && docker compose up -d --remove-orphans --force-recreate" alias ds="docker ps -a --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'" - alias dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'" - alias dl="docker compose logs -f" alias dc="docker compose" - # check_for_updates() { - # if ! command -v jq &>/dev/null; then - # echo -e "${RED}Error: jq is required but not installed. Please install jq.${NC}" - # return 1 - # fi - # - # if ! docker compose version &>/dev/null; then - # echo -e "${RED}Error: docker compose is not available.${NC}" - # return 1 - # fi - # - # local updated=false - # local images - # images=$(docker compose config --format json | jq -r '.services[] | .image' 2>/dev/null) - # - # if [[ -z "$images" ]]; then - # echo -e "${RED}Error: No Docker images found in the compose configuration.${NC}" - # return 1 - # fi - # - # for image in $images; do - # echo -e "${CYAN}Checking for updates for image: ${YELLOW}$image${NC}" - # - # # Get local image digest - # local local_digest - # local_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}') # 2>/dev/null - # - # # If no local digest exists, force check - # if [[ -z "$local_digest" ]]; then - # echo -e "${YELLOW}Image not present locally. Update needed.${NC}" - # updated=true - # continue - # fi - # - # # Get remote digest using pull --dry-run - # local remote_digest - # remote_digest=$(docker pull --quiet "$image" 2>/dev/null | awk '/Digest:/{print $2}') - # - # # Fallback to manifest inspect if dry-run fails - # if [[ -z "$remote_digest" ]]; then - # remote_digest=$(DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect -v "$image" | jq -r '.Descriptor.digest') - # fi - # - # if [[ -z "$remote_digest" ]]; then - # echo -e "${RED}Failed to retrieve remote digest. Performing forced check...${NC}" - # # Fallback method: Pull image and compare before/after digests - # local pre_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}') - # docker pull --quiet "$image" >/dev/null - # local post_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}') - # - # if [[ "$pre_pull_digest" != "$post_pull_digest" ]]; then - # echo -e "${GREEN}Update found during forced pull${NC}" - # updated=true - # fi - # continue - # fi - # - # # Compare digests - # if [[ "$local_digest" != "$remote_digest" ]]; then - # echo -e "${GREEN}Update available for $image${NC}" - # updated=true - # else - # echo -e "${YELLOW}No update available for $image${NC}" - # fi - # done - # - # if $updated; then - # echo -e "${CYAN}Pulling updates and recreating containers...${NC}" - # docker compose pull --quiet && docker compose up -d --force-recreate - # else - # echo -e "${GREEN}All images are up to date. No action needed.${NC}" - # fi - # } + alias dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'" + alias dlog="docker compose logs" + # Check for required dependencies check_for_updates() { local compose_cmd @@ -424,8 +364,6 @@ __git__() { } # ──────────────────────────────────────< functions >──────────────────────────────────── - # use git clone directly with / - # ex: gcl pik4li/ReDeploy.git --> https://github.com/pik4li/ReDeploy.git gcl() { if [ -z "$2" ]; then git clone --depth=1 "https://github.com/$1" @@ -434,7 +372,6 @@ __git__() { fi } - # the same with my own github server gck() { if [ -z "$2" ]; then git clone --recurse-submodules --depth=1 "https://git.k4li.de/$1" @@ -443,7 +380,6 @@ __git__() { fi } - # and to clone with ssh gcs() { if [ -z "$2" ]; then git clone --recurse-submodules --depth=1 "git@git.k4li.de:$1" @@ -452,8 +388,6 @@ __git__() { fi } - # submodule update, goes in every submodule, put't it to main, and pulls down the changes - # It then pulls down every change and commits it gsu() { echo "${CYAN}Updating submodules recursively with -> ${YELLOW}${BOLD}git submodule update --init --recursive${NC}" git submodule update --init --recursive && @@ -503,10 +437,6 @@ __git__() { fi } - # gwip command, to quickly add files and commit and push them directly in one command - # ex: gwip . -> will git add . && git commit -m "gwip" && git push - # ex: gwip "Files changed" -> "Files changed" is now the description, instead of gwip. - # ex: gwip install.sh -> will only add and commit/push the file 'install.sh' and commit with wip gwip() { # Fetch the latest changes from the remote git fetch @@ -517,17 +447,71 @@ __git__() { # Check if there are any changes on the remote branch if git diff --quiet "$branch" "origin/$branch"; then - if [ -n "$2" ]; then - local commit_files="${1:-.}" - local commit_message="${2:-wip}" + local commit_files=() + local commit_message + + # case "$1" in + # '.') + # commit_files=. + # commit_message="wip" + # + # echo "${CYAN}No changes on the remote branch. Adding changes and pushing with ${RED}${BOLD}'$commit_message'${NC}${CYAN} commit.${NC}" + # + # git add "$commit_files" + # git commit -m "$commit_message" + # git push + # ;; + # *) + if [[ -n "$1" ]]; then + for f in "$@"; do + if [ -f "./${f}" ] || [ -d "./${f}/" ]; then + commit_files+=("./${f}") + else + commit_message+="${f}" + break + fi + done else - local commit_message="${1:-wip}" + echo "${BOLD}${YELLOW}You have to provide the command like..:" + echo "${GREEN}'gwip ' or like 'gwip .'${NC}" + + return fi - echo "${CYAN}No changes on the remote branch. Adding changes and pushing with ${RED}${BOLD}'$commit_message'${NC}${CYAN} commit.${NC}" - git add "${commit_files:-.}" - git commit -m "$commit_message" - git push + case $commit_files in + './.') commit_message="wip" ;; + *) + local trimmed_files + + for f in "${commit_files[@]}"; do + trimmed_files+=("${f#./}") + done + + echo "${GREEN}Committed files/folders: ${BOLD}${trimmed_files[*]}${NC}" + ;; + esac + + if [ -n "$commit_files" ] || [ -n "$commit_message" ]; then + [[ -z "$commit_files" ]] && commit_files=. + [[ -z "$commit_message" ]] && commit_message=wip + + echo "${CYAN}No changes on the remote branch. Adding changes and pushing with ${RED}${BOLD}'$commit_message'${NC}${CYAN} commit.${NC}" + + if (("${#commit_files}" <= 1)); then + git add $commit_files + else + for f in "${commit_files[@]}"; do + git add $f + done + fi + git commit -m "${commit_message:-wip}" + git push + else + echo "${RED}${BOLD}There was something wrong with:${NC}" + echo "${YELLOW}- commit-files '$commit_files'" + echo "or.." + echo "${YELLOW}- commit-message '$commit_message'${NC}" + fi else echo "${RED}${BOLD}There are changes on the remote branch. Please pull the latest changes first.${NC}" fi @@ -545,14 +529,27 @@ fi # Tmux session manager if command_exists tmux; then + sleep 0.01 + if [ -z "$TMUX" ]; then + sleep 0.01 + # Attach to existing session if any, otherwise create one named 'main' + if tmux ls &>/dev/null; then + sleep 0.01 + tmux attach-session + else + sleep 0.01 + tmux new-session + fi + fi + ta() { if tmux list-sessions >/dev/null 2>&1; then echo "-- tmux session active! | Connecting to active session --" - sleep 0.1 + sleep 0.3 tmux attach else echo "-- No tmux session found! | Creating one --" - sleep 0.1 + sleep 0.3 tmux fi } @@ -562,29 +559,6 @@ else echo_missing "tmux" fi -# Zellij session manager -# if command_exists zellij; then -# za() { -# # local zsession='zellij list-sessions | tail -1 | grep -q EXITED' -# if ! zellij list-sessions >/dev/null 2>&1; then -# echo "-- Zellij session active! | Connecting to existing session --" -# sleep 0.3 -# zellij attach -# elif ! zellij list-sessions | tail -1 | grep -q EXITED; then -# echo "-- Zellij session active! | Connecting to existing session --" -# sleep 0.3 -# zellij attach -# else -# echo "-- No Zellij session active! | Creating one --" -# sleep 0.3 -# zellij -# fi -# } -# alias ta="za" -# -# # alias zs="zellij setup --dump-config > $HOME/.config/zellij/config.yaml" -# fi - # Function to get the IP address get_ip() { ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1 @@ -596,97 +570,90 @@ if command_exists hugo; then alias hs='hugo server -D --noHTTPCache --disableFastRender' # --bind "$(get_ip)"' fi -__get_Packager__() { - # Load OS release information - [ -f /etc/os-release ] && . /etc/os-release || return 1 +missing() { + local e=( + bash + zsh + fzf + curl + git + docker + nvim + ) - DISTRO="${ID}:${ID_LIKE}" + for i in "$e{[@]}"; do + if ! command_exists $i; then + echo_missing "$i" + fi + done +} - case "$DISTRO" in - *debian*) - pkg_install() { - if command_exists nala; then - $_sudo nala install --assume-yes "$@" - else - $_sudo apt install --assume-yes "$@" - fi - } +# INFO: +# ╭─────────────────╮ +# │ other functions │ +# ╰─────────────────╯ +source-script() { + local url="$1" + local import="$(mktemp)" - if command_exists nala; then - alias search="nala search" - alias update="$_sudo nala update && $_sudo nala upgrade --full" - alias remove="$_sudo nala purge" + # ─< if $1 is a local file, source this one instead >───────────────────────────────────── + if [ -f "$url" ]; then + source "$url" + sleep 0.1 + return 0 + else + echo_info "Sourcing external script:${NC} $url" + # ─< if $1 is a url, grab it and source it, also deletes afterwards >───────────────────── + if command_exists curl; then + curl -fsSL $url -o $import + elif command_exists wget; then + wget -o $import $url else - alias search="apt-cache search" - alias update="$_sudo apt update && $_sudo apt upgrade" - alias remove="$_sudo apt purge" + echo "curl/wget is required, but missing.." + exit 69 fi - alias unbreak="$_sudo dpkg --configure -a" - alias install="pkg_install" - ;; - *arch*) - pkg_install() { - if command_exists paru; then - paru -S --color always --noconfirm --needed "$@" - elif command_exists yay; then - yay -S --color always --noconfirm --needed "$@" - else - $_sudo pacman -S --color always --noconfirm --needed "$@" - fi - } - if command_exists paru; then - alias search="paru -Ss --color always" - # alias install="paru -S --color always --noconfirm" - alias update="paru -Syu --color always" - alias remove="paru -R --color always" - elif command_exists yay; then - alias search="yay -Ss --color always" - # alias install="yay -S --noconfirm --color always" - alias update="yay -Syu --color always" - alias remove="yay -R --color always" - else - alias search="$_sudo pacman -Ss --color always" - # alias install="$_sudo pacman -S --noconfirm --color always" - alias update="$_sudo pacman -Syu --color always" - alias remove="$_sudo pacman -R --color always" - fi - alias install="pkg_install" - ;; - *rhel* | *fedora*) - alias search="dnf search" - alias install="$_sudo dnf install -y --skip-missing" - alias update="$_sudo dnf update -y" - alias remove="$_sudo dnf remove -y" - ;; - *suse*) - alias search="zypper search" - alias install="$_sudo zypper install --no-confirm" - alias update="$_sudo zypper update" - alias remove="$_sudo zypper remove" - ;; - *alpine*) - alias install="$_sudo apk add" - alias update="$_sudo apk update && $_sudo apk upgrade" - alias remove="$_sudo apk del" - ;; - *nixos*) - echo_info "Using NIX!!" - alias update="$_sudo nixos-rebuild switch" - # alias install="$_sudo nix-env -iA nixos." - install() { - "$_sudo nix-end -iA nixos.$@" - } - alias edit="$_sudo -E $EDITOR /etc/nixos/configuration.nix" - ;; - *) - echo_error "Unsupported distro: $ID" - ;; - esac + source "$import" + sleep 0.1 + rm -f "$import" + fi +} + +resolve() { + local quiet=false + if [[ "$1" == "-q" ]]; then + quiet=true + shift + fi + + local ip="$1" + local server="$2" + local dig_out + dig_out=$(command dig +noall +answer -x "$ip" ${server:+@$server} 2>/dev/null) + + local line ptr hostname time + line=$(grep 'PTR' <<<"$dig_out") + ptr=$(awk '{print $4}' <<<"$line") + time=$(awk '{print $2}' <<<"$dig_out") + hostname=$(awk '{print $5}' <<<"$line" | sed 's/\.$//') + + if $quiet; then + [[ -n "$hostname" ]] && echo "$hostname" + return + fi + + if [[ -n "$hostname" ]]; then + # ✅ resolved + printf "✅ ${BOLD}${GREEN}%s${NC} \033[1;90m%s${NC} \033[32m%s${NC} \033[2m%s ms${NC}\n" \ + "$ptr" "$ip" "$hostname" "$time" + else + # ❌ not resolved + printf "❌ \033[1;31m%s${NC} \033[1;90m%s${NC} \033[1;31mNOT FOUND${NC} \033[2m%s ms\033[0m\n" \ + "PTR" "$ip" "${time:-0}" + fi } main() { - __get_Packager__ # ─< g stands for GIT >───────────────────────────────────────────────────────────────────── if command_exists git; then __git__ @@ -697,7 +664,7 @@ main() { # ─< d stands for docker >────────────────────────────────────────────────────────────────── if command_exists docker; then __docker__ - elif command_exists podman-compose; then + elif command_exists podman; then __podman__ else echo_missing "docker | podman"