Skip to content

Commit 072e2f5

Browse files
committed
feat(scripts): add initool-overwrite.* scripts
1 parent 1ac5b20 commit 072e2f5

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ If the filename is `-`, initool reads standard input. For the commands `get`, `s
5757
For `exists`, it reports whether the section or the property exists through its exit status.
5858

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

6367
An INI file consists of properties (`key=value` lines) and sections (designated with a `[section name]` header line).
6468
A property can be at the "top level" of the file (before any section headers) or in a section (after a section header).

initool-overwrite.cmd

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
for %%a in (%*) do (
5+
if "%%a"=="-h" (
6+
goto :help
7+
)
8+
if "%%a"=="--help" (
9+
goto :help
10+
)
11+
)
12+
13+
if "%~2"=="" (
14+
goto :error_usage
15+
)
16+
17+
set "command=%~1"
18+
set "file=%~2"
19+
set status=0
20+
set overwrite=1
21+
22+
if "%command%"=="e" (
23+
set overwrite=0
24+
)
25+
if "%command%"=="exists" (
26+
set overwrite=0
27+
)
28+
29+
if "%file%"=="-" (
30+
echo file must not be "-" 1>&2
31+
exit /b 2
32+
)
33+
34+
:create_temp
35+
set "temp=%temp%\initool-in-place-%random%.tmp"
36+
if exist "!temp!" goto :create_temp
37+
38+
if defined INITOOL (
39+
"!INITOOL!" %* > "!temp!"
40+
) else (
41+
initool %* > "!temp!"
42+
)
43+
44+
set status=%errorlevel%
45+
if %status% equ 0 (
46+
if %overwrite% equ 1 (
47+
copy /y "!temp!" "%file%" > nul
48+
)
49+
)
50+
51+
:cleanup
52+
del "%temp%"
53+
exit /b %status%
54+
55+
:error_usage
56+
echo usage: %~nx0 command file [arg ...] 1>&2
57+
exit /b 2
58+
59+
:help
60+
echo Modify the input file with initool.
61+
echo.
62+
echo usage: %~nx0 command file [arg ...]
63+
echo.
64+
echo You can give the path to initool in the environment variable "INITOOL".
65+
exit /b

initool-overwrite.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#! /bin/sh
2+
set -eu
3+
4+
usage() {
5+
echo "usage: $(basename "$0") command file [arg ...]"
6+
}
7+
8+
help() {
9+
printf 'Modify the input file with initool.\n\n'
10+
usage
11+
printf '\nYou can give the path to initool in the environment variable "INITOOL".\n'
12+
}
13+
14+
for arg in "$@"; do
15+
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
16+
help
17+
exit 0
18+
fi
19+
done
20+
21+
if [ $# -lt 2 ]; then
22+
usage >/dev/stderr
23+
exit 2
24+
fi
25+
26+
command=$1
27+
file=$2
28+
replace_original=1
29+
30+
if [ "$command" = e ] || [ "$command" = exists ]; then
31+
replace_original=0
32+
fi
33+
34+
if [ "$file" = - ]; then
35+
echo 'file must not be "-"' >/dev/stderr
36+
exit 2
37+
fi
38+
39+
temp=$(mktemp)
40+
clean_up() {
41+
rm "$temp" 2>/dev/null || true
42+
}
43+
trap clean_up EXIT HUP INT QUIT TERM
44+
45+
"${INITOOL:-initool}" "$@" >"$temp"
46+
status=$?
47+
48+
if [ "$status" -eq 0 ] && [ "$replace_original" -eq 1 ]; then
49+
mv "$temp" "$file"
50+
else
51+
exit "$status"
52+
fi

0 commit comments

Comments
 (0)