#!/bin/bash # # Library: base.lib # Description: Base Developers Library # Modified: 2024/11/30 15:27:00 # Derechos de Autor (C) [2024] [Mauro Rosero P. ] # # Este programa es software libre: usted puede redistribuirlo y/o modificarlo # bajo los términos de la Licencia Pública Affero General de GNU tal como # lo publica la Free Software Foundation, ya sea la versión 3 de la licencia, # o (a su elección) cualquier versión posterior. # # Este programa se distribuye con la esperanza de que sea útil, # pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de # COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la # Licencia Pública Affero General de GNU para obtener más detalles. # # Debería haber recibido una copia de la Licencia Pública Affero General # junto con este programa. Si no la recibió, consulte . DEVSPATH=${DEVSPATH:=devs} CONFPATH=${CONFPATH:=config} BIN_BASE=${BIN_BASE:=bin} if [ -z "${BIN_HOME}" ]; then BIN_HOME=${HOME}/${DEVSPATH} fi BIN_CONF=${BIN_CONF:=${BIN_BASE}/config} BIN_SOPS=${BIN_SOPS:=${BIN_HOME}/sops} BIN_MESG=${BIN_MESG:=${BIN_BASE}/msg} BIN_LIBS=${BIN_LIBS:=${BIN_BASE}/lib} DEVS_NAME=${CONFPATH}/project.head GIT_IGNORE=.gitignore DATENOW="$(date +"%Y-%m-%d %H:%M:%S")" DATEBAK="$(date +"%Y%m%d%H%M%S")" FREEKV_URL=$(cat < "${BIN_HOME}/${BIN_BASE}/${CONFPATH}/freekeyval.dat") FREEKV_SOPS_FILE=freekv-sops.devs.yaml DOCKER_LOCAL=dockerfile.local DOCKER_BASE=dockerfile.base DOCKER_SAAS=dockerfile.saas DOCKER_BUILD=build DOCKER_FILE=Dockerfile DOCKER_ENTRY=entrypoint.sh VERSION="$(cat < ${BIN_HOME}/${BIN_CONF}/version)" # Test library function baselib_test() { echo "Base Library loaded!" exit 1 } # Load messages function load_messages() { local BIN_PATH=$1 local MSG_PATH=$2 local LANGUAGE=$3 local MSG_FILE=$4 if [ -f $BIN_PATH/$MSG_PATH/$MSG_FILE.$LANGUAGE ] then source $BIN_PATH/$MSG_PATH/$MSG_FILE.$LANGUAGE else source $BIN_PATH/$MSG_PATH/$MSG_FILE.es fi } # Display developers tools header function display_devstools_header() { local tittle=$1 clear echo "$head_000 $head_002 $tittle" echo "======================================================================" } # Display text header for tui dialog function display_text_header() { local head_text=$1 local subhead_text=$2 clear if [ ! -z "${head_text}" ]; then echo "${head_text}" else echo "$head_000 $head_002" fi if [ ! -z "${subhead_text}" ]; then echo "${subhead_text}" fi echo "======================================================================" } # Display text header apps function display_project_header() { local PROJECT=$1 local tittle=$2 clear echo "$(cat < $PROJECT/config/project.head) $tittle" echo "======================================================================" } # Verify if your program or command is installed function command_installed() { local PROGRAM=$1 if command -v $PROGRAM &> /dev/null; then return 0 fi # No program or command is installed return 1 } # Install os packages function os_pkgs_install() { local PACKAGE=$1 echo "${pkg_install_begin} ${PACKAGE}" if [ "$(uname)" == "Darwin" ]; then # En macOS, a través de Homebrew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install $PACKAGE elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then # En sistemas Debian y derivados, a través de apt apt update apt install -y $PACKAGE elif [ -f /etc/redhat-release ]; then # En sistemas Red Hat y derivados, a través de dnf dnf install -y $PACKAGE elif [ -f /etc/arch-release ]; then # En Arch Linux, a través de pacman pacman -Sy --noconfirm $PACKAGE elif [ -f /etc/rc.conf ]; then # En BSD, a través de pkg pkg install -y $PACKAGE else echo "${os_nofound}" exit 1 fi echo "${pkg_install_success} ${PACKAGE}" } # Update or upgrade OS Packages function os_update() { if [ "$(uname)" == "Darwin" ]; then echo "$os_update (BREW)" brew upgrade elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then echo "$os_update (APT)" apt update && apt upgrade -y elif [ -f /etc/redhat-release ]; then echo "$os_update (DNF)" dnf update -y elif [ -f /etc/arch-release ]; then echo "$os_update (PACMAN)" pacman -Syu elif [ -f /etc/rc.conf ]; then echo "$os_update (PKG)" pkg update && pkg upgrade else echo "${os_nofound}" exit 1 fi } # Check for container manager installed function container_mode() { command_installed "podman" if [ $? -eq 0 ] then return 0 else command_installed "docker" if [ $? -eq 0 ] then return 1 fi fi # Exit with code 255 if not docker or podman installed return 255 } # Read a file to convert to array # Use global var ARRAY, set to empty before to call this function function read_file_to_array() { local afile="$1" if [[ ! -f "${afile}" ]]; then return 1 fi while IFS= read -r line; do ARRAY+=("${line}") done < "${afile}" return 0 } # Git init repository function git_init() { local REPO_PATH=$1 local BRANCH=$2 git init "$REPO_PATH" -b $BRANCH return $? } # Git add tracked files to repository function git_add_full() { local REPO_PATH=$1 cd $REPO_PATH if [ $? -eq 0 ]; then git add . return $? else return 255 fi } # Git add tracked files to repository function git_commit() { local REPO_PATH=$1 local GIT_MESSAGES=$2 cd $REPO_PATH if [ $? -eq 0 ]; then git commit -m "$GIT_MESSAGES" return $? else return 255 fi } # Git new remote project repository function git_new_project() { local REPO_PATH=$1 local REMOTE_PATH=$2 local GIT_PROJECT=$3 local BRANCH=$4 local REPO_REMOTE=$(printf "$REMOTE_PATH" "${GIT_PROJECT}") echo "$REPO_REMOTE" if [ -d $REPO_PATH ] then cd $REPO_PATH if [ $? -eq 0 ]; then git remote add origin $REPO_REMOTE if [ $? -eq 0 ]; then git checkout $BRANCH if [ $? -eq 0 ]; then git push --set-upstream origin $BRANCH return $? else return 254 fi fi fi fi return 255 } # Git clone repository function git_clone_pull() { local REPO_PATH=$1 local REPO_REMOTE=$2 local APPS=$3 local BRANCH=$4 if [ ! -d $REPO_PATH/$APPS ] then cd $REPO_PATH if [ $? -eq 0 ]; then git clone $REPO_REMOTE -b $BRANCH $APPS return $? else return 255 fi else cd $REPO_PATH/$APPS if [ $? -eq 0 ]; then git pull return $? else return 255 fi fi } # Check for valid os system function get_osname() { if [ "$(uname)" == "Darwin" ]; then # En macOS, instalamos o actualizamos a través de Homebrew os_name=$(sw_vers -productVersion | awk -F '.' '{print "macOS " $1 "." $2}') elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then # En sistemas Debian y derivados, instalamos o actualizamos a través de apt os_name=$(grep "^ID_LIKE=" /etc/os-release | cut -d= -f2) if grep -qi "${head_ubuntu}" /etc/os-release then os_name="${head_ubuntu}" fi elif [ -f /etc/redhat-release ]; then # En sistemas Red Hat, instalamos o actualizamos a través de dnf os_name=$(awk '{print $1}' /etc/redhat-release) elif [ -f /etc/arch-release ]; then # En Arch Linux, instalamos o actualizamos a través de pacman os_name=$(grep '^NAME=' /etc/os-release | awk -F '"' '{print $2}') elif [ -f /etc/rc.conf ]; then # En BSD, instalamos o actualizamos a través de pkg os_name=$(awk '{print $2}' /etc/version | awk -F '-' '{print $1}') else os_name="${head_unknow}" fi } # Build container with docker function docker_build() { local CONTAINER=$1 local BASE_IMAGE=$2 docker build ./build -t ${CONTAINER} --build-arg BASE_IMAGE=${BASE_IMAGE} return $? } # Build container with podman function podman_build() { local CONTAINER=$1 local BASE_IMAGE=$2 local registry_path=~/.config/containers if [ ! -d ${registry_path} ] then mkdir ${registry_path} fi echo 'unqualified-search-registries = ["docker.io"]' > "${registry_path}/registries.conf" podman build ./build -t ${CONTAINER} --format docker --build-arg BASE_IMAGE=${BASE_IMAGE} return $? } # Build container with podman or docker function build_container() { local CONTAINER=$1 local BASE_IMAGE=$2 # Verificar si Docker está instalado if command -v docker &> /dev/null then docker_build "$CONTAINER" "$BASE_IMAGE" return $? fi # Verificar si Podman está instalado if command -v podman &> /dev/null then podman_build "$CONTAINER" "$BASE_IMAGE" return $? fi return 1 } # Get sops token for free remote key/value server function sops_freekv_token() { local rc=0 local sops_file="${BIN_SOPS}/${FREEKV_SOPS_FILE}" if [ ! -f "${sops_file}" ]; then return 1 fi token=$(sops -d ${sops_file} | yq .freekv_devs_token) rc=$? if [ $rc -ne 0 ]; then return $rc fi if [ -n "${token}" ]; then return 2 fi echo "${token}" return 0 } # Alias claude code to cortana cortana_alias() { local alias_nombre="cortana" local alias_comando="claude" local shell_config="" local title="${head_000} ${head_002}" # Detectar el shell actual y seleccionar el archivo de configuración adecuado case "$SHELL" in */bash) shell_config="$HOME/.bashrc" ;; */zsh) shell_config="$HOME/.zshrc" ;; *) dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_040}" 7 50 return 1 ;; esac # Verificar si el alias ya existe en el archivo de configuración if grep -q "alias $alias_nombre=" "$shell_config"; then dialog --backtitle "${title}" --title "${npm_034}" --msgbox "${npm_035} $shell_config" 7 60 return 2 else # Agregar el alias al final del archivo de configuración echo "alias $alias_nombre='$alias_comando'" >> "$shell_config" if [[ $? -eq 0 ]]; then # Recargar el archivo de configuración source "$shell_config" dialog --backtitle "${title}" --title "${npm_036}" --msgbox "${npm_037} $shell_config\n\n${npm_038}" 9 60 else dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_039}" 7 50 return 1 fi fi }