The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.7 KiB

  1. #!/usr/bin/env bash
  2. if [[ $# -eq 0 || "$1" == @(--help|-h) ]]; then
  3. printf 'Usage: ia-metadata ITEM [CHANGE...]\n' >&2
  4. printf 'Reads or modifies metadata of an item or file in an item\n' >&2
  5. printf 'Each CHANGE consists of two or more arguments: an operation, a field, and potentially variable further arguments for the operation.\n' >&2
  6. printf 'The following operations are supported:\n' >&2
  7. printf ' append FIELD VALUE appends a value to a list field\n' >&2
  8. printf ' remove FIELD VALUE removes the first appearance of the value in a list field\n' >&2
  9. printf ' set FIELD VALUE sets the field to the value; any existing value is overwritten\n' >&2
  10. printf ' unset FIELD removes the field\n' >&2
  11. exit 1
  12. fi
  13. if (($# == 1)); then
  14. # Read
  15. curl --silent --show-error --user-agent 'little-things +https://gitea.arpa.li/JustAnotherArchivist/little-things' "https://archive.org/metadata/$1"
  16. st="$?"
  17. # Emit an LF terminator if stdout is a TTY
  18. [[ -t 1 ]] && printf '\n'
  19. exit "${st}"
  20. fi
  21. # Write
  22. #TODO Add 'test' operations to ensure the local state matches what's in the item: https://archive.org/developers/md-write-adv.html#use-the-test-operator
  23. target="$1"
  24. shift
  25. args=()
  26. while (($#)); do
  27. case "$1" in
  28. append)
  29. (($# >= 3)) || { printf 'Error: expected two arguments on append\n' >&2; exit 1; }
  30. args+=(op add path "/$2/-" value "$3")
  31. shift 3
  32. ;;
  33. remove)
  34. printf 'Error: remove is currently unsupported due to a an apparent bug on the Internet Archive side\n' >&2
  35. exit 1
  36. (($# >= 3)) || { printf 'Error: expected two arguments on remove\n' >&2; exit 1; }
  37. # IA extension to JSON Patch
  38. args+=(op remove-first path "/$2" value "$3")
  39. shift 3
  40. ;;
  41. set)
  42. (($# >= 3)) || { printf 'Error: expected two arguments on set\n' >&2; exit 1; }
  43. args+=(op add path "/$2" value "$3")
  44. shift 3
  45. ;;
  46. unset)
  47. (($# >= 2)) || { printf 'Error: expected an argument on unset\n' >&2; exit 1; }
  48. args+=(op remove path "/$2")
  49. shift 2
  50. ;;
  51. *)
  52. printf 'Error: invalid operation %q\n' "$1" >&2
  53. exit 1
  54. ;;
  55. esac
  56. done
  57. patch="$(jq --null-input --compact-output '$ARGS.positional | . as $arr | [range(0; length/2) * 2 | $arr[. : . + 2]] | reduce .[] as [$key, $value] ([]; if $key == "op" then . += [{($key): $value}] elif .[-1][$key] then if .[-1][$key] | type == "array" then .[-1][$key] += [$value] else .[-1][$key] = [.[-1][$key], $value] end else .[-1] += {($key): $value} end)' --args "${args[@]}")"
  58. "$(cd "$(dirname "$0")"; pwd -P)/curl-ia" header --silent --show-error --user-agent 'little-things +https://gitea.arpa.li/JustAnotherArchivist/little-things' --form '-target=metadata' --form "-patch=${patch}" "https://archive.org/metadata/${target}"
  59. st="$?"
  60. [[ -t 1 ]] && printf '\n'
  61. exit "${st}"