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

@ -3,12 +3,14 @@
scan_directory() {
local target="$1"
local files=
local escaped_config_root="$(echo $CONFIG_ROOT | sed 's/\//\\\//g')"
local escaped_config_root
escaped_config_root="$(echo "$CONFIG_ROOT" | sed 's/\//\\\//g')"
if [ -d "$target" ]; then
scan="$(find "$target" -type f)"
for line in $scan; do
line="$(echo $line | sed "s/$escaped_config_root\///")"
line="$(echo "$line" | sed "s/$escaped_config_root\///")"
files="$line\n$files"
done
fi
@ -18,6 +20,11 @@ scan_directory() {
scan_packages() {
if ! [ -f "$CONFIG_ROOT/packages" ]; then
log debug "No packages file found in $CONFIG_ROOT"
return 1
fi
system_packages="$(get_system_packages)"
user_packages="$(get_user_packages)"

View file

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

View file

@ -1,15 +1,20 @@
#! /usr/bin/env sh
main() {
# paths
VERSION="0.0.4 2024-06-30"
TORI_ROOT="$HOME/tori"
TORI_ROOT="$HOME/.local/share/tori"
CONFIG_ROOT="$HOME/.config/tori"
TMP_DIR="/tmp/tori"
read_config_paths
. "$TORI_ROOT/src/index.sh"
# state
DEBUG=$DEBUG
## user input
argument="$1"
parameter="$2"
@ -38,3 +43,14 @@ elif [ "$argument" = help ] || [ "$argument" = -h ] || [ "$argument" = --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"