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.
- darwin_shell/__init__.py +34 -0
- darwin_shell/cli.py +140 -0
- darwin_shell/share/bootstrap.sh +74 -0
- darwin_shell/share/checkpoint.sh +437 -0
- darwin_shell/share/cli.sh +555 -0
- darwin_shell/share/core.sh +214 -0
- darwin_shell/share/datetime.sh +367 -0
- darwin_shell/share/env.sh +322 -0
- darwin_shell/share/file.sh +303 -0
- darwin_shell/share/i18n.sh +181 -0
- darwin_shell/share/json.sh +258 -0
- darwin_shell/share/locale/zh_CN.sh +456 -0
- darwin_shell/share/lock.sh +414 -0
- darwin_shell/share/log.sh +317 -0
- darwin_shell/share/notify.sh +576 -0
- darwin_shell/share/process.sh +394 -0
- darwin_shell/share/sched.sh +360 -0
- darwin_shell/share/stats.sh +540 -0
- darwin_shell/share/terminal.sh +226 -0
- darwin_shell-0.1.0.dist-info/METADATA +208 -0
- darwin_shell-0.1.0.dist-info/RECORD +25 -0
- darwin_shell-0.1.0.dist-info/WHEEL +5 -0
- darwin_shell-0.1.0.dist-info/entry_points.txt +2 -0
- darwin_shell-0.1.0.dist-info/licenses/LICENSE +21 -0
- darwin_shell-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# darwin-shell i18n: English msgids with a runtime lookup table. Missing
|
|
3
|
+
# translations fall back to the msgid itself, so a language gap never prints
|
|
4
|
+
# an empty string or a raw key. This is the lowest layer and depends on no
|
|
5
|
+
# other module; it writes its own usage errors directly to stderr.
|
|
6
|
+
|
|
7
|
+
if [[ -n "${_dsh_private_i18n_sourced:-}" ]]; then
|
|
8
|
+
return 0
|
|
9
|
+
fi
|
|
10
|
+
_dsh_private_i18n_sourced=1
|
|
11
|
+
readonly _dsh_private_i18n_sourced
|
|
12
|
+
|
|
13
|
+
declare -gA _dsh_private_i18n_table=()
|
|
14
|
+
_dsh_private_i18n_lang='en'
|
|
15
|
+
|
|
16
|
+
_dsh_private_i18n_help() {
|
|
17
|
+
case "${1:-}" in
|
|
18
|
+
t)
|
|
19
|
+
printf '%s\n' \
|
|
20
|
+
'Usage: dsh::i18n::t [-n] <msgid> [args...]' \
|
|
21
|
+
'' \
|
|
22
|
+
'Render <msgid>, using positional placeholders such as %1$s when args' \
|
|
23
|
+
'are supplied. Missing translations fall back to the English msgid.' \
|
|
24
|
+
'With -n, omit the trailing newline.'
|
|
25
|
+
;;
|
|
26
|
+
set_lang)
|
|
27
|
+
printf '%s\n' \
|
|
28
|
+
'Usage: dsh::i18n::set_lang <zh|en|auto>' \
|
|
29
|
+
'' \
|
|
30
|
+
'Switch the active language and reload its table. "auto" resolves' \
|
|
31
|
+
'from LC_ALL / LC_MESSAGES / LANG, defaulting to English.'
|
|
32
|
+
;;
|
|
33
|
+
lang)
|
|
34
|
+
printf '%s\n' \
|
|
35
|
+
'Usage: dsh::i18n::lang' \
|
|
36
|
+
'' \
|
|
37
|
+
'Print the resolved language code: zh or en.'
|
|
38
|
+
;;
|
|
39
|
+
*)
|
|
40
|
+
printf 'dsh::i18n: no help for %s\n' "${1:-<empty>}" >&2
|
|
41
|
+
return 2
|
|
42
|
+
;;
|
|
43
|
+
esac
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_dsh_private_i18n_dir() {
|
|
47
|
+
local dir="${DARWIN_SH_LIB_DIR:-}"
|
|
48
|
+
if [[ -z "$dir" ]]; then
|
|
49
|
+
local src="${BASH_SOURCE[0]}"
|
|
50
|
+
dir="$(cd -- "$(dirname -- "$src")" >/dev/null 2>&1 && pwd -P)" || dir=''
|
|
51
|
+
fi
|
|
52
|
+
printf '%s\n' "$dir"
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_dsh_private_i18n_resolve() {
|
|
56
|
+
local explicit="${DARWIN_SH_LANG:-auto}"
|
|
57
|
+
case "$explicit" in
|
|
58
|
+
zh|zh_*|zh-*) printf 'zh\n'; return 0 ;;
|
|
59
|
+
en|en_*|en-*) printf 'en\n'; return 0 ;;
|
|
60
|
+
auto|'') ;;
|
|
61
|
+
*) printf 'dsh::i18n: unknown DARWIN_SH_LANG=%s; using auto\n' "$explicit" >&2 ;;
|
|
62
|
+
esac
|
|
63
|
+
local loc="${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}"
|
|
64
|
+
case "$loc" in
|
|
65
|
+
zh|zh_*|zh-*) printf 'zh\n' ;;
|
|
66
|
+
*) printf 'en\n' ;;
|
|
67
|
+
esac
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_dsh_private_i18n_load_table() {
|
|
71
|
+
_dsh_private_i18n_table=()
|
|
72
|
+
if [[ "$_dsh_private_i18n_lang" != "zh" ]]; then
|
|
73
|
+
return 0
|
|
74
|
+
fi
|
|
75
|
+
local dir path
|
|
76
|
+
dir="$(_dsh_private_i18n_dir)" || return $?
|
|
77
|
+
path="$dir/locale/zh_CN.sh"
|
|
78
|
+
if [[ ! -r "$path" ]]; then
|
|
79
|
+
printf 'dsh::i18n: locale table missing: %s; falling back to English\n' "$path" >&2
|
|
80
|
+
return 0
|
|
81
|
+
fi
|
|
82
|
+
source "$path" || return $?
|
|
83
|
+
return 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
# Render %1$s, %2$s, ... by scanning, not printf: bash's builtin printf has
|
|
87
|
+
# no positional specifiers, and an external printf would add a dependency.
|
|
88
|
+
# A literal % that is not followed by <digits>$s is preserved verbatim.
|
|
89
|
+
# Global flag: when 1, the caller wants no trailing newline.
|
|
90
|
+
_dsh_private_i18n_no_newline=0
|
|
91
|
+
|
|
92
|
+
_dsh_private_i18n_render() {
|
|
93
|
+
local template="$1" out='' ch next idx j i=0
|
|
94
|
+
shift
|
|
95
|
+
while (( i < ${#template} )); do
|
|
96
|
+
ch="${template:i:1}"
|
|
97
|
+
if [[ "$ch" == '%' ]] && (( i + 3 < ${#template} )); then
|
|
98
|
+
next="${template:i+1:1}"
|
|
99
|
+
if [[ "$next" =~ ^[0-9]$ ]]; then
|
|
100
|
+
idx="$next"
|
|
101
|
+
j=$((i + 2))
|
|
102
|
+
while [[ "${template:j:1}" =~ ^[0-9]$ ]]; do
|
|
103
|
+
idx+="${template:j:1}"
|
|
104
|
+
((++j))
|
|
105
|
+
done
|
|
106
|
+
if [[ "${template:j:1}" == '$' && "${template:j+1:1}" == 's' ]] \
|
|
107
|
+
&& (( idx >= 1 && idx <= $# )); then
|
|
108
|
+
out+="${!idx}"
|
|
109
|
+
i=$((j + 2))
|
|
110
|
+
continue
|
|
111
|
+
fi
|
|
112
|
+
fi
|
|
113
|
+
fi
|
|
114
|
+
out+="$ch"
|
|
115
|
+
((++i))
|
|
116
|
+
done
|
|
117
|
+
if (( _dsh_private_i18n_no_newline == 1 )); then
|
|
118
|
+
printf '%s' "$out"
|
|
119
|
+
else
|
|
120
|
+
printf '%s\n' "$out"
|
|
121
|
+
fi
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# Public: render a msgid with optional positional arguments. A literal % in
|
|
125
|
+
# the msgid is safe when no args are supplied. -n omits the trailing newline.
|
|
126
|
+
dsh::i18n::t() {
|
|
127
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
128
|
+
_dsh_private_i18n_help t
|
|
129
|
+
return $?
|
|
130
|
+
fi
|
|
131
|
+
local no_newline=0
|
|
132
|
+
if [[ "${1:-}" == "-n" ]]; then
|
|
133
|
+
no_newline=1
|
|
134
|
+
shift
|
|
135
|
+
fi
|
|
136
|
+
if (( $# == 0 )); then
|
|
137
|
+
printf 'dsh::i18n::t: msgid required\n' >&2
|
|
138
|
+
return 2
|
|
139
|
+
fi
|
|
140
|
+
local msgid="$1" template
|
|
141
|
+
shift
|
|
142
|
+
template="${_dsh_private_i18n_table[$msgid]-$msgid}"
|
|
143
|
+
_dsh_private_i18n_no_newline=$no_newline
|
|
144
|
+
_dsh_private_i18n_render "$template" "$@"
|
|
145
|
+
local rc=$?
|
|
146
|
+
_dsh_private_i18n_no_newline=0
|
|
147
|
+
return $rc
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
# Public: switch language at runtime. Unknown values are usage errors.
|
|
151
|
+
dsh::i18n::set_lang() {
|
|
152
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
153
|
+
_dsh_private_i18n_help set_lang
|
|
154
|
+
return $?
|
|
155
|
+
fi
|
|
156
|
+
local requested="${1:-}"
|
|
157
|
+
case "$requested" in
|
|
158
|
+
zh|zh_*|zh-*) _dsh_private_i18n_lang='zh' ;;
|
|
159
|
+
en|en_*|en-*) _dsh_private_i18n_lang='en' ;;
|
|
160
|
+
auto|'') _dsh_private_i18n_lang="$(_dsh_private_i18n_resolve)" ;;
|
|
161
|
+
*)
|
|
162
|
+
printf 'dsh::i18n::set_lang: invalid language: %s\n' "$requested" >&2
|
|
163
|
+
return 2
|
|
164
|
+
;;
|
|
165
|
+
esac
|
|
166
|
+
_dsh_private_i18n_load_table
|
|
167
|
+
return $?
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
# Public: print the active language code.
|
|
171
|
+
dsh::i18n::lang() {
|
|
172
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
173
|
+
_dsh_private_i18n_help lang
|
|
174
|
+
return $?
|
|
175
|
+
fi
|
|
176
|
+
printf '%s\n' "$_dsh_private_i18n_lang"
|
|
177
|
+
return 0
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
_dsh_private_i18n_lang="$(_dsh_private_i18n_resolve)"
|
|
181
|
+
_dsh_private_i18n_load_table
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# darwin-shell json: jq-backed JSON reading and atomic writing.
|
|
3
|
+
# The jq dependency is resolved at call time through core::tool, so sourcing
|
|
4
|
+
# this module has no side effects and a missing jq is a reportable status.
|
|
5
|
+
|
|
6
|
+
# Dependencies sit below this module. Load them before the guard so a failed
|
|
7
|
+
# dependency is retryable on the next source attempt.
|
|
8
|
+
if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
|
|
9
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
10
|
+
dsh::use i18n || return $?
|
|
11
|
+
else
|
|
12
|
+
_dsh_private_json_self="${BASH_SOURCE[0]}"
|
|
13
|
+
source "${_dsh_private_json_self%/*}/i18n.sh" || return $?
|
|
14
|
+
unset _dsh_private_json_self
|
|
15
|
+
fi
|
|
16
|
+
fi
|
|
17
|
+
if ! declare -F dsh::core::fail >/dev/null 2>&1; then
|
|
18
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
19
|
+
dsh::use core || return $?
|
|
20
|
+
else
|
|
21
|
+
_dsh_private_json_self="${BASH_SOURCE[0]}"
|
|
22
|
+
source "${_dsh_private_json_self%/*}/core.sh" || return $?
|
|
23
|
+
unset _dsh_private_json_self
|
|
24
|
+
fi
|
|
25
|
+
fi
|
|
26
|
+
if ! declare -F dsh::file::atomic_write >/dev/null 2>&1; then
|
|
27
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
28
|
+
dsh::use file || return $?
|
|
29
|
+
else
|
|
30
|
+
_dsh_private_json_self="${BASH_SOURCE[0]}"
|
|
31
|
+
source "${_dsh_private_json_self%/*}/file.sh" || return $?
|
|
32
|
+
unset _dsh_private_json_self
|
|
33
|
+
fi
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
if [[ -n "${_dsh_private_json_sourced:-}" ]]; then
|
|
37
|
+
return 0
|
|
38
|
+
fi
|
|
39
|
+
_dsh_private_json_sourced=1
|
|
40
|
+
readonly _dsh_private_json_sourced
|
|
41
|
+
|
|
42
|
+
_dsh_private_json_help() {
|
|
43
|
+
case "${1:-}" in
|
|
44
|
+
valid)
|
|
45
|
+
dsh::i18n::t 'Usage: dsh::json::valid <file>'
|
|
46
|
+
printf '\n'
|
|
47
|
+
dsh::i18n::t 'Return 0 when <file> contains valid JSON, 2 when missing.'
|
|
48
|
+
;;
|
|
49
|
+
exists)
|
|
50
|
+
dsh::i18n::t 'Usage: dsh::json::exists <file>'
|
|
51
|
+
printf '\n'
|
|
52
|
+
dsh::i18n::t 'Return 0 when <file> exists and is non-empty. No jq dependency.'
|
|
53
|
+
;;
|
|
54
|
+
get)
|
|
55
|
+
dsh::i18n::t 'Usage: dsh::json::get <file> <filter> [--default V]'
|
|
56
|
+
printf '\n'
|
|
57
|
+
dsh::i18n::t 'Print a jq -r result; empty or null results fall back to --default.'
|
|
58
|
+
;;
|
|
59
|
+
get_raw)
|
|
60
|
+
dsh::i18n::t 'Usage: dsh::json::get_raw <file> <filter>'
|
|
61
|
+
printf '\n'
|
|
62
|
+
dsh::i18n::t 'Print the raw jq result, preserving JSON types.'
|
|
63
|
+
;;
|
|
64
|
+
set)
|
|
65
|
+
dsh::i18n::t 'Usage: dsh::json::set <file> <filter> [--arg K=V] [--argjson K=V]...'
|
|
66
|
+
printf '\n'
|
|
67
|
+
dsh::i18n::t 'Apply a jq filter and atomically replace <file> with the result.'
|
|
68
|
+
;;
|
|
69
|
+
*)
|
|
70
|
+
dsh::i18n::t 'dsh::json: no help for %1$s' "${1:-<empty>}" >&2
|
|
71
|
+
return "$DSH_RC_USAGE"
|
|
72
|
+
;;
|
|
73
|
+
esac
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
# Public: print the resolved jq command. Missing jq is a transient status,
|
|
77
|
+
# not a load-time fatal, so callers can degrade gracefully.
|
|
78
|
+
dsh::json::jq() {
|
|
79
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
80
|
+
dsh::i18n::t 'Usage: dsh::json::jq'
|
|
81
|
+
printf '\n'
|
|
82
|
+
dsh::i18n::t 'Print the jq command selected by core::tool.'
|
|
83
|
+
return 0
|
|
84
|
+
fi
|
|
85
|
+
dsh::core::tool jq
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
# Public: validate a JSON file with jq empty.
|
|
89
|
+
dsh::json::valid() {
|
|
90
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
91
|
+
_dsh_private_json_help valid
|
|
92
|
+
return $?
|
|
93
|
+
fi
|
|
94
|
+
if (( $# != 1 )); then
|
|
95
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: missing argument' valid
|
|
96
|
+
return $?
|
|
97
|
+
fi
|
|
98
|
+
local file="$1" jq_bin
|
|
99
|
+
if [[ ! -f "$file" ]]; then
|
|
100
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: file not found: %2$s' valid "$file"
|
|
101
|
+
return $?
|
|
102
|
+
fi
|
|
103
|
+
jq_bin="$(dsh::json::jq)" || return $?
|
|
104
|
+
if ! "$jq_bin" empty "$file" >/dev/null 2>&1; then
|
|
105
|
+
dsh::core::fail "$DSH_RC_FAILURE" 'dsh::json::%1$s: invalid JSON: %2$s' valid "$file"
|
|
106
|
+
return $?
|
|
107
|
+
fi
|
|
108
|
+
return 0
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
# Public: test that a file exists and is non-empty. Pure bash, no jq needed.
|
|
112
|
+
dsh::json::exists() {
|
|
113
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
114
|
+
_dsh_private_json_help exists
|
|
115
|
+
return $?
|
|
116
|
+
fi
|
|
117
|
+
if (( $# != 1 )); then
|
|
118
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: missing argument' exists
|
|
119
|
+
return $?
|
|
120
|
+
fi
|
|
121
|
+
[[ -s "$1" ]]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# Public: read a jq -r result. The filter is executed as-is: unlike the old
|
|
125
|
+
# "// empty" injection, a legitimate false is preserved. Empty output or the
|
|
126
|
+
# literal null falls back to --default.
|
|
127
|
+
dsh::json::get() {
|
|
128
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
129
|
+
_dsh_private_json_help get
|
|
130
|
+
return $?
|
|
131
|
+
fi
|
|
132
|
+
if (( $# < 2 )); then
|
|
133
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: missing argument' get
|
|
134
|
+
return $?
|
|
135
|
+
fi
|
|
136
|
+
local file="$1" filter="$2" default='' has_default=0
|
|
137
|
+
shift 2
|
|
138
|
+
while (( $# )); do
|
|
139
|
+
case "$1" in
|
|
140
|
+
--default)
|
|
141
|
+
if (( $# < 2 )); then
|
|
142
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::get: --default requires a value'
|
|
143
|
+
return $?
|
|
144
|
+
fi
|
|
145
|
+
default="$2"
|
|
146
|
+
has_default=1
|
|
147
|
+
shift 2
|
|
148
|
+
;;
|
|
149
|
+
*)
|
|
150
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: unknown option: %2$s' get "$1"
|
|
151
|
+
return $?
|
|
152
|
+
;;
|
|
153
|
+
esac
|
|
154
|
+
done
|
|
155
|
+
|
|
156
|
+
if [[ ! -f "$file" ]]; then
|
|
157
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: file not found: %2$s' get "$file"
|
|
158
|
+
return $?
|
|
159
|
+
fi
|
|
160
|
+
local jq_bin out
|
|
161
|
+
jq_bin="$(dsh::json::jq)" || return $?
|
|
162
|
+
if ! out="$("$jq_bin" -r "$filter" "$file" 2>/dev/null)"; then
|
|
163
|
+
dsh::core::fail "$DSH_RC_FAILURE" 'dsh::json::%1$s: jq failed for: %2$s' get "$file"
|
|
164
|
+
return $?
|
|
165
|
+
fi
|
|
166
|
+
if [[ -z "$out" || "$out" == null ]]; then
|
|
167
|
+
if (( has_default )); then
|
|
168
|
+
out="$default"
|
|
169
|
+
fi
|
|
170
|
+
fi
|
|
171
|
+
printf '%s\n' "$out"
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
# Public: read the raw jq result, keeping JSON types and quoting.
|
|
175
|
+
dsh::json::get_raw() {
|
|
176
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
177
|
+
_dsh_private_json_help get_raw
|
|
178
|
+
return $?
|
|
179
|
+
fi
|
|
180
|
+
if (( $# != 2 )); then
|
|
181
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: expected <file> <filter>' get_raw
|
|
182
|
+
return $?
|
|
183
|
+
fi
|
|
184
|
+
local file="$1" filter="$2" jq_bin out
|
|
185
|
+
if [[ ! -f "$file" ]]; then
|
|
186
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: file not found: %2$s' get_raw "$file"
|
|
187
|
+
return $?
|
|
188
|
+
fi
|
|
189
|
+
jq_bin="$(dsh::json::jq)" || return $?
|
|
190
|
+
if ! out="$("$jq_bin" "$filter" "$file" 2>/dev/null)"; then
|
|
191
|
+
dsh::core::fail "$DSH_RC_FAILURE" 'dsh::json::%1$s: jq failed for: %2$s' get_raw "$file"
|
|
192
|
+
return $?
|
|
193
|
+
fi
|
|
194
|
+
printf '%s\n' "$out"
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
# Public: apply a jq filter and atomically replace <file>. Values are passed
|
|
198
|
+
# as --arg/--argjson pairs so they are never interpolated into the filter.
|
|
199
|
+
# jq must succeed and produce a non-empty document before any write happens.
|
|
200
|
+
dsh::json::set() {
|
|
201
|
+
if [[ "${1:-}" == "--help" ]]; then
|
|
202
|
+
_dsh_private_json_help set
|
|
203
|
+
return $?
|
|
204
|
+
fi
|
|
205
|
+
if (( $# < 2 )); then
|
|
206
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::set: expected <file> <filter> [options]'
|
|
207
|
+
return $?
|
|
208
|
+
fi
|
|
209
|
+
local file="$1" filter="$2" spec name value
|
|
210
|
+
shift 2
|
|
211
|
+
local -a jq_extra=()
|
|
212
|
+
while (( $# )); do
|
|
213
|
+
case "$1" in
|
|
214
|
+
--arg|--argjson)
|
|
215
|
+
if (( $# < 2 )); then
|
|
216
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::set: %1$s requires K=V' "$1"
|
|
217
|
+
return $?
|
|
218
|
+
fi
|
|
219
|
+
spec="$2"
|
|
220
|
+
if [[ "$spec" != *=* ]]; then
|
|
221
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::set: %1$s requires K=V' "$1"
|
|
222
|
+
return $?
|
|
223
|
+
fi
|
|
224
|
+
name="${spec%%=*}"
|
|
225
|
+
value="${spec#*=}"
|
|
226
|
+
if [[ ! "$name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
|
227
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::set: invalid argument name: %1$s' "$name"
|
|
228
|
+
return $?
|
|
229
|
+
fi
|
|
230
|
+
jq_extra+=("$1" "$name" "$value")
|
|
231
|
+
shift 2
|
|
232
|
+
;;
|
|
233
|
+
*)
|
|
234
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: unknown option: %2$s' set "$1"
|
|
235
|
+
return $?
|
|
236
|
+
;;
|
|
237
|
+
esac
|
|
238
|
+
done
|
|
239
|
+
|
|
240
|
+
if [[ ! -f "$file" ]]; then
|
|
241
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::json::%1$s: file not found: %2$s' set "$file"
|
|
242
|
+
return $?
|
|
243
|
+
fi
|
|
244
|
+
local jq_bin rendered
|
|
245
|
+
jq_bin="$(dsh::json::jq)" || return $?
|
|
246
|
+
if ! rendered="$("$jq_bin" "${jq_extra[@]}" "$filter" "$file" 2>/dev/null)"; then
|
|
247
|
+
dsh::core::fail "$DSH_RC_FAILURE" 'dsh::json::%1$s: jq failed for: %2$s' set "$file"
|
|
248
|
+
return $?
|
|
249
|
+
fi
|
|
250
|
+
if [[ -z "$rendered" ]]; then
|
|
251
|
+
dsh::core::fail "$DSH_RC_FAILURE" 'dsh::json::set: filter produced no output'
|
|
252
|
+
return $?
|
|
253
|
+
fi
|
|
254
|
+
if ! printf '%s\n' "$rendered" | dsh::file::atomic_write "$file"; then
|
|
255
|
+
return $?
|
|
256
|
+
fi
|
|
257
|
+
return 0
|
|
258
|
+
}
|