-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmktouch_utils.sh
More file actions
258 lines (229 loc) · 6.45 KB
/
Copy pathmktouch_utils.sh
File metadata and controls
258 lines (229 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
# mktouch - Create directories and files with preset support
# Presets are loaded from (in order of precedence):
# 1. .mktouchrc (project-local)
# 2. ~/.config/mktouch/presets (user)
# 3. /etc/mktouch/presets (system)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ── mktouch config paths (cascading hierarchy) ───────────────────────────────
MKT_PRESET_PATHS=(
"${SCRIPT_DIR}/data/mktouch/mktouch.conf"
"/etc/mktouch/presets"
"${HOME}/.config/mktouch/presets"
"${HOME}/.mktouchrc"
"$(pwd)/.mktouchrc"
)
# ── mktouch option metadata ───────────────────────────────────────────────────
# 0 1 2 3 4 5
_MKTOUCH_OPTS_SHORT=(-C -t -n -d -p -l -h)
_MKTOUCH_OPTS_LONG=(--prefix --tree --dry-run --debug --preset --list-presets --help)
_MKTOUCH_OPTS_ARG=("DIR" "" "" "" "NAME..." "" "")
_MKTOUCH_OPTS_DESC=(
"Prefix all paths (git -C style)"
"Show tree of created paths"
"Preview only"
"Print debug info"
"Use preset (explicit mode)"
"List available presets"
"Show this help"
)
# ── preset lookup ─────────────────────────────────────────────────────────────
# Usage: mktouch.preset.get <name>
# Returns: space-separated paths or empty if not found
mktouch.preset.get() {
local name="$1"
local preset_value=""
for config_file in "${MKT_PRESET_PATHS[@]}"; do
if [[ -f "$config_file" ]]; then
# Format: preset_name="path1 path2 path3..."
preset_value=$(grep -E "^${name}=" "$config_file" 2>/dev/null | cut -d'=' -f2- | sed 's/^"//;s/"$//')
if [[ -n "$preset_value" ]]; then
echo "$preset_value"
return 0
fi
fi
done
return 1
}
export -f mktouch.preset.get
# ── list all available presets ────────────────────────────────────────────────
mktouch.preset.list() {
local presets=()
local seen=()
for config_file in "${MKT_PRESET_PATHS[@]}"; do
# echo "Trying config file: ${config_file}"
if [[ -f "$config_file" ]]; then
while IFS= read -r line; do
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ -z "$line" ]] && continue
local pname="${line%%=*}"
[[ -z "$pname" ]] && continue
local already_seen=0
for s in "${seen[@]}"; do
[[ "$s" == "$pname" ]] && {
already_seen=1
break
}
done
((already_seen)) || {
seen+=("$pname")
presets+=("$pname")
}
done <"$config_file"
fi
done
[[ ${#presets[@]} -eq 0 ]] && {
echo "No presets found." >&2
return 1
}
printf '%s\n' "${presets[@]}" | sort
}
export -f mktouch.preset.list
# ── mktouch ───────────────────────────────────────────────────────────────────
mktouch() {
local deps=(tree)
for d in "${deps[@]}"; do
command -v "$d" >/dev/null 2>&1 || {
echo "Error: dependency missing: $d" >&2
# shellcheck disable=SC2317
return 127 2>/dev/null || exit 127
}
done
local paths=() created=()
local show_tree=0 dry_run=0 list_presets=0 debug=0 showhelp=0
local prefix=""
local use_presets=()
local usage_opts="" i
for ((i = 0; i < ${#_MKTOUCH_OPTS_SHORT[@]}; i++)); do
local sig="${_MKTOUCH_OPTS_SHORT[$i]}, ${_MKTOUCH_OPTS_LONG[$i]}${_MKTOUCH_OPTS_ARG[$i]:+ ${_MKTOUCH_OPTS_ARG[$i]}}"
local line
printf -v line '\t%-32s%s\n' "$sig" "${_MKTOUCH_OPTS_DESC[$i]}"
usage_opts+="$line"
done
local fn="${FUNCNAME[0]}"
local usage="Usage: $fn [OPTIONS] <path|preset> [<path|preset> ...]
Creates directories and files for given paths.
Options:
${usage_opts}
Presets:
Defined in .mktouchrc, ~/.config/mktouch/presets, or /etc/mktouch/presets.
$fn presetname (auto-detected)
$fn -p name1 name2 (explicit, space-separated list)"
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
showhelp=1
shift
;;
-C | --prefix)
prefix="$2"
shift 2
;;
-t | --tree)
show_tree=1
shift
;;
-n | --dry-run)
dry_run=1
show_tree=1
shift
;;
-d | --debug)
debug=1
shift
;;
-p | --preset)
shift
while [[ $# -gt 0 && "$1" != -* ]]; do
use_presets+=("$1")
shift
done
;;
-l | --list-presets)
list_presets=1
shift
;;
--)
shift
break
;;
-*)
echo "Error: unknown option $1" >&2
return 1
;;
*)
local preset_paths
preset_paths=$(mktouch.preset.get "$1" 2>/dev/null)
if [[ -n "$preset_paths" ]]; then
read -ra expanded <<<"$preset_paths"
paths+=("${expanded[@]}")
else
paths+=("$1")
fi
shift
;;
esac
done
((showhelp)) && {
printf '%s\n' "$usage"
return 0
}
((debug)) && {
echo "Debug: SCRIPT_DIR=${SCRIPT_DIR}"
echo "Debug: Config paths:"
for p in "${MKT_PRESET_PATHS[@]}"; do
local exists="missing"
[[ -f "$p" ]] && exists="exists"
echo " [$exists] $p"
done
echo "Debug: paths=${paths[*]}"
echo "Debug: prefix=${prefix}"
echo "Debug: use_presets=${use_presets[*]}"
}
((list_presets)) && {
mktouch.preset.list
return $?
}
for preset_name in "${use_presets[@]}"; do
local preset_paths
preset_paths=$(mktouch.preset.get "$preset_name" 2>/dev/null)
[[ -z "$preset_paths" ]] && {
echo "Error: preset not found: $preset_name" >&2
return 1
}
read -ra expanded <<<"$preset_paths"
paths+=("${expanded[@]}")
done
paths+=("$@")
[[ ${#paths[@]} -eq 0 ]] && {
echo "Error: no paths" >&2
return 1
}
[[ -n "$prefix" ]] && paths=("${paths[@]/#/$prefix/}")
((dry_run)) && {
printf '%s\n' "${paths[@]}" | tree --fromfile -F --noreport --dirsfirst
return 0
}
for p in "${paths[@]}"; do
if [[ "$p" == */ ]]; then
mkdir -p "$p"
else
mkdir -p "$(dirname -- "$p")"
touch "$p"
fi
created+=("$p")
done
((show_tree)) && {
printf '%s\n' "${created[@]}" | tree --fromfile -F --noreport --dirsfirst || true
}
}
export -f mktouch
_mktouch_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local all_opts=("${_MKTOUCH_OPTS_SHORT[@]}" "${_MKTOUCH_OPTS_LONG[@]}")
case "$cur" in
-*) mapfile -t COMPREPLY < <(compgen -W "${all_opts[*]}" -- "$cur") ;;
*) mapfile -t COMPREPLY < <(compgen -f -- "$cur") ;;
esac
}
complete -F _mktouch_complete mktouch