Add application behavior configuration parsing

This commit is contained in:
Juno Takano 2024-07-07 21:03:06 -03:00
parent 4db935a935
commit fc4e45ec37
4 changed files with 123 additions and 53 deletions

View file

@ -0,0 +1,47 @@
tori looks for configuration in `~/.config/tori`.
In this directory, you can create the following files and directories:
- `tori.conf`: configures options that will determine how tori itself behaves
- `packages`: a list of packages containing one package name per line
- blank lines and lines beginning with a `#` are ignored
- `base`: a directory containing any number of files you can tell tori to copy to specific locations
## Configuration options
The `tori.conf` file must use the following format:
```conf
tori_root = ~/.local/apps/my-tori-installation
```
The following configuration options can be used to specify how you want tori to behave:
- `tori_root`
Configuration options are case insensitive. The spaces around the `=` character are optional. Blank lines and lines beginning with a `#` are ignored.
You can use `~` and `$HOME` to represent your home directory. `$HOME` will be replaced in every occasion with your home directory, while `~` will only be replaced for the first occasion it appears in the configuration value.
Configuration values must _not_ contain the character `*`.
## Package lists
tori will read the `packages` file and check if it matches the currently installed packages. If it does not match, it will ask you how to proceed.
If you would prefer not to be asked how to proceed, you can configure a default action to be taken for package mismatch resolution.
## Base files
tori will go through the contents of the `base` directory and take different actions depending on how they are laid out.
If you have top-level directories inside `base` that match the directories starting from your system's root (for example, a `base/etc` directory), tori will recursively inspect this directory for files and compare them to the contents of the matching directories in the system.
If any of the files differ from their counterparts on the actual system, tori will ask you how to proceed. If you do not want to be asked every time, you can configure tori to take a specific action without asking.
If you decide to do so, bear in mind important files may get overwritten without warning. tori will create backups in the `bkp` directory every time a file is modified or overwritten.
If you would rather not replicate the system directory hierarchy in your `base`directory, you can also place the files inside `base` itself or in any other structure you desire.
In this case, you will have to manually specify if you want these files to be matched against any system-level files, and, if so, where those files are.

View file

@ -1,34 +1,41 @@
# configuration processing functions # configuration processing functions
scan_directory() { scan_directory() {
local target="$1" local target="$1"
local files= local files=
local escaped_config_root="$(echo $CONFIG_ROOT | sed 's/\//\\\//g')" local escaped_config_root
if [ -d "$target" ]; then escaped_config_root="$(echo "$CONFIG_ROOT" | sed 's/\//\\\//g')"
scan="$(find "$target" -type f)"
for line in $scan; do
line="$(echo $line | sed "s/$escaped_config_root\///")"
files="$line\n$files"
done
fi
echo "$files" if [ -d "$target" ]; then
scan="$(find "$target" -type f)"
for line in $scan; do
line="$(echo "$line" | sed "s/$escaped_config_root\///")"
files="$line\n$files"
done
fi
echo "$files"
} }
scan_packages() { scan_packages() {
system_packages="$(get_system_packages)" if ! [ -f "$CONFIG_ROOT/packages" ]; then
user_packages="$(get_user_packages)" log debug "No packages file found in $CONFIG_ROOT"
return 1
fi
if [ "$system_packages" = "$user_packages" ]; then system_packages="$(get_system_packages)"
log debug "packages match" user_packages="$(get_user_packages)"
else
log debug "packages mismatch"
log debug "system: $system_packages"
log debug "user: $user_packages"
log user "System and configuration packages differ" if [ "$system_packages" = "$user_packages" ]; then
resolve_packages log debug "packages match"
fi else
log debug "packages mismatch"
log debug "system: $system_packages"
log debug "user: $user_packages"
log user "System and configuration packages differ"
resolve_packages
fi
} }

View file

@ -6,8 +6,8 @@ get_operating_system() {
/etc/os-release | grep '^ID=' | sed 's/ID=//')" /etc/os-release | grep '^ID=' | sed 's/ID=//')"
log debug "uname OS: $uname_output" log debug "uname OS: $uname_output"
log debug "os-release OS name: $os_release_name" log debug "os-release name: $os_release_name"
log debug "os-release OS ID: $os_release_id" log debug "os-release ID: $os_release_id"
if [ "$os_release_name" = FreeBSD ]; then if [ "$os_release_name" = FreeBSD ]; then
echo "FreeBSD" echo "FreeBSD"

View file

@ -1,40 +1,56 @@
#! /usr/bin/env sh #! /usr/bin/env sh
# paths main() {
VERSION="0.0.4 2024-06-30" # paths
TORI_ROOT="$HOME/tori" VERSION="0.0.4 2024-06-30"
CONFIG_ROOT="$HOME/.config/tori" TORI_ROOT="$HOME/.local/share/tori"
TMP_DIR="/tmp/tori" CONFIG_ROOT="$HOME/.config/tori"
TMP_DIR="/tmp/tori"
. "$TORI_ROOT/src/index.sh" read_config_paths
# state . "$TORI_ROOT/src/index.sh"
## user input # state
argument="$1"
parameter="$2"
set_opts - DEBUG=$DEBUG
## global constants ## user input
OS="$(get_operating_system)" argument="$1"
parameter="$2"
## global state set_opts -
base_files=
bkp_files=
user_packages=
system_packages=
# entry point ## global constants
OS="$(get_operating_system)"
prepare_directories ## global state
base_files=
bkp_files=
user_packages=
system_packages=
if [ "$argument" = check ]; then # entry point
check
elif [ "$argument" = version ] || [ "$argument" = -v ] || [ "$argument" = --version ]; then prepare_directories
echo "$VERSION"
elif [ "$argument" = help ] || [ "$argument" = -h ] || [ "$argument" = --help ]; then if [ "$argument" = check ]; then
print_help check
else elif [ "$argument" = version ] || [ "$argument" = -v ] || [ "$argument" = --version ]; then
echo "Use 'tori help' for usage instructions" echo "$VERSION"
fi elif [ "$argument" = help ] || [ "$argument" = -h ] || [ "$argument" = --help ]; then
print_help
else
echo "Use 'tori help' for usage instructions"
fi
}
read_config_paths() {
TORI_ROOT="$(grep -i '^tori_root' "$CONFIG_ROOT/tori.conf" \
| cut -d '=' -f 2 | xargs \
| sed "s*~*$HOME*" | sed "s*\$HOME*$HOME*g" \
)"
[ -n "$DEBUG" ] && echo "TORI_ROOT: $TORI_ROOT"
}
main "$1" "$2"