feat!: refactor base, config, dfm and scripts

This commit is contained in:
2024-07-23 03:43:12 +03:00
parent adecceda7a
commit 1f2ca90ca5
36 changed files with 1543 additions and 897 deletions

View File

@@ -14,19 +14,92 @@
SCRIPT=$(basename "$0")
if ! declare -f msg > /dev/null; then
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[ "$VERBOSE" -eq 1 ] && echo "$1"
return 0
}
fi
if ! declare -f msg_err > /dev/null; then
# Function to print error messages and exit
# $1 - error message (string)
msg_err()
{
echo "(!) ERROR: $1" >&2
exit 1
}
fi
if ! declare -f msg_done > /dev/null; then
# Function to print done message
# $1 - message (string)
msg_done()
{
echo "✓ $1"
return 0
}
fi
VERSION_NVM="v0.39.5"
export DOTFILES="$HOME/.dotfiles"
# shellcheck source=./../../scripts/shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
source "$DOTFILES/config/exports"
source "$DOTFILES/config/alias"
source "$DOTFILES/config/functions"
# Loads configs for better installation experience
x-load-configs
source "$DOTFILES/config/shared.sh"
function section_install
source "${DOTFILES}/local/bin/msgr"
# -- Menu builder -- #
menu_section()
{
LINE=$(printf '%-18s [ %-15s ]\n' "$1" "$2")
echo -e " $(__log_marker) $LINE"
}
menu_item()
{
LINE=$(printf '%-15s %-30s\n' "$1" "$2")
echo -e "$(__log_indent)$(__log_marker) $LINE"
}
# Takes a bash array ("cow:moo", "dinosaur:roar") and loops
# through the keys to build menu section listing.
menu_usage_header()
{
MENU_CMD="$1"
shift
MENU_ARRAY=("$@")
KEYS=""
for item in "${MENU_ARRAY[@]}"; do
CMD=$(echo "${item}" | awk -F ":" '{print $1}')
KEYS+="${CMD} | "
done
# "???" removes 3 last characters, being " | " from the end
menu_section "$MENU_CMD" "${KEYS%???}"
}
# Takes the usage command "$0 dotfiles" and a
# bash array ("cow:moo" "dinosaur:roar") and loops
# through in building a menu for dfm command usage listing.
menu_usage()
{
MENU_CMD="$1"
shift
MENU_ARRAY=("$@")
msg "$MENU_CMD"
for item in "${MENU_ARRAY[@]}"; do
CMD=$(echo "${item}" | awk -F ":" '{print $1}')
DESC=$(echo "${item}" | awk -F ":" '{print $2}')
menu_item "$CMD" "$DESC"
done
}
section_install()
{
USAGE_PREFIX="$SCRIPT install <command>"
@@ -56,7 +129,6 @@ function section_install
case "$1" in
all)
msgr msg "Starting to install all and reloading configurations..."
x-load-configs
$0 install macos
$0 install fonts
$0 install antigen
@@ -79,7 +151,7 @@ function section_install
$0 install ntfy
$0 install z
msgr msg "Reloading configurations again..."
x-load-configs
source "$DOTFILES/config/shared.sh"
msgr yay "All done!"
;;
antigen)
@@ -210,7 +282,7 @@ function section_install
esac
}
function section_brew
section_brew()
{
USAGE_PREFIX="$SCRIPT brew <command>"
@@ -258,7 +330,7 @@ function section_brew
! x-have brew && menu_section "$USAGE_PREFIX" "brew not available on this system"
}
function section_helpers
section_helpers()
{
USAGE_PREFIX="$SCRIPT helpers <command>"
MENU=(
@@ -274,7 +346,7 @@ function section_helpers
esac
}
function section_docs
section_docs()
{
USAGE_PREFIX="$SCRIPT docs <command>"
@@ -288,7 +360,7 @@ function section_docs
esac
}
function section_dotfiles
section_dotfiles()
{
USAGE_PREFIX="$SCRIPT dotfiles <command>"
@@ -344,7 +416,7 @@ function section_dotfiles
esac
}
function section_check
section_check()
{
USAGE_PREFIX="$SCRIPT check <command>"
X_HOSTNAME=$(hostname)
@@ -369,7 +441,7 @@ function section_check
}
# Secret menu for visual tests
function section_tests
section_tests()
{
USAGE_PREFIX="$SCRIPT tests <command>"
@@ -398,7 +470,7 @@ function section_tests
esac
}
function usage
usage()
{
echo ""
msg_prompt "Usage: $SCRIPT <section> <command>"

View File

@@ -2,76 +2,97 @@
# Load our configuration files
# Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved.
DOTFILES="${DOTFILES:-$HOME/.dotfiles}"
source "$DOTFILES/config/shared.sh"
# Enable verbosity with VERBOSE=1 x-load-configs
VERBOSE="${VERBOSE:=0}"
VERBOSE="${VERBOSE:-0}"
# Enable debugging with DEBUG=1 x-load-configs
DEBUG="${DEBUG:=0}"
DEBUG="${DEBUG:-0}"
[ "$DEBUG" = "1" ] && {
set -x
# Get the hostname
CONFIG_HOST="$(hostname -s)"
# Enable debugging if requested
[ "$DEBUG" = "1" ] && set -x
CONFIG_PATH="${DOTFILES}/config"
[ -d "$DOTFILES" ] || {
msg_err "Error: DOTFILES is not set or $DOTFILES does not exist"
}
CONFIG_PATH="$DOTFILES/config"
# Function to print messages if VERBOSE is enabled
# $1 - message type (string)
# $2 - message content (string)
config_msg()
{
# if $1 is empty, return
[ -z "$1" ] && return
[ -z "$2" ] && $2=""
# Load the shell dotfiles, and then some:
HOST="$(hostname -s)"
[ "$VERBOSE" = "1" ] && {
echo "x-load-configs: VERBOSE=1"
echo "x-load-configs: HOST: $HOST"
local msg_type="$1"
local msg_content="$2"
[[ "$VERBOSE" -eq 1 ]] && printf 'x-load-configs: %s %s\n' "$msg_type" "$msg_content"
return 0
}
configFile()
# Function to get the full path of a config file
# $1 - filename (string)
config_file_path()
{
echo "$CONFIG_PATH/$1"
}
configMsg()
# Function to source configuration files
source_config()
{
printf 'x-load-configs: %s %s\n' "$1" "$2"
local config_file=$1
if [ -f "$config_file" ]; then
eval "$config_file"
config_msg "Sourced" "$config_file"
else
msg "Config file $config_file not found"
fi
return 0
}
loadConfigFiles()
# Function to load a configuration file
# $1 - base config file name (string)
load_config_files()
{
CONFIG_FILE=$1
SECRET_FILE=$CONFIG_FILE-secret
HOST_FILE=$CONFIG_FILE-$HOST
SECRET_HOST=$HOST_FILE-secret
local config_file="$1"
local secret_file="${config_file}-secret"
local host_file="${config_file}-${CONFIG_HOST}"
local secret_host_file="${host_file}-secret"
[ "$VERBOSE" = "1" ] && configMsg "Looking for" "$CONFIG_FILE"
# global (exports|alias|functions) FILENAME for all hosts
# shellcheck source=../config/exports
[ -r "$CONFIG_FILE" ] && {
source "$CONFIG_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$CONFIG_FILE"
config_msg "Looking for" "$config_file"
[ -r "$config_file" ] && {
source_config "$config_file"
}
# global secret FILENAME, git ignored
# shellcheck source=../config/exports-secret
[ "$VERBOSE" = "1" ] && configMsg "Looking for" "$SECRET_FILE"
[ -r "$SECRET_FILE" ] && {
source "$SECRET_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_FILE"
config_msg "Looking for" "$secret_file"
[ -r "$secret_file" ] && {
source_config "$secret_file"
}
# host specific (exports|alias|functions) FILENAME
# shellcheck source=../config/exports
[ "$VERBOSE" = "1" ] && configMsg "Looking for" "$HOST_FILE"
[ -r "$HOST_FILE" ] && {
source "$HOST_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$HOST_FILE"
config_msg "Looking for" "$host_file"
[ -r "$host_file" ] && {
source_config "$host_file"
}
# host specific (exports|alias|functions) FILENAME, git ignored
# shellcheck source=../config/exports
[ "$VERBOSE" = "1" ] && configMsg "Looking for" "$SECRET_HOST"
[ -r "$SECRET_HOST" ] && {
source "$SECRET_HOST" \
&& [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_HOST"
config_msg "Looking for" "$secret_host_file"
[ -r "$secret_host_file" ] && {
source_config "$secret_host_file"
}
return 0
}
FILE_EXPORTS=$(configFile "exports")
FILE_FUNCTIONS=$(configFile "functions")
FILE_ALIAS=$(configFile "alias")
loadConfigFiles "$FILE_EXPORTS"
loadConfigFiles "$FILE_FUNCTIONS"
loadConfigFiles "$FILE_ALIAS"
config_msg "VERBOSE=1" "Verbose mode enabled"
config_msg "HOST" "$CONFIG_HOST"
load_config_files "$(config_file_path "exports")"
load_config_files "$(config_file_path "functions")"
load_config_files "$(config_file_path "alias")"
exit 0

View File

@@ -3,5 +3,65 @@
# Add a directory to the beginning of the PATH if it's not already there.
# Usage: x-path-append <dir>
x-path-remove "$1"
export PATH="${PATH:+"$PATH:"}$1"
# Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
}
# Function to remove a directory from PATH
# $1 - directory to remove (string)
remove_from_path()
{
local dir=$1
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//')
msg "Directory $dir has been removed from PATH"
else
msg "Directory $dir is not in PATH"
fi
}
# Function to append a directory to PATH
# $1 - directory to append (string)
append_to_path()
{
local dir=$1
if [ ! -d "$dir" ]; then
msg "(?) Directory $dir does not exist"
exit 0
fi
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(!) Directory $dir is already in PATH"
else
export PATH="${PATH:+"$PATH:"}$dir"
msg "(!) Directory $dir has been added to the end of PATH"
fi
}
# Main function
main()
{
if [ "$#" -ne 1 ]; then
usage
fi
remove_from_path "$1"
append_to_path "$1"
}
main "$@"

View File

@@ -3,4 +3,50 @@
# Add a directory to the front of the PATH if it exists and is not already there
# Usage: x-path-prepend <dir>
export PATH="$1${PATH:+":$PATH"}"
# Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
}
# Function to add a directory to the front of PATH
# $1 - directory to add (string)
prepend_to_path()
{
local dir=$1
if [ ! -d "$dir" ]; then
msg "(?) Directory $dir does not exist"
exit 0
fi
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(!) Directory $dir is already in PATH"
else
export PATH="$dir${PATH:+":$PATH"}"
msg "(!) Directory $dir has been added to the front of PATH"
fi
}
# Main function
main()
{
if [ "$#" -ne 1 ]; then
usage
fi
prepend_to_path "$1"
}
main "$@"

View File

@@ -3,4 +3,46 @@
# Remove a directory from the PATH
# Usage: x-path-remove <dir>
export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//')
# Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
}
# Function to remove a directory from PATH
# $1 - directory to remove (string)
remove_from_path()
{
local dir=$1
if ! echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(?) Directory $dir is not in PATH"
exit 0
fi
export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//')
msg "(!) Directory $dir has been removed from PATH"
}
# Main function
main()
{
if [ "$#" -ne 1 ]; then
usage
fi
remove_from_path "$1"
}
main "$@"