feat!: add cli with update and list subcommands + gitignore param

This commit is contained in:
technofab 2026-01-05 21:39:41 +01:00
parent 56f281eea4
commit b39e16ec3c
Signed by: technofab
SSH key fingerprint: SHA256:bV4h88OqS/AxjbPn66uUdvK9JsgIW4tv3vwJQ8tpMqQ
6 changed files with 201 additions and 84 deletions

View file

@ -146,90 +146,100 @@ in {
Packages for updating the hooks without a devshell. (readonly)
'';
example = {
"soonix:update" = "<derivation>";
"soonix" = "<derivation>";
};
};
};
config = let
hooks = config.hooks;
hookNames = builtins.attrNames hooks;
# allow excluding gitignore since stuff like renovate can't use/commit it anyways
getHookNames = {includeGitignored ? true}:
if includeGitignored
then (builtins.attrNames hooks)
else
map
(hook: hook.name)
(builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks));
runHooks = concatMapStringsSep "\n" (hookName: let
hook = hooks.${hookName};
modes = {
link =
# sh
''
if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then
_soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}"
mkdir -p "$(dirname "${hook.output}")"
ln -sf "${hook.generatedDerivation}" "${hook.output}"
_changed=true
else
_soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}"
fi
'';
copy =
# sh
''
if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then
_soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}"
mkdir -p "$(dirname "${hook.output}")"
# required since they're read only
rm -f "${hook.output}"
cp "${hook.generatedDerivation}" "${hook.output}"
_changed=true
else
_soonix_log "info" "${hookName}" "File up to date: ${hook.output}"
fi
'';
};
runHooks = args:
concatMapStringsSep "\n" (hookName: let
hook = hooks.${hookName};
modes = {
link =
# sh
''
if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then
_soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}"
mkdir -p "$(dirname "${hook.output}")"
ln -sf "${hook.generatedDerivation}" "${hook.output}"
_changed=true
else
_soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}"
fi
'';
copy =
# sh
''
if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then
_soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}"
mkdir -p "$(dirname "${hook.output}")"
# required since they're read only
rm -f "${hook.output}"
cp "${hook.generatedDerivation}" "${hook.output}"
_changed=true
else
_soonix_log "info" "${hookName}" "File up to date: ${hook.output}"
fi
'';
};
optionalGitignore =
if hook.hook.gitignore
then ''
_soonix_add_to_gitignore "${hook.output}"
optionalGitignore =
if hook.hook.gitignore
then ''
_soonix_add_to_gitignore "${hook.output}"
''
else "";
in
builtins.addErrorContext "[soonix] while generating script for ${hookName}"
# sh
''
else "";
in
# sh
''
# Process hook: ${hookName}
while IFS= read -r line; do
case "$line" in
UPDATED) _soonix_updated+=("${hookName}") ;;
UPTODATE) _soonix_uptodate+=("${hookName}") ;;
*) echo "$line" ;;
esac
done < <(
set -euo pipefail
_changed=false
# Process hook: ${hookName}
while IFS= read -r line; do
case "$line" in
UPDATED) _soonix_updated+=("${hookName}") ;;
UPTODATE) _soonix_uptodate+=("${hookName}") ;;
*) echo "$line" ;;
esac
done < <(
set -euo pipefail
_changed=false
${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")}
${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")}
# Add to gitignore if requested
${optionalGitignore}
# Add to gitignore if requested
${optionalGitignore}
# Run extra commands if file changed
if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then
_soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}"
eval "${hook.hook.extra}"
fi
# Run extra commands if file changed
if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then
_soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}"
eval "${hook.hook.extra}"
fi
if [[ "$_changed" == "true" ]]; then
echo "UPDATED"
else
echo "UPTODATE"
fi
) || {
_soonix_log "error" "${hookName}" "Failed to process hook"
_soonix_failed+=("${hookName}")
}
'')
hookNames;
if [[ "$_changed" == "true" ]]; then
echo "UPDATED"
else
echo "UPTODATE"
fi
) || {
_soonix_log "error" "${hookName}" "Failed to process hook"
_soonix_failed+=("${hookName}")
}
'')
(getHookNames args);
generatedShellHook =
generateShellHook = args:
builtins.addErrorContext "[soonix] while generating shell hook"
# sh
''
_soonix_log() {
@ -265,7 +275,7 @@ in {
_soonix_failed=()
_soonix_uptodate=()
${runHooks}
${runHooks args}
local output=$'\E[msoonix:\E[38;5;8m'
local status=0
@ -297,8 +307,8 @@ in {
in rec {
# nothing to do if no hooks exist
shellHook =
if (builtins.length hookNames > 0)
then generatedShellHook
if (builtins.length (builtins.attrNames config.hooks) > 0)
then generateShellHook {}
else "";
shellHookFile = pkgs.writeShellScript "shellHook" shellHook;
devshellModule = {
@ -307,11 +317,99 @@ in {
};
finalFiles = buildAllFiles allFiles;
# make it simpler to update the hooks without any devshell
packages."soonix:update" = pkgs.writeShellScriptBin "soonix:update" ''
function _soonix() {
${shellHook}
}
_soonix
'';
packages = {
soonix =
pkgs.writeShellScriptBin "soonix"
# sh
''
set -euo pipefail
SKIP_GITIGNORE=false
COMMAND=""
show_help() {
cat << EOF
soonix - Declarative file management tool
USAGE:
soonix <COMMAND> [OPTIONS]
COMMANDS:
update Update all managed files
list List all managed file targets
help Show this help message
OPTIONS:
--skip-gitignore Skip files that would be added to .gitignore
EXAMPLES:
soonix update # Update all files
soonix update --skip-gitignore # Update only non-gitignored files
soonix list # List all targets
soonix list --skip-gitignore # List only non-gitignored targets
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
update|list|help)
COMMAND="$1"
shift
;;
--skip-gitignore)
SKIP_GITIGNORE=true
shift
;;
*)
echo "Error: Unknown argument '$1'" >&2
echo ""
show_help
exit 1
;;
esac
done
if [[ -z "$COMMAND" ]]; then
echo "Error: No command specified" >&2
echo ""
show_help
exit 1
fi
case "$COMMAND" in
help)
show_help
;;
list)
${concatMapStringsSep "\n" (hookName: let
hook = hooks.${hookName};
in ''
if [[ "$SKIP_GITIGNORE" == "true" && "${
if hook.hook.gitignore
then "true"
else "false"
}" == "true" ]]; then
: # skip
else
echo "${hook.output}"
fi
'') (builtins.attrNames hooks)}
;;
update)
if [[ "$SKIP_GITIGNORE" == "true" ]]; then
function _soonix() {
${generateShellHook {includeGitignored = false;}}
}
_soonix
else
function _soonix() {
${generateShellHook {}}
}
_soonix
fi
;;
esac
'';
};
};
}