1#!/usr/bin/env bash
2
3function atfile.invoke() {
4 command="$1"
5 shift
6 args=("$@")
7
8 if [[ $_is_sourced == 0 ]] && [[ $ATFILE_DEVEL_NO_INVOKE != 1 ]]; then
9 atfile.say.debug "Invoking '$command'...\nā³ Arguments: ${args[*]}"
10
11 case "$command" in
12 "ai")
13 atfile.ai
14 ;;
15 "blob")
16 case "${args[0]}" in
17 "list"|"ls"|"l") atfile.invoke.blob_list "${args[1]}" ;;
18 "upload"|"u") atfile.invoke.blob_upload "${args[1]}" ;;
19 *) atfile.die.unknown_command "$(echo "$command ${args[0]}" | xargs)" ;;
20 esac
21 ;;
22 "bsky")
23 if [[ -z "${args[0]}" ]]; then
24 atfile.util.override_actor "$_username"
25 atfile.bsky_profile "$_username"
26 else
27 atfile.bsky_profile "${args[0]}"
28 fi
29 ;;
30 "cat")
31 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
32 if [[ -n "${args[1]}" ]]; then
33 atfile.util.override_actor "${args[1]}"
34 fi
35
36 atfile.invoke.print "${args[0]}"
37 ;;
38 "delete")
39 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
40 atfile.invoke.delete "${args[0]}"
41 ;;
42 "fetch")
43 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
44 if [[ -n "${args[1]}" ]]; then
45 atfile.util.override_actor "${args[1]}"
46 fi
47
48 atfile.invoke.download "${args[0]}"
49 ;;
50 "fetch-crypt")
51 atfile.util.check_prog_gpg
52 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
53 if [[ -n "${args[1]}" ]]; then
54 atfile.util.override_actor "${args[1]}"
55 fi
56
57 atfile.invoke.download "${args[0]}" 1
58 ;;
59 "handle")
60 uri="${args[0]}"
61 protocol="$(atfile.util.get_uri_segment "$uri" protocol)"
62
63 if [[ $protocol == "https" ]]; then
64 http_uri="$uri"
65 uri="$(atfile.util.map_http_to_at "$http_uri")"
66
67 atfile.say.debug "Mapping '$http_uri'..."
68
69 if [[ -z "$uri" ]]; then
70 atfile.die "Unable to map '$http_uri' to at:// URI"
71 else
72 protocol="$(atfile.util.get_uri_segment "$uri" protocol)"
73 fi
74 fi
75
76 atfile.say.debug "Handling protocol '$protocol://'..."
77
78 case $protocol in
79 "at") atfile.invoke.handle_aturi "$uri" ;;
80 "atfile") atfile.invoke.handle_atfile "$uri" "${args[1]}" ;;
81 esac
82 ;;
83 "help")
84 atfile.help
85 ;;
86 "info")
87 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
88 if [[ -n "${args[1]}" ]]; then
89 atfile.util.override_actor "${args[1]}"
90 fi
91
92 atfile.invoke.get "${args[0]}"
93 ;;
94 "install")
95 # TODO: Disable when installed (how?), similar to `release`
96 atfile.install "${args[0]}" "${args[1]}" "${args[2]}"
97 ;;
98 "list")
99 if [[ "${args[0]}" == *.* || "${args[0]}" == did:* ]]; then
100 # NOTE: User has entered <actor> in the wrong place, so we'll fix it
101 # for them
102 # BUG: Keys with periods in them can't be used as a cursor
103
104 atfile.util.override_actor "${args[0]}"
105
106 atfile.invoke.list "${args[1]}"
107 else
108 if [[ -n "${args[1]}" ]]; then
109 atfile.util.override_actor "${args[1]}"
110 fi
111 atfile.invoke.list "${args[0]}"
112 fi
113 ;;
114 "lock")
115 atfile.invoke.lock "${args[0]}" 1
116 ;;
117 "now")
118 atfile.now "${args[0]}"
119 ;;
120 "record")
121 # NOTE: Performs no validation (apart from JSON)! Here be dragons
122 case "${args[0]}" in
123 "add"|"create"|"c") atfile.record "create" "${args[1]}" "${args[2]}" ;;
124 "get"|"g") atfile.record "get" "${args[1]}" ;;
125 "ls"|"list"|"l") atfile.record_list "${args[1]}" ;;
126 "put"|"update"|"u") atfile.record "update" "${args[1]}" "${args[2]}" ;;
127 "rc"|"recreate"|"r") atfile.record "recreate" "${args[1]}" "${args[2]}" ;;
128 "rm"|"delete"|"d") atfile.record "delete" "${args[1]}" ;;
129 *) atfile.die.unknown_command "$(echo "$command ${args[0]}" | xargs)" ;;
130 esac
131 ;;
132 "release")
133 if [[ $ATFILE_DEVEL == 1 ]]; then
134 atfile.release
135 else
136 atfile.die.unknown_command "$command"
137 fi
138 ;;
139 "resolve")
140 atfile.resolve "${args[0]}"
141 ;;
142 "something-broke")
143 atfile.something_broke
144 ;;
145 "stream")
146 atfile.stream "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}"
147 ;;
148 "token")
149 atfile.invoke.token
150 ;;
151 "toggle-mime")
152 atfile.invoke.toggle_desktop
153 ;;
154 "upload")
155 atfile.util.check_prog_optional_metadata
156 [[ -z "${args[0]}" ]] && atfile.die "<file> not set"
157 atfile.invoke.upload "${args[0]}" "" "${args[1]}"
158 ;;
159 "upload-crypt")
160 atfile.util.check_prog_optional_metadata
161 atfile.util.check_prog_gpg
162 [[ -z "${args[0]}" ]] && atfile.die "<file> not set"
163 [[ -z "${args[1]}" ]] && atfile.die "<recipient> not set"
164 atfile.invoke.upload "${args[0]}" "${args[1]}" "${args[2]}"
165 ;;
166 "unlock")
167 atfile.invoke.lock "${args[0]}" 0
168 ;;
169 "update")
170 atfile.update install
171 ;;
172 "url")
173 [[ -z "${args[0]}" ]] && atfile.die "<key> not set"
174 if [[ -n "${args[1]}" ]]; then
175 atfile.util.override_actor "${args[1]}"
176 fi
177
178 atfile.invoke.get_url "${args[0]}"
179 ;;
180 "version")
181 echo -e "$_version"
182 atfile.cache.del "update-check"
183 atfile.update check-only
184 ;;
185 *)
186 atfile.die.unknown_command "$command"
187 ;;
188 esac
189
190 atfile.update check-only
191 fi
192}