darwin-shell 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ """Package metadata and path resolution for darwin-shell.
2
+
3
+ Python is used only to locate, verify and export the shell modules. The
4
+ modules themselves have no Python runtime dependency.
5
+ """
6
+
7
+ from pathlib import Path
8
+
9
+ __version__ = "0.1.0"
10
+
11
+ SHARE_DIR = Path(__file__).resolve().parent / "share"
12
+
13
+ MODULES = (
14
+ "i18n",
15
+ "core",
16
+ "env",
17
+ "terminal",
18
+ "log",
19
+ "cli",
20
+ "file",
21
+ "json",
22
+ "datetime",
23
+ "lock",
24
+ "process",
25
+ "sched",
26
+ "checkpoint",
27
+ "stats",
28
+ "notify",
29
+ )
30
+
31
+
32
+ def lib_dir() -> Path:
33
+ """Return the absolute directory that contains the shipped shell modules."""
34
+ return SHARE_DIR
darwin_shell/cli.py ADDED
@@ -0,0 +1,140 @@
1
+ """darwin-sh: locate, verify and export darwin-shell modules."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import shlex
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ from . import MODULES, __version__, lib_dir
13
+
14
+
15
+ def _cmd_version(_args: argparse.Namespace) -> int:
16
+ """Print the installed version."""
17
+ print(__version__)
18
+ return 0
19
+
20
+
21
+ def _cmd_lib(args: argparse.Namespace) -> int:
22
+ """Print the library directory, or the path of one named module."""
23
+ root = lib_dir()
24
+ if args.module is None:
25
+ print(root)
26
+ return 0
27
+ if args.module not in MODULES:
28
+ print(f"darwin-sh: unknown module: {args.module}", file=sys.stderr)
29
+ return 2
30
+ print(root / f"{args.module}.sh")
31
+ return 0
32
+
33
+
34
+ def _cmd_init(_args: argparse.Namespace) -> int:
35
+ """Print a sourceable snippet that bootstraps the library."""
36
+ root = lib_dir()
37
+ print(f"DARWIN_SH_LIB_DIR={shlex.quote(str(root))}; export DARWIN_SH_LIB_DIR")
38
+ print(f"source {shlex.quote(str(root / 'bootstrap.sh'))}")
39
+ return 0
40
+
41
+
42
+ def _cmd_install(args: argparse.Namespace) -> int:
43
+ """Copy the shipped modules to a stable directory outside the Python env."""
44
+ target = Path(args.dir).expanduser()
45
+ target.mkdir(parents=True, exist_ok=True)
46
+ source = lib_dir()
47
+ for name in ("bootstrap.sh", *(f"{m}.sh" for m in MODULES)):
48
+ path = source / name
49
+ if path.is_file():
50
+ shutil.copy2(path, target / name)
51
+
52
+ locale_source = source / "locale"
53
+ if locale_source.is_dir():
54
+ locale_target = target / "locale"
55
+ locale_target.mkdir(parents=True, exist_ok=True)
56
+ for path in sorted(locale_source.glob("*.sh")):
57
+ shutil.copy2(path, locale_target / path.name)
58
+
59
+ print(f"DARWIN_SH_LIB_DIR={shlex.quote(str(target))}; export DARWIN_SH_LIB_DIR")
60
+ print(f"source {shlex.quote(str(target / 'bootstrap.sh'))}")
61
+ return 0
62
+
63
+
64
+ def _cmd_doctor(_args: argparse.Namespace) -> int:
65
+ """Report bootstrap prerequisites; exit non-zero only when loading is impossible."""
66
+ root = lib_dir()
67
+ bash = shutil.which("bash")
68
+ if bash is None:
69
+ # uv/isolated environments on Windows can drop bash from PATH; probe directly.
70
+ for candidate in ("/usr/bin/bash", "/bin/bash", "C:/Program Files/Git/usr/bin/bash.exe"):
71
+ if Path(candidate).is_file():
72
+ bash = candidate
73
+ break
74
+ if bash is None:
75
+ print("FAIL: bash not found on PATH", file=sys.stderr)
76
+ return 1
77
+ result = subprocess.run(
78
+ [bash, "-c", 'printf "%s" "${BASH_VERSINFO[0]}"'],
79
+ capture_output=True,
80
+ text=True,
81
+ check=False,
82
+ )
83
+ major = result.stdout.strip().splitlines()[0].strip() if result.stdout.strip() else ""
84
+ if not major.isdigit() or int(major) < 4:
85
+ print("FAIL: bash >= 4.0 is required", file=sys.stderr)
86
+ return 1
87
+ print(f"OK: darwin-shell {__version__}")
88
+ print(f"OK: bash {major}")
89
+ print(f"OK: lib dir {root}")
90
+ if not (root / "bootstrap.sh").is_file():
91
+ print("FAIL: bootstrap.sh is missing", file=sys.stderr)
92
+ return 1
93
+ missing = [m for m in MODULES if not (root / f"{m}.sh").is_file()]
94
+ if missing:
95
+ print("WARN: modules not yet shipped: " + ", ".join(missing))
96
+ if not (root / "locale" / "zh_CN.sh").is_file():
97
+ print("WARN: zh_CN locale table is missing")
98
+ for tool in ("shellcheck", "bats"):
99
+ state = "ok" if shutil.which(tool) else "absent (optional)"
100
+ print(f"INFO: {tool} {state}")
101
+ return 0
102
+
103
+
104
+ def main(argv: list[str] | None = None) -> int:
105
+ """Entry point for the ``darwin-sh`` console script."""
106
+ parser = argparse.ArgumentParser(
107
+ prog="darwin-sh",
108
+ description="Locate, verify and export darwin-shell modules.",
109
+ )
110
+ parser.add_argument(
111
+ "--version", action="version", version=f"%(prog)s {__version__}"
112
+ )
113
+ commands = parser.add_subparsers(dest="command", required=True)
114
+
115
+ commands.add_parser("version", help="print the installed version").set_defaults(
116
+ func=_cmd_version
117
+ )
118
+
119
+ lib_parser = commands.add_parser("lib", help="print a module path")
120
+ lib_parser.add_argument("module", nargs="?", help="module name, e.g. sched")
121
+ lib_parser.set_defaults(func=_cmd_lib)
122
+
123
+ commands.add_parser("init", help="print a sourceable bootstrap snippet").set_defaults(
124
+ func=_cmd_init
125
+ )
126
+ install_parser = commands.add_parser(
127
+ "install", help="copy modules to a stable directory"
128
+ )
129
+ install_parser.add_argument("--dir", required=True, dest="dir")
130
+ install_parser.set_defaults(func=_cmd_install)
131
+ commands.add_parser("doctor", help="check bootstrap prerequisites").set_defaults(
132
+ func=_cmd_doctor
133
+ )
134
+
135
+ args = parser.parse_args(argv)
136
+ return int(args.func(args))
137
+
138
+
139
+ if __name__ == "__main__":
140
+ sys.exit(main())
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell bootstrap: the only shell file a consumer sources directly.
3
+ # Contract: sourced only; sets no shell options, installs no trap, writes no
4
+ # stdout on success. Requires bash >= 4.3 (associative arrays and namerefs).
5
+
6
+ # Module-specific guard. An if-block keeps this safe under caller's set -e.
7
+ if [[ -n "${_dsh_private_bootstrap_sourced:-}" ]]; then
8
+ return 0
9
+ fi
10
+ _dsh_private_bootstrap_sourced=1
11
+ readonly _dsh_private_bootstrap_sourced
12
+
13
+ DARWIN_SH_VERSION="${DARWIN_SH_VERSION:-0.1.0}"
14
+ declare -gA _dsh_private_bootstrap_loaded=()
15
+ _dsh_private_bootstrap_self="${BASH_SOURCE[0]}"
16
+ _dsh_private_bootstrap_dir=''
17
+
18
+ _dsh_private_bootstrap_resolve_dir() {
19
+ local dir="${DARWIN_SH_LIB_DIR:-}"
20
+ if [[ -z "$dir" ]]; then
21
+ local src_dir
22
+ src_dir="$(cd -- "$(dirname -- "$_dsh_private_bootstrap_self")" >/dev/null 2>&1 && pwd -P)" || src_dir=''
23
+ dir="$src_dir"
24
+ fi
25
+ if [[ -z "$dir" ]] || [[ ! -d "$dir" ]]; then
26
+ printf 'dsh: cannot locate library directory; set DARWIN_SH_LIB_DIR\n' >&2
27
+ return 1
28
+ fi
29
+ printf '%s\n' "$dir"
30
+ }
31
+
32
+ _dsh_private_bootstrap_ensure_dir() {
33
+ if [[ -z "$_dsh_private_bootstrap_dir" ]]; then
34
+ _dsh_private_bootstrap_dir="$(_dsh_private_bootstrap_resolve_dir)" || return $?
35
+ fi
36
+ }
37
+
38
+ # Public: print the resolved library directory (stdout is data here).
39
+ dsh::lib_dir() {
40
+ _dsh_private_bootstrap_ensure_dir || return $?
41
+ printf '%s\n' "$_dsh_private_bootstrap_dir"
42
+ }
43
+
44
+ # Public: load named modules, at most once each, in argument order.
45
+ # Unknown or unreadable modules are usage errors (exit code 2).
46
+ dsh::use() {
47
+ local module path
48
+ if (( $# == 0 )); then
49
+ return 0
50
+ fi
51
+ _dsh_private_bootstrap_ensure_dir || return $?
52
+ for module in "$@"; do
53
+ case "$module" in
54
+ i18n|core|env|terminal|log|cli|file|json|datetime|lock|process|sched|checkpoint|stats|notify) ;;
55
+ *)
56
+ printf 'dsh: unknown module: %s\n' "$module" >&2
57
+ return 2
58
+ ;;
59
+ esac
60
+ if [[ -n "${_dsh_private_bootstrap_loaded[$module]:-}" ]]; then
61
+ continue
62
+ fi
63
+ path="${_dsh_private_bootstrap_dir}/${module}.sh"
64
+ if [[ ! -r "$path" ]]; then
65
+ printf 'dsh: cannot read module: %s\n' "$path" >&2
66
+ return 2
67
+ fi
68
+ if ! source "$path"; then
69
+ printf 'dsh: failed to load module: %s\n' "$module" >&2
70
+ return 1
71
+ fi
72
+ _dsh_private_bootstrap_loaded[$module]=1
73
+ done
74
+ }
@@ -0,0 +1,437 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell checkpoint: generic, content-agnostic task markers.
3
+ # Four mutually exclusive states are distinguished by file prefixes. This
4
+ # module never interprets marker JSON, never installs a trap, never changes
5
+ # shell options and never exits on behalf of the caller.
6
+
7
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
8
+ if declare -F dsh::use >/dev/null 2>&1; then
9
+ dsh::use i18n || return $?
10
+ else
11
+ _dsh_private_checkpoint_self="${BASH_SOURCE[0]}"
12
+ source "${_dsh_private_checkpoint_self%/*}/i18n.sh" || return $?
13
+ unset _dsh_private_checkpoint_self
14
+ fi
15
+ fi
16
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
17
+ if declare -F dsh::use >/dev/null 2>&1; then
18
+ dsh::use core || return $?
19
+ else
20
+ _dsh_private_checkpoint_self="${BASH_SOURCE[0]}"
21
+ source "${_dsh_private_checkpoint_self%/*}/core.sh" || return $?
22
+ unset _dsh_private_checkpoint_self
23
+ fi
24
+ fi
25
+ if ! declare -F dsh::file::atomic_write >/dev/null 2>&1; then
26
+ if declare -F dsh::use >/dev/null 2>&1; then
27
+ dsh::use file || return $?
28
+ else
29
+ _dsh_private_checkpoint_self="${BASH_SOURCE[0]}"
30
+ source "${_dsh_private_checkpoint_self%/*}/file.sh" || return $?
31
+ unset _dsh_private_checkpoint_self
32
+ fi
33
+ fi
34
+
35
+ if [[ -n "${_dsh_private_checkpoint_sourced:-}" ]]; then
36
+ return 0
37
+ fi
38
+ _dsh_private_checkpoint_sourced=1
39
+ readonly _dsh_private_checkpoint_sourced
40
+
41
+ # Configuration lives only in module-private state and is populated by an
42
+ # explicit configure call; sourcing this file has no filesystem side effects.
43
+ declare -gA _dsh_private_checkpoint_cfg=(
44
+ [root]=''
45
+ [layout]='<stage>/<source>/<type>/<date>/<file>.json'
46
+ [done_prefix]=''
47
+ [empty_prefix]='empty.'
48
+ [fail_prefix]='fail.'
49
+ [skip_prefix]='skip.'
50
+ )
51
+ declare -ga _dsh_private_checkpoint_slots=()
52
+
53
+ _dsh_private_checkpoint_help() {
54
+ case "${1:-}" in
55
+ configure)
56
+ dsh::i18n::t 'Usage: dsh::checkpoint::configure --root DIR --layout TEMPLATE [--done-prefix P] [--empty-prefix P] [--fail-prefix P] [--skip-prefix P]'
57
+ printf '\n'
58
+ dsh::i18n::t 'Set the checkpoint root, path layout and state file prefixes.'
59
+ ;;
60
+ path)
61
+ dsh::i18n::t 'Usage: dsh::checkpoint::path key=value [key=value...]'
62
+ printf '\n'
63
+ dsh::i18n::t 'Expand the configured layout into a base marker path.'
64
+ ;;
65
+ state)
66
+ dsh::i18n::t 'Usage: dsh::checkpoint::state <path>'
67
+ printf '\n'
68
+ dsh::i18n::t 'Print done, empty, fail, skip or none for a base marker path.'
69
+ ;;
70
+ mark)
71
+ dsh::i18n::t 'Usage: dsh::checkpoint::mark <path> <done|empty|fail|skip> [--json JSON]'
72
+ printf '\n'
73
+ dsh::i18n::t 'Atomically publish one state marker and clear the other three.'
74
+ ;;
75
+ read)
76
+ dsh::i18n::t 'Usage: dsh::checkpoint::read <path> [--state STATE]'
77
+ printf '\n'
78
+ dsh::i18n::t 'Print marker JSON verbatim; no schema or field interpretation.'
79
+ ;;
80
+ clear)
81
+ dsh::i18n::t 'Usage: dsh::checkpoint::clear <path> [--state STATE]'
82
+ printf '\n'
83
+ dsh::i18n::t 'Remove markers for a base path, or only one state.'
84
+ ;;
85
+ *)
86
+ dsh::i18n::t 'dsh::checkpoint: no help for %1$s' "${1:-<empty>}" >&2
87
+ return "$DSH_RC_USAGE"
88
+ ;;
89
+ esac
90
+ }
91
+
92
+ _dsh_private_checkpoint_safe_file() {
93
+ local name="${1:-}"
94
+ [[ "$name" != */* ]] || return 1
95
+ [[ "$name" != '..' && "$name" != '.' ]] || return 1
96
+ [[ "$name" != *'..'* ]] || return 1
97
+ return 0
98
+ }
99
+
100
+ _dsh_private_checkpoint_valid_state() {
101
+ case "${1:-}" in
102
+ done|empty|fail|skip) return 0 ;;
103
+ *) return 1 ;;
104
+ esac
105
+ }
106
+
107
+ # State files are checked most-severe first. A well-formed mark leaves exactly
108
+ # one file; this order only defines behaviour for a partially cleared marker.
109
+ _dsh_private_checkpoint_file_for() {
110
+ local base="$1" state="$2" dir name
111
+ dir="${base%/*}"
112
+ name="${base##*/}"
113
+ if [[ "$dir" == "$base" ]]; then
114
+ dir='.'
115
+ fi
116
+ case "$state" in
117
+ done) printf '%s/%s\n' "$dir" "$name" ;;
118
+ empty) printf '%s/%s%s\n' "$dir" "${_dsh_private_checkpoint_cfg[empty_prefix]}" "$name" ;;
119
+ fail) printf '%s/%s%s\n' "$dir" "${_dsh_private_checkpoint_cfg[fail_prefix]}" "$name" ;;
120
+ skip) printf '%s/%s%s\n' "$dir" "${_dsh_private_checkpoint_cfg[skip_prefix]}" "$name" ;;
121
+ *) return 1 ;;
122
+ esac
123
+ }
124
+
125
+ # Public: set the root, layout and prefixes. Values are private; no directory
126
+ # is created here so sourcing and configuring stay side-effect free.
127
+ dsh::checkpoint::configure() {
128
+ if [[ "${1:-}" == "--help" ]]; then
129
+ _dsh_private_checkpoint_help configure
130
+ return $?
131
+ fi
132
+ local root='' layout=''
133
+ local done_p="${_dsh_private_checkpoint_cfg[done_prefix]}"
134
+ local empty_p="${_dsh_private_checkpoint_cfg[empty_prefix]}"
135
+ local fail_p="${_dsh_private_checkpoint_cfg[fail_prefix]}"
136
+ local skip_p="${_dsh_private_checkpoint_cfg[skip_prefix]}"
137
+ while (( $# )); do
138
+ case "$1" in
139
+ --root|--layout|--done-prefix|--empty-prefix|--fail-prefix|--skip-prefix)
140
+ if (( $# < 2 )); then
141
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: %1$s requires a value' "$1"
142
+ return $?
143
+ fi
144
+ case "$1" in
145
+ --root) root="$2" ;;
146
+ --layout) layout="$2" ;;
147
+ --done-prefix) done_p="$2" ;;
148
+ --empty-prefix) empty_p="$2" ;;
149
+ --fail-prefix) fail_p="$2" ;;
150
+ --skip-prefix) skip_p="$2" ;;
151
+ esac
152
+ shift 2
153
+ ;;
154
+ *)
155
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: unknown option: %1$s' "$1"
156
+ return $?
157
+ ;;
158
+ esac
159
+ done
160
+ if [[ -z "$root" ]]; then
161
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::configure: --root requires a directory'
162
+ return $?
163
+ fi
164
+ if [[ -z "$layout" ]]; then
165
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::configure: --layout requires a template'
166
+ return $?
167
+ fi
168
+ local prefix
169
+ for prefix in "$done_p" "$empty_p" "$fail_p" "$skip_p"; do
170
+ if ! _dsh_private_checkpoint_safe_file "$prefix"; then
171
+ dsh::core::fail "$DSH_RC_USAGE" \
172
+ 'dsh::checkpoint::configure: prefix must not contain a path separator: %1$s' "$prefix"
173
+ return $?
174
+ fi
175
+ done
176
+
177
+ local -a slots=()
178
+ local rest="$layout" slot
179
+ while [[ "$rest" == *'<'*'>'* ]]; do
180
+ rest="${rest#*'<'}"
181
+ slot="${rest%%'>'*}"
182
+ [[ -n "$slot" ]] || {
183
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::configure: empty layout placeholder'
184
+ return $?
185
+ }
186
+ _dsh_private_checkpoint_safe_file "$slot" || {
187
+ dsh::core::fail "$DSH_RC_USAGE" \
188
+ 'dsh::checkpoint::configure: invalid layout placeholder: %1$s' "$slot"
189
+ return $?
190
+ }
191
+ slots+=("$slot")
192
+ rest="${rest#*'>'}"
193
+ done
194
+ if (( ${#slots[@]} == 0 )); then
195
+ dsh::core::fail "$DSH_RC_USAGE" \
196
+ 'dsh::checkpoint::configure: --layout needs at least one <placeholder>'
197
+ return $?
198
+ fi
199
+
200
+ _dsh_private_checkpoint_cfg[root]="$root"
201
+ _dsh_private_checkpoint_cfg[layout]="$layout"
202
+ _dsh_private_checkpoint_cfg[done_prefix]="$done_p"
203
+ _dsh_private_checkpoint_cfg[empty_prefix]="$empty_p"
204
+ _dsh_private_checkpoint_cfg[fail_prefix]="$fail_p"
205
+ _dsh_private_checkpoint_cfg[skip_prefix]="$skip_p"
206
+ _dsh_private_checkpoint_slots=("${slots[@]}")
207
+ return 0
208
+ }
209
+
210
+ # Public: expand the layout with key=value pairs. Empty values collapse the
211
+ # corresponding path segment, so a missing key can omit a directory layer.
212
+ dsh::checkpoint::path() {
213
+ if [[ "${1:-}" == "--help" ]]; then
214
+ _dsh_private_checkpoint_help path
215
+ return $?
216
+ fi
217
+ if [[ -z "${_dsh_private_checkpoint_cfg[root]}" ]]; then
218
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: configure must be called first'
219
+ return $?
220
+ fi
221
+ local -A values=()
222
+ local item key value
223
+ for item in "$@"; do
224
+ [[ "$item" == *=* ]] || {
225
+ dsh::core::fail "$DSH_RC_USAGE" \
226
+ 'dsh::checkpoint::path: expected key=value: %1$s' "$item"
227
+ return $?
228
+ }
229
+ key="${item%%=*}"
230
+ value="${item#*=}"
231
+ _dsh_private_checkpoint_safe_file "$key" || {
232
+ dsh::core::fail "$DSH_RC_USAGE" \
233
+ 'dsh::checkpoint::path: invalid key: %1$s' "$key"
234
+ return $?
235
+ }
236
+ values["$key"]="$value"
237
+ done
238
+
239
+ local slot
240
+ for slot in "${_dsh_private_checkpoint_slots[@]}"; do
241
+ if [[ -z "${values[$slot]+x}" ]]; then
242
+ dsh::core::fail "$DSH_RC_USAGE" \
243
+ 'dsh::checkpoint::path: missing value for %1$s' "$slot"
244
+ return $?
245
+ fi
246
+ done
247
+
248
+ local expanded="${_dsh_private_checkpoint_cfg[layout]}" result=''
249
+ while [[ "$expanded" == *'<'*'>'* ]]; do
250
+ result+="${expanded%%'<'*}"
251
+ slot="${expanded#*'<'}"
252
+ slot="${slot%%'>'*}"
253
+ result+="${values[$slot]}"
254
+ expanded="${expanded#*'>'}"
255
+ done
256
+ result+="$expanded"
257
+ # Collapse repeated separators so an empty value removes one directory layer.
258
+ while [[ "$result" == *'//'* ]]; do
259
+ result="${result//\/\//\/}"
260
+ done
261
+ printf '%s/%s\n' "${_dsh_private_checkpoint_cfg[root]}" "$result"
262
+ return 0
263
+ }
264
+
265
+ # Public: detect which state marker exists for a base path.
266
+ dsh::checkpoint::state() {
267
+ if [[ "${1:-}" == "--help" ]]; then
268
+ _dsh_private_checkpoint_help state
269
+ return $?
270
+ fi
271
+ if (( $# != 1 )); then
272
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::state: expected <path>'
273
+ return $?
274
+ fi
275
+ local base="$1" state file
276
+ for state in fail skip empty done; do
277
+ file="$(_dsh_private_checkpoint_file_for "$base" "$state")" || return $?
278
+ if [[ -f "$file" ]]; then
279
+ printf '%s\n' "$state"
280
+ return 0
281
+ fi
282
+ done
283
+ printf 'none\n'
284
+ return 0
285
+ }
286
+
287
+ # Public: publish a marker. The target is written atomically first; the other
288
+ # state files are removed afterwards. No JSON field is interpreted.
289
+ dsh::checkpoint::mark() {
290
+ if [[ "${1:-}" == "--help" ]]; then
291
+ _dsh_private_checkpoint_help mark
292
+ return $?
293
+ fi
294
+ if (( $# < 2 )); then
295
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::mark: expected <path> <state>'
296
+ return $?
297
+ fi
298
+ local base="$1" state="$2" json='{}'
299
+ shift 2
300
+ while (( $# )); do
301
+ case "$1" in
302
+ --json)
303
+ if (( $# < 2 )); then
304
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: --json requires a value'
305
+ return $?
306
+ fi
307
+ json="$2"
308
+ shift 2
309
+ ;;
310
+ *)
311
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: unknown option: %1$s' "$1"
312
+ return $?
313
+ ;;
314
+ esac
315
+ done
316
+ if ! _dsh_private_checkpoint_valid_state "$state"; then
317
+ dsh::core::fail "$DSH_RC_USAGE" \
318
+ 'dsh::checkpoint::mark: invalid state: %1$s' "$state"
319
+ return $?
320
+ fi
321
+
322
+ local target dir other file
323
+ target="$(_dsh_private_checkpoint_file_for "$base" "$state")" || return $?
324
+ dir="${target%/*}"
325
+ mkdir -p "$dir" || {
326
+ dsh::core::fail "$DSH_RC_FAILURE" \
327
+ 'dsh::checkpoint::mark: cannot create directory: %1$s' "$dir"
328
+ return $?
329
+ }
330
+ printf '%s\n' "$json" | dsh::file::atomic_write "$target" || return $?
331
+ for other in done empty fail skip; do
332
+ [[ "$other" == "$state" ]] && continue
333
+ file="$(_dsh_private_checkpoint_file_for "$base" "$other")" || return $?
334
+ rm -f -- "$file" || {
335
+ dsh::core::fail "$DSH_RC_FAILURE" \
336
+ 'dsh::checkpoint::mark: cannot clear marker: %1$s' "$file"
337
+ return $?
338
+ }
339
+ done
340
+ return 0
341
+ }
342
+
343
+ # Public: print marker JSON verbatim. No jq, no schema, no field lookup.
344
+ dsh::checkpoint::read() {
345
+ if [[ "${1:-}" == "--help" ]]; then
346
+ _dsh_private_checkpoint_help read
347
+ return $?
348
+ fi
349
+ if (( $# < 1 )); then
350
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::read: expected <path>'
351
+ return $?
352
+ fi
353
+ local base="$1" state='' file
354
+ shift
355
+ while (( $# )); do
356
+ case "$1" in
357
+ --state)
358
+ if (( $# < 2 )); then
359
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: --state requires a value'
360
+ return $?
361
+ fi
362
+ state="$2"
363
+ shift 2
364
+ ;;
365
+ *)
366
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: unknown option: %1$s' "$1"
367
+ return $?
368
+ ;;
369
+ esac
370
+ done
371
+ if [[ -z "$state" ]]; then
372
+ state="$(dsh::checkpoint::state "$base")" || return $?
373
+ [[ "$state" != none ]] || {
374
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::read: no marker for %1$s' "$base"
375
+ return $?
376
+ }
377
+ fi
378
+ if ! _dsh_private_checkpoint_valid_state "$state"; then
379
+ dsh::core::fail "$DSH_RC_USAGE" \
380
+ 'dsh::checkpoint::read: invalid state: %1$s' "$state"
381
+ return $?
382
+ fi
383
+ file="$(_dsh_private_checkpoint_file_for "$base" "$state")" || return $?
384
+ if [[ ! -f "$file" ]]; then
385
+ dsh::core::fail "$DSH_RC_USAGE" \
386
+ 'dsh::checkpoint::read: marker not found: %1$s' "$file"
387
+ return $?
388
+ fi
389
+ cat -- "$file"
390
+ return $?
391
+ }
392
+
393
+ # Public: remove one or all state markers for a base path.
394
+ dsh::checkpoint::clear() {
395
+ if [[ "${1:-}" == "--help" ]]; then
396
+ _dsh_private_checkpoint_help clear
397
+ return $?
398
+ fi
399
+ if (( $# < 1 )); then
400
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint::clear: expected <path>'
401
+ return $?
402
+ fi
403
+ local base="$1" state='' file
404
+ shift
405
+ while (( $# )); do
406
+ case "$1" in
407
+ --state)
408
+ if (( $# < 2 )); then
409
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: --state requires a value'
410
+ return $?
411
+ fi
412
+ state="$2"
413
+ shift 2
414
+ ;;
415
+ *)
416
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::checkpoint: unknown option: %1$s' "$1"
417
+ return $?
418
+ ;;
419
+ esac
420
+ done
421
+ if [[ -n "$state" ]] && ! _dsh_private_checkpoint_valid_state "$state"; then
422
+ dsh::core::fail "$DSH_RC_USAGE" \
423
+ 'dsh::checkpoint::clear: invalid state: %1$s' "$state"
424
+ return $?
425
+ fi
426
+ local current
427
+ for current in done empty fail skip; do
428
+ [[ -z "$state" || "$current" == "$state" ]] || continue
429
+ file="$(_dsh_private_checkpoint_file_for "$base" "$current")" || return $?
430
+ rm -f -- "$file" || {
431
+ dsh::core::fail "$DSH_RC_FAILURE" \
432
+ 'dsh::checkpoint::clear: cannot remove marker: %1$s' "$file"
433
+ return $?
434
+ }
435
+ done
436
+ return 0
437
+ }