Skip to content

Commit 9e4f9eb

Browse files
committed
- finish implementing __slice
- prevent variable conflicts on eval'd statments - consistent reference behaviour - consistent return handling on print lines - consistent index syntax, `$` is only necessary on associative arrays - fix a syntax error on bash 4.3 and 4.4 - some mild improvements to `symlink-helper` - `errors.md` make the problems a little bit clear, even if it has some duplication
1 parent 6489f08 commit 9e4f9eb

10 files changed

Lines changed: 915 additions & 530 deletions

File tree

commands/confirm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ function confirm_() (
255255
local str='' index
256256
for index in "${!options_unselected[@]}"; do
257257
if [[ -n $selected_index && $index -eq $selected_index ]]; then
258-
str+="${options_selected[$index]}"
258+
str+="${options_selected[index]}"
259259
else
260-
str+="${options_unselected[$index]}"
260+
str+="${options_unselected[index]}"
261261
fi
262262
done
263263
__do --redirect-stdout=tty -- __print_string "$str "

commands/dorothy-internals

Lines changed: 158 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,41 +2110,192 @@ fi
21102110

21112111
if __begin_test_block --name='__slice'; then
21122112
function slice_tests {
2113-
local arr
2113+
local arr target
2114+
2115+
__print_lines 'start, no indices, no-op:'
2116+
arr=(a b c d)
2117+
__slice {arr}
2118+
echo-verbose --no-color -- "${arr[@]}"
2119+
2120+
__print_lines 'start, empty indices, failure:'
2121+
__slice {arr} '' || __print_lines "status: $?"
2122+
2123+
__print_lines 'start, out of bounds:'
2124+
arr=(a b c d)
2125+
__slice {arr} 10
2126+
echo-verbose --no-color -- "${arr[@]}"
2127+
2128+
__print_lines 'start, negative, out of bounds:'
2129+
arr=(a b c d)
2130+
__slice {arr} -10
2131+
echo-verbose -- "${arr[@]}"
2132+
2133+
__print_lines 'start:'
21142134
arr=(a b c d)
21152135
__slice {arr} 1
21162136
echo-verbose -- "${arr[@]}"
21172137

2118-
__print_line
2138+
__print_lines 'start, negative:'
2139+
arr=(a b c d)
2140+
__slice {arr} -2
2141+
echo-verbose -- "${arr[@]}"
2142+
2143+
__print_lines 'start, finish:'
21192144
arr=(a b c d)
21202145
__slice {arr} 1 2
21212146
echo-verbose -- "${arr[@]}"
21222147

2123-
__print_line
2148+
__print_lines 'start, finish, negatives:'
2149+
arr=(a b c d)
2150+
__slice {arr} -3 -1
2151+
echo-verbose -- "${arr[@]}"
2152+
2153+
__print_lines 'start, finish, start:'
21242154
arr=(a b c d)
21252155
__slice {arr} 0 1 2
21262156
echo-verbose -- "${arr[@]}"
21272157

2128-
__print_line
2158+
__print_lines 'start, finish, start, negatives:'
2159+
arr=(a b c d)
2160+
__slice {arr} -4 -3 -1
2161+
echo-verbose -- "${arr[@]}"
2162+
2163+
__print_lines 'start, finish, start, finish:'
21292164
arr=(a b c d)
21302165
__slice {arr} 0 1 2 3
21312166
echo-verbose -- "${arr[@]}"
2167+
2168+
__print_lines 'start, finish, start, finish, negatives:'
2169+
arr=(a b c d)
2170+
__slice {arr} -4 -3 -1 -0
2171+
echo-verbose -- "${arr[@]}"
2172+
2173+
__print_lines 'target var with inputs:'
2174+
__slice {target} 0 1 2 3 -- a b c d
2175+
echo-verbose -- "${target[@]}"
2176+
2177+
__print_lines 'target var with inputs, append:'
2178+
__slice {target} --append -1 -- y x z
2179+
echo-verbose -- "${target[@]}"
2180+
2181+
__print_lines 'input var, target var:'
2182+
arr=(a b c d)
2183+
__slice {arr} {target} 0 1 2 3
2184+
echo-verbose -- "${arr[@]}"
2185+
echo-verbose -- "${target[@]}"
2186+
2187+
__print_lines 'input var, target var, append:'
2188+
__slice {arr} {target} --append -2
2189+
echo-verbose -- "${arr[@]}"
2190+
echo-verbose -- "${target[@]}"
2191+
2192+
__print_lines 'inputs, two target vars:'
2193+
__slice {arr} {target} -1 -- y x z
2194+
echo-verbose -- "${arr[@]}"
2195+
echo-verbose -- "${target[@]}"
2196+
2197+
__print_lines 'input var, target var, no indices, append:'
2198+
__slice {arr} {target} --append
2199+
echo-verbose -- "${arr[@]}"
2200+
echo-verbose -- "${target[@]}"
2201+
2202+
__print_lines 'inputs, two target vars:'
2203+
__slice --append {arr} {target} -2 -- x y z
2204+
echo-verbose -- "${arr[@]}"
2205+
echo-verbose -- "${target[@]}"
2206+
2207+
__print_lines 'input var, target var, no indices:'
2208+
__slice {arr} {target}
2209+
echo-verbose -- "${arr[@]}"
2210+
echo-verbose -- "${target[@]}"
21322211
}
21332212
slice_results="$(slice_tests)"
21342213
__end_test_block 'slice_results' "$(
21352214
cat <<-EOF
2215+
start, no indices, no-op:
2216+
[0] = [a]
2217+
[1] = [b]
2218+
[2] = [c]
2219+
[3] = [d]
2220+
start, empty indices, failure:
2221+
status: 22
2222+
start, out of bounds:
2223+
[ nothing provided ]
2224+
start, negative, out of bounds:
2225+
[0] = [a]
2226+
[1] = [b]
2227+
[2] = [c]
2228+
[3] = [d]
2229+
start:
21362230
[0] = [b]
21372231
[1] = [c]
21382232
[2] = [d]
2139-
2233+
start, negative:
2234+
[0] = [c]
2235+
[1] = [d]
2236+
start, finish:
21402237
[0] = [b]
2141-
2238+
start, finish, negatives:
2239+
[0] = [b]
2240+
[1] = [c]
2241+
start, finish, start:
21422242
[0] = [a]
21432243
[1] = [c]
21442244
[2] = [d]
2145-
2245+
start, finish, start, negatives:
2246+
[0] = [a]
2247+
[1] = [d]
2248+
start, finish, start, finish:
21462249
[0] = [a]
21472250
[1] = [c]
2251+
start, finish, start, finish, negatives:
2252+
[0] = [a]
2253+
[1] = [d]
2254+
target var with inputs:
2255+
[0] = [a]
2256+
[1] = [c]
2257+
target var with inputs, append:
2258+
[0] = [a]
2259+
[1] = [c]
2260+
[2] = [z]
2261+
input var, target var:
2262+
[0] = [a]
2263+
[1] = [b]
2264+
[2] = [c]
2265+
[3] = [d]
2266+
[0] = [a]
2267+
[1] = [c]
2268+
input var, target var, append:
2269+
[0] = [a]
2270+
[1] = [b]
2271+
[2] = [c]
2272+
[3] = [d]
2273+
[0] = [a]
2274+
[1] = [c]
2275+
[2] = [c]
2276+
[3] = [d]
2277+
inputs, two target vars:
2278+
[0] = [z]
2279+
[0] = [z]
2280+
input var, target var, no indices, append:
2281+
[0] = [z]
2282+
[0] = [z]
2283+
[1] = [z]
2284+
inputs, two target vars:
2285+
[0] = [z]
2286+
[1] = [y]
2287+
[2] = [z]
2288+
[0] = [z]
2289+
[1] = [z]
2290+
[2] = [y]
2291+
[3] = [z]
2292+
input var, target var, no indices:
2293+
[0] = [z]
2294+
[1] = [y]
2295+
[2] = [z]
2296+
[0] = [z]
2297+
[1] = [y]
2298+
[2] = [z]
21482299
EOF
21492300
)"
21502301
fi
@@ -2560,9 +2711,7 @@ if __begin_test_block --name='try throws_outer throws_inner' \
25602711
__print_lines "FAIL: [$?] SHOULD NOT SEE THIS (3/3)"
25612712
return 126
25622713
}
2563-
# __enable_debugging
25642714
__try {caught_test_status} -- throws_outer
2565-
# __disable_debugging
25662715
__end_test_block 'caught_test_status' 123
25672716
fi
25682717

