addet some needed functions/aliase

This commit is contained in:
pika 2025-07-05 13:09:20 +02:00
parent 963fcf8ab6
commit 3389fe8cc7

View file

@ -36,13 +36,16 @@ echo_info() {
_MESSAGES[info]+="${CYAN} $@${NC}\n" _MESSAGES[info]+="${CYAN} $@${NC}\n"
} }
# INFO:
# ╭──────────╮
# │ defaults │
# ╰──────────╯
alias sudo="sudo -E" alias sudo="sudo -E"
alias please="sudo" alias please="sudo"
alias whoami="command echo 'Ghost in a shell..'"
# ─< easier dir up >──────────────────────────────────────────────────────────────────────── # ─< easier dir up >────────────────────────────────────────────────────────────────────────
alias ..="cd .." alias ..="cd .."
alias ...="cd ../.."
# ─< colored ip >─────────────────────────────────────────────────────────────────── # ─< colored ip >───────────────────────────────────────────────────────────────────
alias ip="ip --color=always" alias ip="ip --color=always"
@ -68,6 +71,14 @@ else
alias hl="grep --passthrough" alias hl="grep --passthrough"
fi fi
hm() {
find -type f -name "${1}" | wc -l
}
if command_exists dog; then
alias dig="dog"
fi
# ─< define copy command >──────────────────────────────────────────────────────────────── # ─< define copy command >────────────────────────────────────────────────────────────────
if command_exists wl-copy; then if command_exists wl-copy; then
copy() { copy() {
@ -122,6 +133,7 @@ choose_nvim() {
# Default to nvim # Default to nvim
echo "command nvim" echo "command nvim"
} }
# Set up Neovim aliases based on environment # Set up Neovim aliases based on environment
if command_exists nvim; then if command_exists nvim; then
alias cnvim="command nvim" alias cnvim="command nvim"
@ -253,82 +265,10 @@ __docker__() {
alias dr="docker run --rm -it" alias dr="docker run --rm -it"
alias drs="docker compose down && docker compose up -d --remove-orphans --force-recreate" 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 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" alias dc="docker compose"
# check_for_updates() { alias dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
# if ! command -v jq &>/dev/null; then alias dlog="docker compose logs"
# 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
# }
# Check for required dependencies # Check for required dependencies
check_for_updates() { check_for_updates() {
local compose_cmd local compose_cmd
@ -424,8 +364,6 @@ __git__() {
} }
# ──────────────────────────────────────< functions >──────────────────────────────────── # ──────────────────────────────────────< functions >────────────────────────────────────
# use git clone directly with <username>/<repo>
# ex: gcl pik4li/ReDeploy.git --> https://github.com/pik4li/ReDeploy.git
gcl() { gcl() {
if [ -z "$2" ]; then if [ -z "$2" ]; then
git clone --depth=1 "https://github.com/$1" git clone --depth=1 "https://github.com/$1"
@ -434,7 +372,6 @@ __git__() {
fi fi
} }
# the same with my own github server
gck() { gck() {
if [ -z "$2" ]; then if [ -z "$2" ]; then
git clone --recurse-submodules --depth=1 "https://git.k4li.de/$1" git clone --recurse-submodules --depth=1 "https://git.k4li.de/$1"
@ -443,7 +380,6 @@ __git__() {
fi fi
} }
# and to clone with ssh
gcs() { gcs() {
if [ -z "$2" ]; then if [ -z "$2" ]; then
git clone --recurse-submodules --depth=1 "git@git.k4li.de:$1" git clone --recurse-submodules --depth=1 "git@git.k4li.de:$1"
@ -452,8 +388,6 @@ __git__() {
fi 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() { gsu() {
echo "${CYAN}Updating submodules recursively with -> ${YELLOW}${BOLD}git submodule update --init --recursive${NC}" echo "${CYAN}Updating submodules recursively with -> ${YELLOW}${BOLD}git submodule update --init --recursive${NC}"
git submodule update --init --recursive && git submodule update --init --recursive &&
@ -503,10 +437,6 @@ __git__() {
fi 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() { gwip() {
# Fetch the latest changes from the remote # Fetch the latest changes from the remote
git fetch git fetch
@ -517,17 +447,71 @@ __git__() {
# Check if there are any changes on the remote branch # Check if there are any changes on the remote branch
if git diff --quiet "$branch" "origin/$branch"; then if git diff --quiet "$branch" "origin/$branch"; then
if [ -n "$2" ]; then local commit_files=()
local commit_files="${1:-.}" local commit_message
local commit_message="${2:-wip}"
# 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 else
local commit_message="${1:-wip}" echo "${BOLD}${YELLOW}You have to provide the command like..:"
echo "${GREEN}'gwip <list of files> <commit message>' or like 'gwip .'${NC}"
return
fi fi
echo "${CYAN}No changes on the remote branch. Adding changes and pushing with ${RED}${BOLD}'$commit_message'${NC}${CYAN} commit.${NC}" case $commit_files in
git add "${commit_files:-.}" './.') commit_message="wip" ;;
git commit -m "$commit_message" *)
git push 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 else
echo "${RED}${BOLD}There are changes on the remote branch. Please pull the latest changes first.${NC}" echo "${RED}${BOLD}There are changes on the remote branch. Please pull the latest changes first.${NC}"
fi fi
@ -545,14 +529,27 @@ fi
# Tmux session manager # Tmux session manager
if command_exists tmux; then 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() { ta() {
if tmux list-sessions >/dev/null 2>&1; then if tmux list-sessions >/dev/null 2>&1; then
echo "-- tmux session active! | Connecting to active session --" echo "-- tmux session active! | Connecting to active session --"
sleep 0.1 sleep 0.3
tmux attach tmux attach
else else
echo "-- No tmux session found! | Creating one --" echo "-- No tmux session found! | Creating one --"
sleep 0.1 sleep 0.3
tmux tmux
fi fi
} }
@ -562,29 +559,6 @@ else
echo_missing "tmux" echo_missing "tmux"
fi 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 # Function to get the IP address
get_ip() { get_ip() {
ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1 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)"' alias hs='hugo server -D --noHTTPCache --disableFastRender' # --bind "$(get_ip)"'
fi fi
__get_Packager__() { missing() {
# Load OS release information local e=(
[ -f /etc/os-release ] && . /etc/os-release || return 1 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 # INFO:
*debian*) # ╭─────────────────╮
pkg_install() { # │ other functions │
if command_exists nala; then # ╰─────────────────╯
$_sudo nala install --assume-yes "$@" source-script() {
else local url="$1"
$_sudo apt install --assume-yes "$@" local import="$(mktemp)"
fi
}
if command_exists nala; then # ─< if $1 is a local file, source this one instead >─────────────────────────────────────
alias search="nala search" if [ -f "$url" ]; then
alias update="$_sudo nala update && $_sudo nala upgrade --full" source "$url"
alias remove="$_sudo nala purge" 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 else
alias search="apt-cache search" echo "curl/wget is required, but missing.."
alias update="$_sudo apt update && $_sudo apt upgrade" exit 69
alias remove="$_sudo apt purge"
fi fi
alias unbreak="$_sudo dpkg --configure -a" source "$import"
alias install="pkg_install" sleep 0.1
;; rm -f "$import"
*arch*) fi
pkg_install() { }
if command_exists paru; then
paru -S --color always --noconfirm --needed "$@" resolve() {
elif command_exists yay; then local quiet=false
yay -S --color always --noconfirm --needed "$@" if [[ "$1" == "-q" ]]; then
else quiet=true
$_sudo pacman -S --color always --noconfirm --needed "$@" shift
fi fi
}
if command_exists paru; then local ip="$1"
alias search="paru -Ss --color always" local server="$2"
# alias install="paru -S --color always --noconfirm" local dig_out
alias update="paru -Syu --color always" dig_out=$(command dig +noall +answer -x "$ip" ${server:+@$server} 2>/dev/null)
alias remove="paru -R --color always"
elif command_exists yay; then local line ptr hostname time
alias search="yay -Ss --color always" line=$(grep 'PTR' <<<"$dig_out")
# alias install="yay -S --noconfirm --color always" ptr=$(awk '{print $4}' <<<"$line")
alias update="yay -Syu --color always" time=$(awk '{print $2}' <<<"$dig_out")
alias remove="yay -R --color always" hostname=$(awk '{print $5}' <<<"$line" | sed 's/\.$//')
else
alias search="$_sudo pacman -Ss --color always" if $quiet; then
# alias install="$_sudo pacman -S --noconfirm --color always" [[ -n "$hostname" ]] && echo "$hostname"
alias update="$_sudo pacman -Syu --color always" return
alias remove="$_sudo pacman -R --color always" fi
fi
alias install="pkg_install" if [[ -n "$hostname" ]]; then
;; # ✅ resolved
*rhel* | *fedora*) printf "${BOLD}${GREEN}%s${NC} \033[1;90m%s${NC} \033[32m%s${NC} \033[2m%s ms${NC}\n" \
alias search="dnf search" "$ptr" "$ip" "$hostname" "$time"
alias install="$_sudo dnf install -y --skip-missing" else
alias update="$_sudo dnf update -y" # ❌ not resolved
alias remove="$_sudo dnf remove -y" 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}"
*suse*) fi
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
} }
main() { main() {
__get_Packager__
# ─< g stands for GIT >───────────────────────────────────────────────────────────────────── # ─< g stands for GIT >─────────────────────────────────────────────────────────────────────
if command_exists git; then if command_exists git; then
__git__ __git__
@ -697,7 +664,7 @@ main() {
# ─< d stands for docker >────────────────────────────────────────────────────────────────── # ─< d stands for docker >──────────────────────────────────────────────────────────────────
if command_exists docker; then if command_exists docker; then
__docker__ __docker__
elif command_exists podman-compose; then elif command_exists podman; then
__podman__ __podman__
else else
echo_missing "docker | podman" echo_missing "docker | podman"