From fc4e45ec3731aac51aa4beb07f9d46a24196b27e Mon Sep 17 00:00:00 2001 From: jutty Date: Sun, 7 Jul 2024 21:03:06 -0300 Subject: [PATCH] Add application behavior configuration parsing --- docs/usage/configuration.md | 47 +++++++++++++++++++++++ src/configuration.sh | 51 ++++++++++++++----------- src/system.sh | 4 +- src/tori | 74 ++++++++++++++++++++++--------------- 4 files changed, 123 insertions(+), 53 deletions(-) create mode 100644 docs/usage/configuration.md diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md new file mode 100644 index 0000000..6ef2e23 --- /dev/null +++ b/docs/usage/configuration.md @@ -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. diff --git a/src/configuration.sh b/src/configuration.sh index f28132a..0bab213 100644 --- a/src/configuration.sh +++ b/src/configuration.sh @@ -1,34 +1,41 @@ # configuration processing functions scan_directory() { - local target="$1" - local files= - local escaped_config_root="$(echo $CONFIG_ROOT | sed 's/\//\\\//g')" + local target="$1" + local files= + local escaped_config_root - 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 + escaped_config_root="$(echo "$CONFIG_ROOT" | sed 's/\//\\\//g')" - 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() { - system_packages="$(get_system_packages)" - user_packages="$(get_user_packages)" + if ! [ -f "$CONFIG_ROOT/packages" ]; then + log debug "No packages file found in $CONFIG_ROOT" + return 1 + fi - if [ "$system_packages" = "$user_packages" ]; then - log debug "packages match" - else - log debug "packages mismatch" - log debug "system: $system_packages" - log debug "user: $user_packages" + system_packages="$(get_system_packages)" + user_packages="$(get_user_packages)" - log user "System and configuration packages differ" - resolve_packages - fi + if [ "$system_packages" = "$user_packages" ]; then + log debug "packages match" + 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 } diff --git a/src/system.sh b/src/system.sh index 4dd8529..06a8c1a 100644 --- a/src/system.sh +++ b/src/system.sh @@ -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" diff --git a/src/tori b/src/tori index 078ee42..63c6dc4 100755 --- a/src/tori +++ b/src/tori @@ -1,40 +1,56 @@ #! /usr/bin/env sh -# paths -VERSION="0.0.4 2024-06-30" -TORI_ROOT="$HOME/tori" -CONFIG_ROOT="$HOME/.config/tori" -TMP_DIR="/tmp/tori" +main() { + # paths + VERSION="0.0.4 2024-06-30" + TORI_ROOT="$HOME/.local/share/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 -argument="$1" -parameter="$2" + # state -set_opts - + DEBUG=$DEBUG -## global constants -OS="$(get_operating_system)" + ## user input + argument="$1" + parameter="$2" -## global state -base_files= -bkp_files= -user_packages= -system_packages= + set_opts - -# entry point + ## global constants + OS="$(get_operating_system)" -prepare_directories + ## global state + base_files= + bkp_files= + user_packages= + system_packages= -if [ "$argument" = check ]; then - check -elif [ "$argument" = version ] || [ "$argument" = -v ] || [ "$argument" = --version ]; then - echo "$VERSION" -elif [ "$argument" = help ] || [ "$argument" = -h ] || [ "$argument" = --help ]; then - print_help -else - echo "Use 'tori help' for usage instructions" -fi + # entry point + + prepare_directories + + if [ "$argument" = check ]; then + check + elif [ "$argument" = version ] || [ "$argument" = -v ] || [ "$argument" = --version ]; then + echo "$VERSION" + 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"