flake-update-20260505
 1#!/usr/bin/env bash
 2# yubikey-oath - Select and copy OATH code from YubiKey
 3
 4set -euo pipefail
 5
 6# List all OATH accounts from YubiKey
 7accounts=$(ykman oath accounts list 2>&1)
 8exit_code=$?
 9
10# Check if ykman failed
11if [ $exit_code -ne 0 ]; then
12    notify-send "YubiKey OATH" "Failed to connect to YubiKey. Make sure it's plugged in." --urgency=critical
13    exit 1
14fi
15
16# Check if there are any accounts
17if [ -z "$accounts" ]; then
18    notify-send "YubiKey OATH" "No OATH accounts found on YubiKey" --urgency=normal
19    exit 0
20fi
21
22# Let user select an account with fuzzel
23selected=$(echo "$accounts" | fuzzel --dmenu --prompt "OATH: " --lines 10 --width 40)
24
25if [ -z "$selected" ]; then
26    exit 0
27fi
28
29# Get the code for the selected account (extract just the digits)
30code=$(ykman oath accounts code --single "$selected" 2>&1 | grep -oE '[0-9]{6,8}')
31
32if [ -z "$code" ]; then
33    notify-send "YubiKey OATH" "Failed to get code for $selected" --urgency=critical
34    exit 1
35fi
36
37# Copy to clipboard
38echo -n "$code" | pbcopy
39
40# Show notification
41notify-send "YubiKey OATH" "Code for $selected copied to clipboard" --urgency=normal
42
43exit 0