mirror of
https://gitlab.com/TECHNOFAB/soonix.git
synced 2026-02-02 07:15:06 +01:00
feat(cli): add check mode which exits 2 if hooks/files are outdated
This commit is contained in:
parent
374e31d852
commit
5caa66ec8d
2 changed files with 82 additions and 64 deletions
|
|
@ -154,16 +154,7 @@ in {
|
||||||
config = let
|
config = let
|
||||||
hooks = config.hooks;
|
hooks = config.hooks;
|
||||||
# allow excluding gitignore since stuff like renovate can't use/commit it anyways
|
# allow excluding gitignore since stuff like renovate can't use/commit it anyways
|
||||||
getHookNames = {includeGitignored ? true}:
|
runHooks = concatMapStringsSep "\n" (hookName: let
|
||||||
if includeGitignored
|
|
||||||
then (builtins.attrNames hooks)
|
|
||||||
else
|
|
||||||
map
|
|
||||||
(hook: hook.name)
|
|
||||||
(builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks));
|
|
||||||
|
|
||||||
runHooks = args:
|
|
||||||
concatMapStringsSep "\n" (hookName: let
|
|
||||||
hook = hooks.${hookName};
|
hook = hooks.${hookName};
|
||||||
modes = {
|
modes = {
|
||||||
link =
|
link =
|
||||||
|
|
@ -171,8 +162,12 @@ in {
|
||||||
''
|
''
|
||||||
if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then
|
if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then
|
||||||
_soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}"
|
_soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}"
|
||||||
|
if [[ "$CHECK_MODE" != "true" ]]; then
|
||||||
mkdir -p "$(dirname "${hook.output}")"
|
mkdir -p "$(dirname "${hook.output}")"
|
||||||
ln -sf "${hook.generatedDerivation}" "${hook.output}"
|
ln -sf "${hook.generatedDerivation}" "${hook.output}"
|
||||||
|
else
|
||||||
|
_soonix_log "info" "${hookName}" "Would create symlink: ${hook.output} -> ${hook.generatedDerivation}"
|
||||||
|
fi
|
||||||
_changed=true
|
_changed=true
|
||||||
else
|
else
|
||||||
_soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}"
|
_soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}"
|
||||||
|
|
@ -183,10 +178,14 @@ in {
|
||||||
''
|
''
|
||||||
if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then
|
if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then
|
||||||
_soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}"
|
_soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}"
|
||||||
|
if [[ "$CHECK_MODE" != "true" ]]; then
|
||||||
mkdir -p "$(dirname "${hook.output}")"
|
mkdir -p "$(dirname "${hook.output}")"
|
||||||
# required since they're read only
|
# required since they're read only
|
||||||
rm -f "${hook.output}"
|
rm -f "${hook.output}"
|
||||||
cp "${hook.generatedDerivation}" "${hook.output}"
|
cp "${hook.generatedDerivation}" "${hook.output}"
|
||||||
|
else
|
||||||
|
_soonix_log "info" "${hookName}" "Would copy file: ${hook.generatedDerivation} -> ${hook.output}"
|
||||||
|
fi
|
||||||
_changed=true
|
_changed=true
|
||||||
else
|
else
|
||||||
_soonix_log "info" "${hookName}" "File up to date: ${hook.output}"
|
_soonix_log "info" "${hookName}" "File up to date: ${hook.output}"
|
||||||
|
|
@ -200,11 +199,20 @@ in {
|
||||||
_soonix_add_to_gitignore "${hook.output}"
|
_soonix_add_to_gitignore "${hook.output}"
|
||||||
''
|
''
|
||||||
else "";
|
else "";
|
||||||
|
|
||||||
|
isGitignored =
|
||||||
|
if hook.hook.gitignore
|
||||||
|
then "true"
|
||||||
|
else "false";
|
||||||
in
|
in
|
||||||
builtins.addErrorContext "[soonix] while generating script for ${hookName}"
|
builtins.addErrorContext "[soonix] while generating script for ${hookName}"
|
||||||
# sh
|
# sh
|
||||||
''
|
''
|
||||||
# Process hook: ${hookName}
|
# Process hook: ${hookName}
|
||||||
|
# Skip if SKIP_GITIGNORE is set and this hook is gitignored
|
||||||
|
if [[ "$SKIP_GITIGNORE" == "true" && "${isGitignored}" == "true" ]]; then
|
||||||
|
: # skip
|
||||||
|
else
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
case "$line" in
|
case "$line" in
|
||||||
UPDATED) _soonix_updated+=("${hookName}") ;;
|
UPDATED) _soonix_updated+=("${hookName}") ;;
|
||||||
|
|
@ -217,6 +225,7 @@ in {
|
||||||
|
|
||||||
${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")}
|
${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")}
|
||||||
|
|
||||||
|
if [[ "$CHECK_MODE" != "true" ]]; then
|
||||||
# Add to gitignore if requested
|
# Add to gitignore if requested
|
||||||
${optionalGitignore}
|
${optionalGitignore}
|
||||||
|
|
||||||
|
|
@ -225,6 +234,7 @@ in {
|
||||||
_soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}"
|
_soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}"
|
||||||
eval "${hook.hook.extra}"
|
eval "${hook.hook.extra}"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ "$_changed" == "true" ]]; then
|
if [[ "$_changed" == "true" ]]; then
|
||||||
echo "UPDATED"
|
echo "UPDATED"
|
||||||
|
|
@ -235,10 +245,11 @@ in {
|
||||||
_soonix_log "error" "${hookName}" "Failed to process hook"
|
_soonix_log "error" "${hookName}" "Failed to process hook"
|
||||||
_soonix_failed+=("${hookName}")
|
_soonix_failed+=("${hookName}")
|
||||||
}
|
}
|
||||||
|
fi
|
||||||
'')
|
'')
|
||||||
(getHookNames args);
|
(builtins.attrNames hooks);
|
||||||
|
|
||||||
generateShellHook = args:
|
generateShellHook =
|
||||||
builtins.addErrorContext "[soonix] while generating shell hook"
|
builtins.addErrorContext "[soonix] while generating shell hook"
|
||||||
# sh
|
# sh
|
||||||
''
|
''
|
||||||
|
|
@ -275,13 +286,16 @@ in {
|
||||||
_soonix_failed=()
|
_soonix_failed=()
|
||||||
_soonix_uptodate=()
|
_soonix_uptodate=()
|
||||||
|
|
||||||
${runHooks args}
|
${runHooks}
|
||||||
|
|
||||||
local output=$'\E[msoonix:\E[38;5;8m'
|
local output=$'\E[msoonix:\E[38;5;8m'
|
||||||
local status=0
|
local status=0
|
||||||
|
|
||||||
if [[ ''${#_soonix_updated[@]} -gt 0 ]]; then
|
if [[ ''${#_soonix_updated[@]} -gt 0 ]]; then
|
||||||
output="$output [updated: ''${_soonix_updated[*]}]" >&2
|
output="$output [updated: ''${_soonix_updated[*]}]" >&2
|
||||||
|
if [[ "$CHECK_MODE" == "true" ]]; then
|
||||||
|
status=2
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ ''${#_soonix_uptodate[@]} -gt 0 ]]; then
|
if [[ ''${#_soonix_uptodate[@]} -gt 0 ]]; then
|
||||||
output="$output [unchanged: ''${_soonix_uptodate[*]}]" >&2
|
output="$output [unchanged: ''${_soonix_uptodate[*]}]" >&2
|
||||||
|
|
@ -293,8 +307,8 @@ in {
|
||||||
|
|
||||||
printf "%s\E[m\n" "$output" >&2
|
printf "%s\E[m\n" "$output" >&2
|
||||||
|
|
||||||
if [[ $status -eq 1 ]]; then
|
if [[ $status -ne 0 ]]; then
|
||||||
exit 1
|
exit $status
|
||||||
fi
|
fi
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
|
@ -308,7 +322,7 @@ in {
|
||||||
# nothing to do if no hooks exist
|
# nothing to do if no hooks exist
|
||||||
shellHook =
|
shellHook =
|
||||||
if (builtins.length (builtins.attrNames config.hooks) > 0)
|
if (builtins.length (builtins.attrNames config.hooks) > 0)
|
||||||
then generateShellHook {}
|
then generateShellHook
|
||||||
else "";
|
else "";
|
||||||
shellHookFile = pkgs.writeShellScript "shellHook" shellHook;
|
shellHookFile = pkgs.writeShellScript "shellHook" shellHook;
|
||||||
devshellModule = {
|
devshellModule = {
|
||||||
|
|
@ -325,6 +339,7 @@ in {
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
SKIP_GITIGNORE=false
|
SKIP_GITIGNORE=false
|
||||||
|
CHECK_MODE=false
|
||||||
COMMAND=""
|
COMMAND=""
|
||||||
|
|
||||||
show_help() {
|
show_help() {
|
||||||
|
|
@ -336,6 +351,7 @@ in {
|
||||||
|
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
update Update all managed files
|
update Update all managed files
|
||||||
|
check Check if all files are up to date (dry-run)
|
||||||
list List all managed file targets
|
list List all managed file targets
|
||||||
help Show this help message
|
help Show this help message
|
||||||
|
|
||||||
|
|
@ -352,7 +368,7 @@ in {
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
update|list|help)
|
update|check|list|help)
|
||||||
COMMAND="$1"
|
COMMAND="$1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
|
@ -396,17 +412,18 @@ in {
|
||||||
'') (builtins.attrNames hooks)}
|
'') (builtins.attrNames hooks)}
|
||||||
;;
|
;;
|
||||||
update)
|
update)
|
||||||
if [[ "$SKIP_GITIGNORE" == "true" ]]; then
|
|
||||||
function _soonix() {
|
function _soonix() {
|
||||||
${generateShellHook {includeGitignored = false;}}
|
${generateShellHook}
|
||||||
}
|
}
|
||||||
_soonix
|
_soonix
|
||||||
else
|
;;
|
||||||
|
check)
|
||||||
|
CHECK_MODE=true
|
||||||
|
echo "Checking files..."
|
||||||
function _soonix() {
|
function _soonix() {
|
||||||
${generateShellHook {}}
|
${generateShellHook}
|
||||||
}
|
}
|
||||||
_soonix
|
_soonix
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
'';
|
'';
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,7 @@ in {
|
||||||
assert_file_contains "${soonixBin}" "test.json"
|
assert_file_contains "${soonixBin}" "test.json"
|
||||||
|
|
||||||
assert_file_contains "${soonixBin}" "SKIP_GITIGNORE"
|
assert_file_contains "${soonixBin}" "SKIP_GITIGNORE"
|
||||||
|
assert_file_contains "${soonixBin}" "CHECK_MODE"
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue