Nushell function for creating a tangled string via the goat CLI
tangled.nu edited
58 lines 1.8 kB view raw
1# Strings CLI 2# 3# Examples: 4# $ string main.go 5# $ echo "Hey tanglers!" | string --title "tanglers.txt" --description "A file for all the tanglers" 6export def string [ 7 file?: path # File to create string from, otherwise stdin 8 --title (-t): string # Title of the string (default is filename when file is provided, otherwise random uuid) 9 --description (-d): string # Description of the string 10]: [ 11 path -> string 12 nothing -> string 13] { 14 mut _content = $in 15 mut _title = (random uuid) 16 if ($file != null) { 17 $_content = ($file | open --raw | decode utf-8) 18 $_title = ($file | path basename) 19 } 20 if ($title != null) { 21 $_title = $title 22 } 23 mut _desc = "" 24 if ($description != null) { 25 $_desc = $description 26 } 27 28 if ($_content == null or $_content == "") { 29 error make {msg: "Cannot create a string with no content"} 30 } 31 32 # https://tangled.sh/@tangled.sh/core/blob/83002d8f7c812138d664b7d1d24bdd7c9b471795/lexicons/string/string.json 33 let payload = ({ 34 "$type": "sh.tangled.string", 35 "contents": $_content, 36 "filename": $_title, 37 "createdAt": (date now | format date "%Y-%m-%dT%H:%M:%SZ"), 38 "description": $_desc 39 } | to json) 40 41 let tmp = (mktemp --tmpdir sh.tangled.string.XXX) 42 43 $payload | save --force $tmp 44 45 mut out = "" 46 try { 47 let resp = (^goat record create --no-validate $tmp) 48 # at://did:plc:35kdk2ntcs626zs6cm62i7ih/sh.tangled.string/3lxuash7vvc2f bafyreifv7c6il2zsa67oa6umutb52izbgrnmz336wzbgti2we3tpw3bcj4 49 # 0 1 2 3 4 5 6 7 8 50 let plc = ($resp | split words | get 3) 51 let rkey = ($resp | split words | get 7) 52 $out = $"https://tangled.sh/strings/did:plc:($plc)/($rkey)" 53 } 54 55 rm $tmp 56 57 $out 58}