Skip to content

Commit ade03ed

Browse files
committed
feat: Output verbosity control #4
closes #4
1 parent dd5904a commit ade03ed

2 files changed

Lines changed: 73 additions & 45 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ sudo-touchid [options]
6969
[-d, --disable] # Remove TouchID from sudo config
7070
[--with-reattach] # Include pam_reattach.so for tmux/screen support
7171
[--migrate] # Migrate from legacy configuration
72+
[--verbose] # Show detailed output
73+
[-q, --quiet] # Show minimal output (errors only)
7274
```
7375

7476
if not installed, can be used via [`curl`][curl] <sup>bundled with macOS</sup>

sudo-touchid.sh

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ VERSION=0.5
44
readable_name='[TouchID for sudo]'
55
executable_name='sudo-touchid'
66

7+
# Verbosity control
8+
VERBOSE=false
9+
QUIET=false
10+
711
# PAM configuration
812
PAM_TOUCHID='auth sufficient pam_tid.so'
913
PAM_REATTACH_PATH='/opt/homebrew/lib/pam/pam_reattach.so'
@@ -25,6 +29,9 @@ usage() {
2529
--with-reattach Include pam_reattach.so for GUI session reattachment
2630
--migrate Migrate from legacy configuration to new system
2731
32+
--verbose Show detailed output
33+
-q, --quiet Show minimal output (errors only)
34+
2835
-v, --version Output version
2936
-h, --help This message.
3037
@@ -53,6 +60,19 @@ wait_for_user() {
5360

5461
# Utility functions
5562

63+
# Output functions for verbosity control
64+
verbose_echo() {
65+
[[ "$VERBOSE" == true ]] && echo "$@"
66+
}
67+
68+
status_echo() {
69+
[[ "$QUIET" != true ]] && echo "$@"
70+
}
71+
72+
error_echo() {
73+
echo "$@" >&2
74+
}
75+
5676
detect_os_version() {
5777
sw_vers -productVersion | cut -d. -f1
5878
}
@@ -80,14 +100,14 @@ install_file() {
80100
temp_file=$(mktemp 2>/dev/null)
81101

82102
if [[ -z "$temp_file" ]]; then
83-
echo "Error: Unable to create temporary file. Check /tmp directory permissions and available space."
84-
echo "Please ensure /tmp exists, is writable, and has sufficient space."
103+
error_echo "Error: Unable to create temporary file. Check /tmp directory permissions and available space."
104+
error_echo "Please ensure /tmp exists, is writable, and has sufficient space."
85105
return 1
86106
fi
87107

88108
if ! echo "$content" > "$temp_file" 2>/dev/null; then
89-
echo "Error: Unable to write to temporary file. Check /tmp directory permissions and available space."
90-
echo "Please ensure /tmp exists, is writable, and has sufficient space."
109+
error_echo "Error: Unable to write to temporary file. Check /tmp directory permissions and available space."
110+
error_echo "Please ensure /tmp exists, is writable, and has sufficient space."
91111
rm -f "$temp_file" 2>/dev/null
92112
return 1
93113
fi
@@ -106,15 +126,15 @@ check_legacy_configuration() {
106126
}
107127

108128
migrate_legacy_configuration() {
109-
echo "Migrating from legacy TouchID configuration..."
129+
status_echo "Migrating from legacy TouchID configuration..."
110130

111131
local major_version
112132
major_version=$(detect_os_version)
113133

114134
# Remove legacy PAM file if it exists
115135
if [[ -f "$LEGACY_PAM_FILE" ]]; then
116136
sudo rm -f "$LEGACY_PAM_FILE"
117-
echo "Removed legacy PAM file: $LEGACY_PAM_FILE"
137+
verbose_echo "Removed legacy PAM file: $LEGACY_PAM_FILE"
118138
fi
119139

120140

@@ -123,42 +143,42 @@ migrate_legacy_configuration() {
123143
sudo cp "$SUDO_PATH" "$SUDO_PATH.bak"
124144
sudo sed -i '.bak' '/pam_tid\.so/d' "$SUDO_PATH"
125145
sudo sed -i '.bak' '/pam_reattach\.so/d' "$SUDO_PATH"
126-
echo "Removed TouchID configuration from $SUDO_PATH (backup saved as $SUDO_PATH.bak)"
146+
verbose_echo "Removed TouchID configuration from $SUDO_PATH (backup saved as $SUDO_PATH.bak)"
127147
fi
128148

129-
echo "Legacy configuration removed successfully."
149+
status_echo "Legacy configuration removed successfully."
130150
}
131151

132152
sudo_touchid_pamlocal_install() {
133153
local include_reattach="$1"
134154

135-
echo "Installing TouchID configuration for macOS 14+"
155+
verbose_echo "Installing TouchID configuration for macOS 14+"
136156

137157
# Create PAM configuration for sudo_local
138158
local pam_content
139159
pam_content=$(create_pam_content "$include_reattach")
140160

141161
if ! install_file "$pam_content" "$SUDO_LOCAL_PATH" "644"; then
142-
echo "Error: Failed to create $SUDO_LOCAL_PATH"
162+
error_echo "Error: Failed to create $SUDO_LOCAL_PATH"
143163
return 1
144164
fi
145165

146-
echo "Created $SUDO_LOCAL_PATH"
147-
echo
148-
echo "$readable_name enabled successfully for macOS 14+."
149-
echo "Note: If TouchID for sudo stops working, you can disable it with: $executable_name --disable"
166+
verbose_echo "Created $SUDO_LOCAL_PATH"
167+
status_echo
168+
status_echo "$readable_name enabled successfully for macOS 14+."
169+
verbose_echo "Note: If TouchID for sudo stops working, you can disable it with: $executable_name --disable"
150170

151171
return 0
152172
}
153173

154174
sudo_touchid_legacy_install() {
155175
local include_reattach="$1"
156176

157-
echo "Installing TouchID configuration for macOS ≤13"
177+
verbose_echo "Installing TouchID configuration for macOS ≤13"
158178

159179
# Check if already configured
160180
if grep -q "pam_tid.so" "$SUDO_PATH" 2>/dev/null; then
161-
echo "$readable_name seems to be enabled already"
181+
status_echo "$readable_name seems to be enabled already"
162182
return 0
163183
fi
164184

@@ -174,9 +194,9 @@ sudo_touchid_legacy_install() {
174194
sudo sed -E -i ".bak" "1s/^(#.*)$/\1\\${nl}$touch_pam_line/" "$SUDO_PATH"
175195
fi
176196

177-
echo "Created a backup file at $SUDO_PATH.bak"
178-
echo
179-
echo "$readable_name enabled successfully."
197+
verbose_echo "Created a backup file at $SUDO_PATH.bak"
198+
status_echo
199+
status_echo "$readable_name enabled successfully."
180200

181201
return 0
182202
}
@@ -192,14 +212,14 @@ sudo_touchid_install() {
192212

193213
# Check for migration from legacy configuration
194214
if check_legacy_configuration; then
195-
echo "Legacy TouchID configuration detected. Migrating to new secure method..."
215+
status_echo "Legacy TouchID configuration detected. Migrating to new secure method..."
196216
if migrate_legacy_configuration; then
197217
# After migration, verify legacy configuration is removed
198218
if check_legacy_configuration; then
199-
echo "Error: Legacy configuration still detected after migration. Aborting to prevent infinite loop."
219+
error_echo "Error: Legacy configuration still detected after migration. Aborting to prevent infinite loop."
200220
return 1
201221
else
202-
echo "Migration completed. Re-running installation with new method..."
222+
verbose_echo "Migration completed. Re-running installation with new method..."
203223
sudo_touchid_install "$include_reattach"
204224
return $?
205225
fi
@@ -211,40 +231,40 @@ sudo_touchid_install() {
211231
# Check if already installed
212232
if [[ "$major_version" -ge 14 && -f "$SUDO_LOCAL_PATH" ]]; then
213233
if [[ "$include_reattach" == "true" ]] && ! check_reattach_available; then
214-
echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
215-
echo "Install it with: brew install pam-reattach"
234+
error_echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
235+
error_echo "Install it with: brew install pam-reattach"
216236
return 1
217237
fi
218238

219239
# Check if user wants pam_reattach but it's not installed
220240
if [[ "$include_reattach" == "true" ]] && check_reattach_available && ! grep -q "pam_reattach.so" "$SUDO_LOCAL_PATH" 2>/dev/null; then
221-
echo "$readable_name is installed but without pam_reattach support."
222-
echo "Please run --disable first, then reinstall with --with-reattach."
241+
error_echo "$readable_name is installed but without pam_reattach support."
242+
error_echo "Please run --disable first, then reinstall with --with-reattach."
223243
return 1
224244
fi
225-
echo "$readable_name appears to be already installed."
245+
status_echo "$readable_name appears to be already installed."
226246
return 0
227247
elif [[ "$major_version" -lt 14 ]] && grep -q "pam_tid.so" "$SUDO_PATH" 2>/dev/null; then
228248
if [[ "$include_reattach" == "true" ]] && ! check_reattach_available; then
229-
echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
230-
echo "Install it with: brew install pam-reattach"
249+
error_echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
250+
error_echo "Install it with: brew install pam-reattach"
231251
return 1
232252
fi
233253

234254
# Check if user wants pam_reattach but it's not installed
235255
if [[ "$include_reattach" == "true" ]] && check_reattach_available && ! grep -q "pam_reattach.so" "$SUDO_PATH" 2>/dev/null; then
236-
echo "$readable_name is installed but without pam_reattach support."
237-
echo "Please run --disable first, then reinstall with --with-reattach."
256+
error_echo "$readable_name is installed but without pam_reattach support."
257+
error_echo "Please run --disable first, then reinstall with --with-reattach."
238258
return 1
239259
fi
240-
echo "$readable_name appears to be already installed."
260+
status_echo "$readable_name appears to be already installed."
241261
return 0
242262
fi
243263

244264
# Check for pam_reattach if requested
245265
if [[ "$include_reattach" == "true" ]] && ! check_reattach_available; then
246-
echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
247-
echo "Install it with: brew install pam-reattach"
266+
error_echo "Error: pam_reattach.so not found at $PAM_REATTACH_PATH"
267+
error_echo "Install it with: brew install pam-reattach"
248268
return 1
249269
fi
250270

@@ -267,23 +287,23 @@ sudo_touchid_disable() {
267287
fi
268288

269289
if [[ $has_config -eq 0 ]]; then
270-
echo "$readable_name seems to be already disabled"
290+
status_echo "$readable_name seems to be already disabled"
271291
return 0
272292
fi
273293

274294
# Show what will be removed
275-
echo "The following TouchID configurations will be removed:"
276-
echo
295+
verbose_echo "The following TouchID configurations will be removed:"
296+
verbose_echo
277297

278298
if [[ -f "$SUDO_LOCAL_PATH" ]]; then
279-
echo " - $SUDO_LOCAL_PATH"
299+
verbose_echo " - $SUDO_LOCAL_PATH"
280300
fi
281301

282302
if [[ -f "$LEGACY_PAM_FILE" ]]; then
283-
echo " - $LEGACY_PAM_FILE"
303+
verbose_echo " - $LEGACY_PAM_FILE"
284304
fi
285305

286-
if grep -q "pam_tid.so" "$SUDO_PATH" 2>/dev/null; then
306+
if [[ "$VERBOSE" == "true" ]] && grep -q "pam_tid.so" "$SUDO_PATH" 2>/dev/null; then
287307
echo " - TouchID line from $SUDO_PATH"
288308
echo
289309
echo "Your $SUDO_PATH will look like this after removal:"
@@ -300,14 +320,14 @@ sudo_touchid_disable() {
300320
# Remove sudo_local file (macOS 14+)
301321
if [[ -f "$SUDO_LOCAL_PATH" ]]; then
302322
sudo rm -f "$SUDO_LOCAL_PATH"
303-
echo "Removed $SUDO_LOCAL_PATH"
323+
verbose_echo "Removed $SUDO_LOCAL_PATH"
304324
files_removed=$((files_removed + 1))
305325
fi
306326

307327
# Remove legacy PAM file
308328
if [[ -f "$LEGACY_PAM_FILE" ]]; then
309329
sudo rm -f "$LEGACY_PAM_FILE"
310-
echo "Removed $LEGACY_PAM_FILE"
330+
verbose_echo "Removed $LEGACY_PAM_FILE"
311331
files_removed=$((files_removed + 1))
312332
fi
313333

@@ -316,12 +336,12 @@ sudo_touchid_disable() {
316336
sudo cp "$SUDO_PATH" "$SUDO_PATH.bak"
317337
sudo sed -i '.bak' '/pam_tid\.so/d' "$SUDO_PATH"
318338
sudo sed -i '.bak' '/pam_reattach\.so/d' "$SUDO_PATH"
319-
echo "Removed TouchID configuration from $SUDO_PATH (backup saved as $SUDO_PATH.bak)"
339+
verbose_echo "Removed TouchID configuration from $SUDO_PATH (backup saved as $SUDO_PATH.bak)"
320340
files_removed=$((files_removed + 1))
321341
fi
322342

323-
echo
324-
echo "$readable_name has been disabled."
343+
status_echo
344+
status_echo "$readable_name has been disabled."
325345
}
326346

327347

@@ -344,6 +364,12 @@ sudo_touchid() {
344364
--migrate)
345365
action="migrate"
346366
;;
367+
--verbose)
368+
VERBOSE=true
369+
;;
370+
-q | --quiet)
371+
QUIET=true
372+
;;
347373
-h | --help)
348374
usage
349375
return 0

0 commit comments

Comments
 (0)