dotfiles/bin/gpg-key-to-datamatrix

48 lines
1.2 KiB
Text
Raw Permalink Normal View History

#! /usr/bin/env nix-shell
2024-08-02 11:21:47 -05:00
#! nix-shell -p typst dmtx-utils nushell -i nu
2024-08-05 12:14:24 -05:00
def append-if [cond: bool, item: any] {
if $cond { $in | append $item } else { $in }
}
2024-08-02 11:21:47 -05:00
def get-chunked-key [keyid?: string] {
2024-08-06 12:19:17 -05:00
let flags = ["--export-secret-key", "--export-options", "export-minimal"]
| append-if ($keyid != null) $keyid
2024-08-02 11:21:47 -05:00
let key = (gpg ...$flags)
let length = ($key | bytes length)
2024-08-06 12:19:17 -05:00
# 1555 is the maximum length in bytes
2024-08-02 11:21:47 -05:00
let n = ($length / (($length / 1555)| math ceil)) | math ceil
2024-08-06 12:19:17 -05:00
2024-08-02 11:21:47 -05:00
0..$n..$length
2024-08-06 12:19:17 -05:00
| each {|i| $key | bytes at $i..<($i + $n)}
2024-08-02 11:21:47 -05:00
}
def generate-typst-doc [tmp: string, length: int] {
0..<$length
2024-08-06 12:19:17 -05:00
| each { |i|
let path = $tmp
| path join $"dmtx-($i).svg"
| path relative-to $env.PWD
['#image("', $path, '")'] | str join
}
| str join "\n"
2024-08-02 11:21:47 -05:00
}
2024-08-05 12:14:24 -05:00
def main [keyid?: string, --output(-o) = "dmtxdata.pdf"] {
2024-08-02 11:21:47 -05:00
let tmpdir = mktemp -d -p . -t tmp.key-2-matrix-XXX
let byte_chunks = get-chunked-key $keyid
2024-08-06 12:19:17 -05:00
$byte_chunks
| enumerate
| each {|it|
$it.item
| dmtxwrite -e 8 -o ($tmpdir | path join $"dmtx-($it.index).svg")
}
2024-08-02 11:21:47 -05:00
2024-08-06 12:19:17 -05:00
generate-typst-doc $tmpdir ($byte_chunks | length)
| typst compile - $output
2024-08-02 11:21:47 -05:00
rm -r $tmpdir
}