From: sev Date: Sun, 27 Aug 2023 02:51:17 +0000 (-0500) Subject: ykman-oath-reload: new + restored features X-Git-Url: https://git.sev.monster/~sev/dotfiles.git/commitdiff_plain/2f61cc7adbb880a728745ac1467cfc4196f793f8?ds=sidebyside ykman-oath-reload: new + restored features - pull in codes from local files, or from stdin if not present - look for binaries outside of *nix PATH for cygwin/msys2 envs - handle non-*nix line endings sent by ykman - ignore blank lines and comments (#) in input - add script comments --- diff --git a/bin/ykman-oath-reload b/bin/ykman-oath-reload index d33914a..d39e113 100755 --- a/bin/ykman-oath-reload +++ b/bin/ykman-oath-reload @@ -1,16 +1,37 @@ #!/bin/zsh +# find the right binary under mingw, cygwin +for x ({/mnt,}/c/Program\ Files{,\ \(x86\)}/Yubico/YubiKey\ Manager/ykman.exe ykman) { + if [[ -x $x ]] { + yk=$x + } elif [[ -v commands[$x] ]] { + yk=$commands[$x] + } +} read -s 'p?Password:'$'\n' typeset -a oath -echo OATH URIs: -while read -r x; do - [[ -z $x ]] && break - oath+=($x) -done -for x in $(ykman list --serials); do - ykman -d $x oath accounts list -Hp $p | while read -r y; do - ykman -d $x oath accounts delete -fp $p ${y/%$'\r'} - done - for y in "$oath[@]"; do - ykman -d $x oath accounts uri -fp $p $y - done -done +if [[ -f ./oath.txt ]] { + # attempt to pull from local file first + cat ./oath.txt | while {read -r x} { + echo $x + [[ -n $x && ! $x =~ '^\s*#' ]] && oath+=($x) + } +} else { + # read from stdin otherwise, empty line to stop + echo OATH URIs: + while {read -r x} { + [[ -z $x ]] && break + [[ ! $x =~ '^\s*#' ]] && oath+=($x) + } +} +for x ($($yk list --serials)) { + # NOTE: `%$'\r'` to strip carriage return from Windows binary output + x=${x/%$'\r'} + # WARN: delete existing codes, remove this if you don't need it + $yk -d $x oath accounts list -Hp $p | while {read -r y} { + $yk -d $x oath accounts delete -fp $p ${y/%$'\r'} + } + for y ("$oath[@]") { + $yk -d $x oath accounts uri -fp $p $y + echo Added $y + } +}