469 lines
20 KiB
Bash
469 lines
20 KiB
Bash
# HISTFILE Configuration (Bash equivalent)
|
|
HISTFILE=~/.bash_history
|
|
HISTSIZE=1000
|
|
HISTFILESIZE=2000 # Adjusted to match both histfile and size criteria
|
|
|
|
# GPU Related shenanigans
|
|
if [ "$(hostname)" = "mennos-desktop" ]; then
|
|
export DRI_PRIME=1
|
|
export MESA_VK_DEVICE_SELECT=1002:744c
|
|
fi
|
|
|
|
if [ -f /etc/os-release ]; then
|
|
distro=$(awk -F= '/^NAME/{print $ssss2}' /etc/os-release | tr -d '"')
|
|
if [[ "$distro" == *"Pop!_OS"* ]]; then
|
|
export CGO_CFLAGS="-I/usr/include"
|
|
fi
|
|
fi
|
|
|
|
# For microsoft-standard-WSL2 in uname -a
|
|
if [[ "$(uname -a)" == *"microsoft-standard-WSL2"* ]]; then
|
|
source $HOME/.agent-bridge.sh
|
|
alias winget='winget.exe'
|
|
fi
|
|
|
|
# Docker Compose Alias (Mostly for old shell scripts)
|
|
alias docker-compose='docker compose'
|
|
|
|
# Modern tools aliases
|
|
alias l="eza --header --long --git --group-directories-first --group --icons --color=always --sort=name --hyperlink -o --no-permissions"
|
|
alias ll='l'
|
|
alias la='l -a'
|
|
alias cat='bat'
|
|
alias du='dust'
|
|
alias df='duf'
|
|
alias augp='sudo apt update && sudo apt upgrade -y && sudo apt autopurge -y && sudo apt autoclean'
|
|
|
|
# Docker Aliases
|
|
alias d='docker'
|
|
alias dc='docker compose'
|
|
alias dce='docker compose exec'
|
|
alias dcl='docker compose logs'
|
|
alias dcd='docker compose down'
|
|
alias dcu='docker compose up'
|
|
alias dcp='docker compose ps'
|
|
alias dcps='docker compose ps'
|
|
alias dcpr='dcp && dcd && dcu -d && dcl -f'
|
|
alias dcr='dcd && dcu -d && dcl -f'
|
|
alias ddpul='docker compose down && docker compose pull && docker compose up -d && docker compose logs -f'
|
|
alias docker-nuke='docker kill $(docker ps -q) && docker rm $(docker ps -a -q) && docker system prune --all --volumes --force && docker volume prune --force'
|
|
|
|
# Git aliases
|
|
alias g='git'
|
|
alias gg='git pull'
|
|
alias gl='git log --stat'
|
|
alias gp='git push'
|
|
alias gs='git status -s'
|
|
alias gst='git status'
|
|
alias ga='git add'
|
|
alias gc='git commit'
|
|
alias gcm='git commit -m'
|
|
alias gco='git checkout'
|
|
alias gcb='git checkout -b'
|
|
|
|
# Kubernetes aliases (Minikube)
|
|
alias kubectl="minikube kubectl --"
|
|
|
|
# netstat port in use check
|
|
alias port='netstat -atupn | grep LISTEN'
|
|
|
|
# Check if a specific port is in use with detailed process information
|
|
inuse() {
|
|
# Color definitions
|
|
local RED='\033[0;31m'
|
|
local GREEN='\033[0;32m'
|
|
local YELLOW='\033[1;33m'
|
|
local BLUE='\033[0;34m'
|
|
local CYAN='\033[0;36m'
|
|
local BOLD='\033[1m'
|
|
local NC='\033[0m' # No Color
|
|
|
|
# Input validation
|
|
if [ $# -eq 0 ]; then
|
|
echo -e "${RED}Usage:${NC} inuse <port_number>"
|
|
echo -e "${YELLOW} inuse --list${NC}"
|
|
echo -e "${YELLOW} inuse --help${NC}"
|
|
echo -e "${YELLOW}Example:${NC} inuse 80"
|
|
echo -e "${YELLOW} inuse --list${NC}"
|
|
return 1
|
|
fi
|
|
|
|
# Handle --help option
|
|
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
|
echo -e "${CYAN}${BOLD}inuse - Check if a port is in use${NC}"
|
|
echo
|
|
echo -e "${BOLD}USAGE:${NC}"
|
|
echo -e " inuse <port_number> Check if a specific port is in use"
|
|
echo -e " inuse --list, -l List all Docker services with listening ports"
|
|
echo -e " inuse --help, -h Show this help message"
|
|
echo
|
|
echo -e "${BOLD}EXAMPLES:${NC}"
|
|
echo -e " ${GREEN}inuse 80${NC} Check if port 80 is in use"
|
|
echo -e " ${GREEN}inuse 3000${NC} Check if port 3000 is in use"
|
|
echo -e " ${GREEN}inuse --list${NC} Show all Docker services with ports"
|
|
echo
|
|
echo -e "${BOLD}DESCRIPTION:${NC}"
|
|
echo -e " The inuse function checks if a specific port is in use and identifies"
|
|
echo -e " the process using it. It can detect regular processes, Docker containers"
|
|
echo -e " with published ports, and containers using host networking."
|
|
echo
|
|
echo -e "${BOLD}OUTPUT:${NC}"
|
|
echo -e " ${GREEN}✓${NC} Port is in use - shows process name, PID, and Docker info if applicable"
|
|
echo -e " ${RED}✗${NC} Port is free"
|
|
echo -e " ${YELLOW}⚠${NC} Port is in use but process cannot be identified"
|
|
echo
|
|
return 0
|
|
fi
|
|
|
|
# Handle --list option
|
|
if [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
echo -e "${RED}Error:${NC} Docker is not available"
|
|
return 1
|
|
fi
|
|
|
|
echo -e "${CYAN}${BOLD}Docker Services with Listening Ports:${NC}"
|
|
echo
|
|
|
|
# Get all running containers
|
|
local containers=$(docker ps --format "{{.Names}}" 2>/dev/null)
|
|
if [ -z "$containers" ]; then
|
|
echo -e "${YELLOW}No running Docker containers found${NC}"
|
|
return 0
|
|
fi
|
|
|
|
local found_services=false
|
|
while IFS= read -r container; do
|
|
# Get port mappings for this container
|
|
local ports=$(docker port "$container" 2>/dev/null)
|
|
if [ -n "$ports" ]; then
|
|
# Get container image name (clean it up)
|
|
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
|
local clean_image=$(echo "$image" | sed 's/sha256:[a-f0-9]*/[image-hash]/' | sed 's/^.*\///')
|
|
|
|
echo -e "${GREEN}📦 ${BOLD}$container${NC} ${CYAN}($clean_image)${NC}"
|
|
|
|
# Parse and display ports nicely
|
|
echo "$ports" | while IFS= read -r port_line; do
|
|
if [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*0\.0\.0\.0:([0-9]+) ]]; then
|
|
local container_port="${BASH_REMATCH[1]}"
|
|
local protocol="${BASH_REMATCH[2]}"
|
|
local host_port="${BASH_REMATCH[3]}"
|
|
echo -e "${CYAN} ├─ Port ${BOLD}$host_port${NC}${CYAN} → $container_port ($protocol)${NC}"
|
|
elif [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*\[::\]:([0-9]+) ]]; then
|
|
local container_port="${BASH_REMATCH[1]}"
|
|
local protocol="${BASH_REMATCH[2]}"
|
|
local host_port="${BASH_REMATCH[3]}"
|
|
echo -e "${CYAN} ├─ Port ${BOLD}$host_port${NC}${CYAN} → $container_port ($protocol) [IPv6]${NC}"
|
|
fi
|
|
done
|
|
echo
|
|
found_services=true
|
|
fi
|
|
done <<< "$containers"
|
|
|
|
# Also check for host networking containers
|
|
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
|
if [ -n "$host_containers" ]; then
|
|
echo -e "${YELLOW}${BOLD}Host Networking Containers:${NC}"
|
|
while IFS= read -r container; do
|
|
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
|
local clean_image=$(echo "$image" | sed 's/sha256:[a-f0-9]*/[image-hash]/' | sed 's/^.*\///')
|
|
echo -e "${YELLOW}🌐 ${BOLD}$container${NC} ${CYAN}($clean_image)${NC} ${YELLOW}- uses host networking${NC}"
|
|
done <<< "$host_containers"
|
|
echo
|
|
found_services=true
|
|
fi
|
|
|
|
if [ "$found_services" = false ]; then
|
|
echo -e "${YELLOW}No Docker services with exposed ports found${NC}"
|
|
fi
|
|
|
|
return 0
|
|
fi
|
|
|
|
local port="$1"
|
|
|
|
# Validate port number
|
|
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
|
|
echo -e "${RED}Error:${NC} Invalid port number. Must be between 1 and 65535."
|
|
return 1
|
|
fi
|
|
|
|
# Check if port is in use first
|
|
local port_in_use=false
|
|
if command -v ss >/dev/null 2>&1; then
|
|
if ss -tulpn 2>/dev/null | grep -q ":$port "; then
|
|
port_in_use=true
|
|
fi
|
|
elif command -v netstat >/dev/null 2>&1; then
|
|
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
|
|
port_in_use=true
|
|
fi
|
|
fi
|
|
|
|
if [ "$port_in_use" = false ]; then
|
|
echo -e "${RED}✗ Port $port is FREE${NC}"
|
|
return 1
|
|
fi
|
|
|
|
# Port is in use, now find what's using it
|
|
local found_process=false
|
|
|
|
# Method 1: Try netstat first (most reliable for PID info)
|
|
if command -v netstat >/dev/null 2>&1; then
|
|
local netstat_result=$(netstat -tulpn 2>/dev/null | grep ":$port ")
|
|
if [ -n "$netstat_result" ]; then
|
|
while IFS= read -r line; do
|
|
local pid=$(echo "$line" | awk '{print $7}' | cut -d'/' -f1)
|
|
local process_name=$(echo "$line" | awk '{print $7}' | cut -d'/' -f2)
|
|
local protocol=$(echo "$line" | awk '{print $1}')
|
|
|
|
if [[ "$pid" =~ ^[0-9]+$ ]] && [ -n "$process_name" ]; then
|
|
# Check if it's a Docker container
|
|
local docker_info=""
|
|
if command -v docker >/dev/null 2>&1; then
|
|
# Check for docker-proxy
|
|
if [ "$process_name" = "docker-proxy" ]; then
|
|
local container_name=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
|
if [ -n "$container_name" ]; then
|
|
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
|
else
|
|
docker_info=" ${CYAN}(Docker proxy)${NC}"
|
|
fi
|
|
else
|
|
# Check if process is in a container by examining cgroup
|
|
if [ -f "/proc/$pid/cgroup" ] && grep -q docker "/proc/$pid/cgroup" 2>/dev/null; then
|
|
local container_id=$(cat "/proc/$pid/cgroup" 2>/dev/null | grep docker | grep -o '[a-f0-9]\{64\}' | head -1)
|
|
if [ -n "$container_id" ]; then
|
|
local container_name=$(docker inspect "$container_id" 2>/dev/null | grep -o '"Name": *"[^"]*"' | cut -d'"' -f4 | sed 's/^\/*//' | head -1)
|
|
if [ -n "$container_name" ]; then
|
|
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
|
else
|
|
docker_info=" ${CYAN}(Docker: ${container_id:0:12})${NC}"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
|
found_process=true
|
|
fi
|
|
done <<< "$netstat_result"
|
|
fi
|
|
fi
|
|
|
|
# Method 2: Try ss if netstat didn't work
|
|
if [ "$found_process" = false ] && command -v ss >/dev/null 2>&1; then
|
|
local ss_result=$(ss -tulpn 2>/dev/null | grep ":$port ")
|
|
if [ -n "$ss_result" ]; then
|
|
while IFS= read -r line; do
|
|
local pid=$(echo "$line" | grep -o 'pid=[0-9]*' | cut -d'=' -f2)
|
|
local protocol=$(echo "$line" | awk '{print $1}')
|
|
|
|
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
|
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
|
if [ -n "$process_name" ]; then
|
|
# Check for Docker container
|
|
local docker_info=""
|
|
if command -v docker >/dev/null 2>&1; then
|
|
if [ "$process_name" = "docker-proxy" ]; then
|
|
local container_name=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
|
if [ -n "$container_name" ]; then
|
|
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
|
else
|
|
docker_info=" ${CYAN}(Docker proxy)${NC}"
|
|
fi
|
|
elif [ -f "/proc/$pid/cgroup" ] && grep -q docker "/proc/$pid/cgroup" 2>/dev/null; then
|
|
local container_id=$(cat "/proc/$pid/cgroup" 2>/dev/null | grep docker | grep -o '[a-f0-9]\{64\}' | head -1)
|
|
if [ -n "$container_id" ]; then
|
|
local container_name=$(docker inspect "$container_id" 2>/dev/null | grep -o '"Name": *"[^"]*"' | cut -d'"' -f4 | sed 's/^\/*//' | head -1)
|
|
if [ -n "$container_name" ]; then
|
|
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
|
else
|
|
docker_info=" ${CYAN}(Docker: ${container_id:0:12})${NC}"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
|
found_process=true
|
|
fi
|
|
fi
|
|
done <<< "$ss_result"
|
|
fi
|
|
fi
|
|
|
|
# Method 3: Try fuser as last resort
|
|
if [ "$found_process" = false ] && command -v fuser >/dev/null 2>&1; then
|
|
local fuser_pids=$(fuser "$port/tcp" 2>/dev/null)
|
|
if [ -n "$fuser_pids" ]; then
|
|
for pid in $fuser_pids; do
|
|
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
|
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
|
if [ -n "$process_name" ]; then
|
|
echo -e "${GREEN}✓ Port $port (tcp) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}"
|
|
found_process=true
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
fi
|
|
fi
|
|
|
|
# Method 4: Check for Docker containers more accurately
|
|
if [ "$found_process" = false ] && command -v docker >/dev/null 2>&1; then
|
|
# First, try to find containers with published ports matching our port
|
|
local container_with_port=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
|
if [ -n "$container_with_port" ]; then
|
|
local image=$(docker inspect "$container_with_port" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
|
echo -e "${GREEN}✓ Port $port in use by Docker container ${BOLD}$container_with_port${NC} ${CYAN}(published port, image: $image)${NC}"
|
|
found_process=true
|
|
else
|
|
# Only check host networking containers if we haven't found anything else
|
|
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
|
if [ -n "$host_containers" ]; then
|
|
local host_container_count=$(echo "$host_containers" | wc -l)
|
|
if [ "$host_container_count" -eq 1 ]; then
|
|
# Only one host networking container, likely candidate
|
|
local image=$(docker inspect "$host_containers" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
|
echo -e "${YELLOW}⚠ Port $port possibly in use by Docker container ${BOLD}$host_containers${NC} ${CYAN}(host networking, image: $image)${NC}"
|
|
found_process=true
|
|
else
|
|
# Multiple host networking containers, can't determine which one
|
|
echo -e "${YELLOW}⚠ Port $port is in use, multiple Docker containers using host networking:${NC}"
|
|
while IFS= read -r container; do
|
|
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
|
echo -e "${CYAN} - $container (image: $image)${NC}"
|
|
done <<< "$host_containers"
|
|
found_process=true
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# If we still haven't found the process, show a generic message
|
|
if [ "$found_process" = false ]; then
|
|
echo -e "${YELLOW}⚠ Port $port is in use but unable to identify the process${NC}"
|
|
echo -e "${CYAN} This might be due to insufficient permissions or the process being in a different namespace${NC}"
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
# random string (Syntax: random <length>)
|
|
alias random='openssl rand -base64'
|
|
|
|
# Alias for ls to l but only if it's an interactive shell because we don't want to override ls in scripts which could blow up in our face
|
|
if [ -t 1 ]; then
|
|
alias ls='l'
|
|
fi
|
|
|
|
# PATH Manipulation
|
|
export DOTFILES_PATH=$HOME/.dotfiles
|
|
export PATH=$PATH:$HOME/.local/bin
|
|
export PATH=$PATH:$HOME/.cargo/bin
|
|
export PATH=$PATH:$DOTFILES_PATH/bin
|
|
export PATH=$PATH:$HOME/.spicetify
|
|
|
|
# Include pnpm if it exists
|
|
if [ -d "$HOME/.local/share/pnpm" ]; then
|
|
export PATH=$PATH:$HOME/.local/share/pnpm
|
|
fi
|
|
|
|
# Miniconda
|
|
export PATH="$HOME/miniconda3/bin:$PATH"
|
|
|
|
# In case $HOME/.flutter/flutter/bin is found, we can add it to the PATH
|
|
if [ -d "$HOME/.flutter/flutter/bin" ]; then
|
|
export PATH=$PATH:$HOME/.flutter/flutter/bin
|
|
export PATH="$PATH":"$HOME/.pub-cache/bin"
|
|
|
|
# Flutter linux fixes:
|
|
export CPPFLAGS="-I/usr/include"
|
|
export LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lbz2"
|
|
export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig:$PKG_CONFIG_PATH
|
|
fi
|
|
|
|
# Add flatpak to XDG_DATA_DIRS
|
|
export XDG_DATA_DIRS=$XDG_DATA_DIRS:/usr/share:/var/lib/flatpak/exports/share:$HOME/.local/share/flatpak/exports/share
|
|
|
|
# Allow unfree nixos
|
|
export NIXPKGS_ALLOW_UNFREE=1
|
|
|
|
# Allow insecure nixpkgs
|
|
export NIXPKGS_ALLOW_INSECURE=1
|
|
|
|
# Tradaware / DiscountOffice Configuration
|
|
if [ -d "/home/menno/Projects/Work" ]; then
|
|
export TRADAWARE_DEVOPS=true
|
|
fi
|
|
|
|
# 1Password Source Plugin (Assuming bash compatibility)
|
|
if [ -f /home/menno/.config/op/plugins.sh ]; then
|
|
source /home/menno/.config/op/plugins.sh
|
|
fi
|
|
|
|
# Initialize starship if available
|
|
if ! command -v starship &> /dev/null; then
|
|
echo "FYI, starship not found"
|
|
else
|
|
export STARSHIP_ENABLE_RIGHT_PROMPT=true
|
|
export STARSHIP_ENABLE_BASH_CONTINUATION=true
|
|
eval "$(starship init bash)"
|
|
fi
|
|
|
|
# Read .op_sat
|
|
if [ -f ~/.op_sat ]; then
|
|
export OP_SERVICE_ACCOUNT_TOKEN=$(cat ~/.op_sat)
|
|
|
|
# Ensure .op_sat is 0600 and only readable by the owner
|
|
if [ "$(stat -c %a ~/.op_sat)" != "600" ]; then
|
|
echo "WARNING: ~/.op_sat is not 0600, please fix this!"
|
|
fi
|
|
|
|
if [ "$(stat -c %U ~/.op_sat)" != "$(whoami)" ]; then
|
|
echo "WARNING: ~/.op_sat is not owned by the current user, please fix this!"
|
|
fi
|
|
fi
|
|
|
|
# Source nix home-manager
|
|
if [ -f "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh" ]; then
|
|
. "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh"
|
|
fi
|
|
|
|
# Source ble.sh if it exists
|
|
if [[ -f "${HOME}/.nix-profile/share/blesh/ble.sh" ]]; then
|
|
source "${HOME}/.nix-profile/share/blesh/ble.sh"
|
|
|
|
# Custom function for fzf history search
|
|
function fzf_history_search() {
|
|
local selected
|
|
selected=$(history | fzf --tac --height=40% --layout=reverse --border --info=inline \
|
|
--query="$READLINE_LINE" \
|
|
--color 'fg:#ebdbb2,bg:#282828,hl:#fabd2f,fg+:#ebdbb2,bg+:#3c3836,hl+:#fabd2f' \
|
|
--color 'info:#83a598,prompt:#bdae93,spinner:#fabd2f,pointer:#83a598,marker:#fe8019,header:#665c54' \
|
|
| sed 's/^ *[0-9]* *//')
|
|
if [[ -n "$selected" ]]; then
|
|
READLINE_LINE="$selected"
|
|
READLINE_POINT=${#selected}
|
|
fi
|
|
ble-redraw-prompt
|
|
}
|
|
|
|
# Bind Ctrl+R to our custom function
|
|
bind -x '"\C-r": fzf_history_search'
|
|
fi
|
|
|
|
# In case a basrc.local exists, source it
|
|
if [ -f $HOME/.bashrc.local ]; then
|
|
source $HOME/.bashrc.local
|
|
fi
|
|
|
|
# Display a welcome message for interactive shells
|
|
if [ -t 1 ]; then
|
|
helloworld
|
|
fi
|