-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmktouch_utils.sh
More file actions
210 lines (183 loc) · 5.23 KB
/
mktouch_utils.sh
File metadata and controls
210 lines (183 loc) · 5.23 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
#!/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="$(dirname "${BASH_SOURCE[0]}")"
# ---- config paths (cascading hierarchy) ----
MKT_PRESET_PATHS=(
"${SCRIPT_DIR}/data/mktouch/mktouch.conf"
"/etc/mktouch/presets"
"${HOME}/.config/mktouch/presets"
"${HOME}/.mktouchrc"
)
# ---- 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
# Read preset from config file
# 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
}
# ---- list all available presets ----
mktouch.preset.list() {
local presets=()
local seen=()
for config_file in "${MKT_PRESET_PATHS[@]}"; do
if [[ -f "$config_file" ]]; then
while IFS= read -r line; do
# Skip comments and empty lines
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ -z "$line" ]] && continue
# Extract preset name
local pname="${line%%=*}"
[[ -z "$pname" ]] && continue
# Check if already seen (higher precedence wins)
local already_seen=false
for s in "${seen[@]}"; do
if [[ "$s" == "$pname" ]]; then
already_seen=true
break
fi
done
if [[ "$already_seen" == false ]]; then
seen+=("$pname")
presets+=("$pname")
fi
done < "$config_file"
fi
done
if [[ ${#presets[@]} -eq 0 ]]; then
echo "No presets found." >&2
return 1
fi
printf '%s\n' "${presets[@]}" | sort
}
# ---- main mktouch function ----
mktouch() {
local deps=(tree)
for d in "${deps[@]}"; do
command -v "$d" >/dev/null 2>&1 || {
echo "Error: dependency missing: $d" >&2
return 127 2>/dev/null || exit 127
}
done
local paths=() created=()
local show_tree=false dry_run=false list_presets=false debug=false
local prefix=""
local use_presets=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
cat <<-EOF
Usage: ${FUNCNAME[0]} [OPTIONS] [--path|-p] <path|preset> [<path|preset> ...]
Creates directories and files for given paths.
Options:
-C, --prefix <dir> Prefix all paths (git -C style)
-t, --tree Show tree of created paths
-n, --dry-run Preview only
-d, --debug Print debug info
-p, --preset <name> Use preset (explicit mode)
-l, --list-presets List available presets
-h, --help Show this help
Presets:
Presets are defined in .mktouchrc, ~/.config/mktouch/presets,
or /etc/mktouch/presets. Call with:
mktouch presetname (auto-detected)
mktouch -p presetname (explicit)
EOF
return 0 ;;
-C|--prefix)
shift; [[ $# -eq 0 ]] && { echo "Error: --prefix needs arg" >&2; return 1; }
prefix="$1"; shift ;;
-t|--tree) show_tree=true; shift ;;
-n|--dry-run) dry_run=true; show_tree=true; shift ;;
-d|--debug) debug=true; shift ;;
-p|--preset)
shift; [[ $# -eq 0 ]] && { echo "Error: --preset needs arg" >&2; return 1; }
use_presets+=("$1"); shift ;;
-l|--list-presets) list_presets=true; shift ;;
--) shift; break ;;
-*) echo "Error: unknown option $1" >&2; return 1 ;;
*)
# Check if it's a preset name
local preset_paths
preset_paths=$(mktouch.preset.get "$1" 2>/dev/null)
if [[ -n "$preset_paths" ]]; then
# It's a preset - expand it
read -ra expanded <<< "$preset_paths"
paths+=("${expanded[@]}")
else
# It's a regular path
paths+=("$1")
fi
shift ;;
esac
done
# Print debug info
if [[ "$debug" == true ]]; then
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[*]}"
fi
# Handle --list-presets
if $list_presets; then
mktouch.preset.list
return $?
fi
# Handle explicit --preset(s)
for preset_name in "${use_presets[@]}"; do
local preset_paths
preset_paths=$(mktouch.preset.get "$preset_name" 2>/dev/null)
if [[ -z "$preset_paths" ]]; then
echo "Error: preset not found: $preset_name" >&2
return 1
fi
read -ra expanded <<< "$preset_paths"
paths+=("${expanded[@]}")
done
# Add remaining args
paths+=("$@")
[[ ${#paths[@]} -eq 0 ]] && { echo "Error: no paths" >&2; return 1; }
if [[ -n "$prefix" ]]; then
paths=("${paths[@]/#/$prefix/}")
fi
if $dry_run; then
printf '%s\n' "${paths[@]}" \
| tree --fromfile -F --noreport --dirsfirst
return 0
fi
for p in "${paths[@]}"; do
if [[ "$p" == */ ]]; then
mkdir -p "$p"
else
mkdir -p "$(dirname -- "$p")"
touch "$p"
fi
created+=("$p")
done
if $show_tree; then
printf '%s\n' "${created[@]}" \
| tree --fromfile -F --noreport --dirsfirst || true
fi
}
export -f mktouch