@@ -2654,9 +2803,7 @@ if __begin_test_block --name='try throws_outer[subshell] throws_inner[subshell]'
26542803
__print_lines "FAIL: [$?] SHOULD NOT SEE THIS (3/3)"
26552804
return 126
26562805
)
2657-
# __enable_debugging
26582806
__try {caught_test_status} -- throws_outer
2659-
# __disable_debugging
26602807
__end_test_block 'caught_test_status' 123
26612808
fi
26622809

commands/echo-write

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ function echo_write_test() (
3636

3737
echo-style --newline --header1='bash read'
3838
function __read_whole {
39-
local whole=''
39+
local whole='' reply
4040
# bash can write/output null-bytes [\0], but [read] cannot read them
41-
while LC_ALL=C IFS= read -rd '' || [[ -n $REPLY ]]; do
42-
whole+="$REPLY"
43-
REPLY=''
41+
while LC_ALL=C IFS= read -rd '' reply || [[ -n $reply ]]; do
42+
whole+="$reply"
43+
reply=''
4444
done
4545
printf '%q' "$whole"
4646
}

commands/get-devices

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ function get_devices() (
406406
local values=() key value line return_values=() # color_index=0 color_open color_close
407407
for key in "${option_display[@]}"; do
408408
# if [[ $color_index -lt ${#open_colors[@]} ]]; then
409-
# color_open="${open_colors[$color_index]}"
410-
# color_close="${close_colors[$color_index]}"
409+
# color_open="${open_colors[color_index]}"
410+
# color_close="${close_colors[color_index]}"
411411
# color_index="$((color_index + 1))"
412412
# else
413413
# color_open="${open_colors[0]}"
@@ -445,7 +445,7 @@ function get_devices() (
445445
if [[ $index -lt 0 || $index -ge ${#return_lines[@]} ]]; then
446446
echo-error "Invalid index [$index] for [0, ${#return_lines[@]}]" || return
447447
fi
448-
local return_line="${return_lines[$index]}"
448+
local return_line="${return_lines[index]}"
449449
__print_lines "$return_line" || return
450450
}
451451
elif [[ ${#option_return[@]} -ne 0 ]]; then
@@ -632,11 +632,11 @@ function get_devices() (
632632
type_map['41504653-0000-11AA-AA11-00306543ECAC']='APFS Volume' # Partition Type
633633

634634
# cycle through each device
635-
local i device device id size label model type filesystem
636-
for i in "${!devices[@]}"; do
635+
local index device device id size label model type filesystem
636+
for index in "${!devices[@]}"; do
637637
# checks
638638
# __progress "$i" "$devices_count"
639-
device="${devices[$i]}"
639+
device="${devices[index]}"
640640
if is-whitespace -- "$device"; then
641641
continue
642642
fi
@@ -752,9 +752,9 @@ function get_devices() (
752752

753753
# cycle devices
754754
declare -A fields
755-
local i device node size model filesystem temp
756-
for i in "${!devices[@]}"; do
757-
device="${devices[$i]}"
755+
local index device node size model filesystem temp
756+
for index in "${!devices[@]}"; do
757+
device="${devices[index]}"
758758
IFS=$'\t' read -r node size model <<<"$device"
759759
if [[ -z $node ]]; then
760760
continue

commands/mount-helper

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ function mount_helper() (
12051205
}
12061206
function do_parse {
12071207
__print_line
1208-
__dump_vars \
1208+
__dump \
12091209
log_source option_actions option_remount option_source option_target \
12101210
mount_source mounted_source fstab_source open_source gocryptfs_source \
12111211
mount_type mounted_type fstab_type \

commands/secret

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ function secret_() (
537537
# fetch available vaults
538538
__split {tuples} --no-zero-length --invoke -- __op_grab || :
539539
if [[ ${#tuples[@]} -eq 0 ]]; then
540-
echo-error 'Failed to fetch vault:' --newline --="$(__dump_vars vault)" || return
540+
echo-error 'Failed to fetch vault:' --newline --="$(__dump vault)" || return
541541
return 1
542542
fi
543543

@@ -554,7 +554,7 @@ function secret_() (
554554
# fetch available items
555555
__split {tuples} --no-zero-length --invoke -- __op_grab "$vault" || :
556556
if [[ ${#tuples[@]} -eq 0 ]]; then
557-
echo-error 'Failed to fetch item:' --newline --="$(__dump_vars vault item)" || return
557+
echo-error 'Failed to fetch item:' --newline --="$(__dump vault item)" || return
558558
return 1
559559
fi
560560

@@ -577,7 +577,7 @@ function secret_() (
577577
# fetch available items
578578
__split {tuples} --no-zero-length --invoke -- __op_grab "$vault" "$item" || :
579579
if [[ ${#tuples[@]} -eq 0 ]]; then
580-
echo-error 'Failed to fetch field:' --newline --="$(__dump_vars vault item field)" || return
580+
echo-error 'Failed to fetch field:' --newline --="$(__dump vault item field)" || return
581581
return 1
582582
fi
583583

commands/symlink-helper

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,40 @@ function symlink_helper() (
111111
)")
112112
fi
113113

114-
# check
114+
# =====================================
115+
# Helpers
116+
115117
function __wrap {
116-
eval-helper --elevated="$option_elevated" --elevate="$option_elevate" --user="$option_user" --group="$option_group" --reason="$option_reason" -- "$@"
118+
eval-helper --elevated="$option_elevated" --elevate="$option_elevate" --user="$option_user" --group="$option_group" --reason="$option_reason" -- "$@" || return
117119
}
118-
# create the symlink
119-
# -F: replace symlink if directory if needed
120-
# -f: unlink symlink path if needed
121-
# -s: symbolic link
120+
121+
# create the symlink, shared between [symlink-helper] and [fs-relocate]
122+
# __ln <target> <symlink>
122123
if is-busybox -- ln; then
123124
function __ln {
124125
# alpine's ln doesn't support -F, however the above removals should make it unnecessary
125126
# https://github.com/bevry/dorothy/actions/runs/11323459946/job/31486170602#step:4:11
126-
__wrap ln -sf "$@"
127+
# -f: unlink symlink path if needed
128+
# -s: symbolic link
129+
__wrap ln -sf "$@" || return
127130
}
128131
else
129132
function __ln {
130-
__wrap ln -sfF "$@"
133+
# -F: replace symlink if directory if needed
134+
# -f: unlink symlink path if needed
135+
# -s: symbolic link
136+
__wrap ln -sfF "$@" || return
131137
}
132138
fi
133139

134140
# =====================================
135-
# Act
141+
# Action
136142

137143
# ensure validity of target path
138144
local index target target_resolved symlink symlink_resolved
139145
for index in "${!option_targets[@]}"; do
140-
target="${option_targets[$index]}"
141-
symlink="${option_symlinks[$index]}"
146+
target="${option_targets[index]}"
147+
symlink="${option_symlinks[index]}"
142148

143149
# resolve paths for now
144150
target="$(fs-path --absolute --elevated="$option_elevated" --elevate="$option_elevate" --user="$option_user" --group="$option_group" --reason="$option_reason" -- "$target")"
@@ -188,7 +194,7 @@ function symlink_helper() (
188194
if [[ -z $option_quiet || $option_quiet == 'no' ]]; then
189195
echo-style --stderr --note='💁‍♀️ Symlink already exists at ' --path="$symlink" --bold=' targetting ' --path="$target" ' ' --note='recreating...'
190196
fi
191-
# is a symlink but a different target, drop it and recreate
197+
# is a symlink but a different target, drop it and recreate to our intended target, ignoring target of the symlink
192198
__wrap rm -f -- "$symlink" >&2
193199
else
194200
# not a symlink, confirm with the user what to do

0 commit comments

Comments
 (0)