main
 1#!/usr/bin/env bash
 2# List YubiKey OATH accounts for raffi script filter.
 3# Outputs Alfred Script Filter JSON format.
 4# The selected account name is copied/coded by the raffi action.
 5# Usage: yubikey-oath-raffi [query]
 6set -euo pipefail
 7
 8query="${1:-}"
 9
10if ! accounts=$(ykman oath accounts list 2>/dev/null); then
11    jq -n '{"items": [{"title": "YubiKey error", "subtitle": "Failed to connect to YubiKey — is it plugged in?", "valid": false}]}'
12    exit 0
13fi
14
15if [[ -z "$accounts" ]]; then
16    jq -n '{"items": [{"title": "No accounts", "subtitle": "No OATH accounts found on YubiKey", "valid": false}]}'
17    exit 0
18fi
19
20items="[]"
21while IFS= read -r account; do
22    [[ -z "$account" ]] && continue
23    if [[ -n "$query" ]] && ! echo "$account" | grep -qi "$query"; then
24        continue
25    fi
26    items=$(jq --arg a "$account" \
27        '. + [{"title": $a, "subtitle": "Copy OATH code for \($a)", "arg": $a}]' <<< "$items")
28done <<< "$accounts"
29
30jq -n --argjson items "$items" '{"items": $items}'