Skip to content

Commit

Permalink
feat(scripts): add initool-overwrite.* scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dbohdan committed Aug 14, 2024
1 parent f3b9d8f commit 01e7301
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ If the filename is `-`, initool reads standard input. For the commands `get`, `s
For `exists`, it reports whether the section or the property exists through its exit status.

**Initool never modifies the input file.**
(One exception is if you redirect initool's output to the same file as input, which results in an empty file
[like with other programs](https://superuser.com/questions/597244/why-does-redirecting-the-output-of-a-file-to-itself-produce-a-blank-file).)
One exception is if you redirect initool's output to the same file as input, which results in an empty file
[like with other programs](https://superuser.com/questions/597244/why-does-redirecting-the-output-of-a-file-to-itself-produce-a-blank-file).
Two wrapper scripts are included if you want to modify the input file:
[`initool-overwrite.sh`](initool-overwrite.sh) (POSIX shell)
and [`initool-overwrite.cmd`](initool-overwrite.cmd) (Windows batch).
The scripts redirect the output of initool to a temporary file, then overwrite the original.

An INI file consists of properties (`key=value` lines) and sections (designated with a `[section name]` header line).
A property can be at the "top level" of the file (before any section headers) or in a section (after a section header).
Expand Down
65 changes: 65 additions & 0 deletions initool-overwrite.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@echo off
setlocal enabledelayedexpansion

for %%a in (%*) do (
if "%%a"=="-h" (
goto :help
)
if "%%a"=="--help" (
goto :help
)
)

if "%~2"=="" (
goto :error_usage
)

set "command=%~1"
set "file=%~2"
set status=0
set overwrite=1

if "%command%"=="e" (
set overwrite=0
)
if "%command%"=="exists" (
set overwrite=0
)

if "%file%"=="-" (
echo file must not be "-" 1>&2
exit /b 2
)

:create_temp
set "temp=%temp%\initool-in-place-%random%.tmp"
if exist "!temp!" goto :create_temp

if defined INITOOL (
"!INITOOL!" %* > "!temp!"
) else (
initool %* > "!temp!"
)

set status=%errorlevel%
if %status% equ 0 (
if %overwrite% equ 1 (
copy /y "!temp!" "%file%" > nul
)
)

:cleanup
del "%temp%"
exit /b %status%

:error_usage
echo usage: %~nx0 command file [arg ...] 1>&2
exit /b 2

:help
echo Modify the input file with initool.
echo.
echo usage: %~nx0 command file [arg ...]
echo.
echo You can give the path to initool in the environment variable "INITOOL".
exit /b
52 changes: 52 additions & 0 deletions initool-overwrite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/sh
set -eu

usage() {
echo "usage: $(basename "$0") command file [arg ...]"
}

help() {
printf 'Modify the input file with initool.\n\n'
usage
printf '\nYou can give the path to initool in the environment variable "INITOOL".\n'
}

for arg in "$@"; do
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
help
exit 0
fi
done

if [ $# -lt 2 ]; then
usage >/dev/stderr
exit 2
fi

command=$1
file=$2
replace_original=1

if [ "$command" = e ] || [ "$command" = exists ]; then
replace_original=0
fi

if [ "$file" = - ]; then
echo 'file must not be "-"' >/dev/stderr
exit 2
fi

temp=$(mktemp)
clean_up() {
rm "$temp" 2>/dev/null || true
}
trap clean_up EXIT HUP INT QUIT TERM

"${INITOOL:-initool}" "$@" >"$temp"
status=$?

if [ "$status" -eq 0 ] && [ "$replace_original" -eq 1 ]; then
mv "$temp" "$file"
else
exit "$status"
fi

0 comments on commit 01e7301

Please sign in to comment.