formist-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- formist_cli/__init__.py +1 -0
- formist_cli/__main__.py +3 -0
- formist_cli/airtable.py +238 -0
- formist_cli/alerts.py +207 -0
- formist_cli/api.py +1027 -0
- formist_cli/chat.py +618 -0
- formist_cli/chats.py +517 -0
- formist_cli/cli.py +1367 -0
- formist_cli/cli_support.py +221 -0
- formist_cli/connectors.py +358 -0
- formist_cli/context.py +164 -0
- formist_cli/diff.py +943 -0
- formist_cli/doctor.py +128 -0
- formist_cli/export.py +427 -0
- formist_cli/files.py +670 -0
- formist_cli/importer.py +425 -0
- formist_cli/kb.py +185 -0
- formist_cli/login.py +615 -0
- formist_cli/logs.py +781 -0
- formist_cli/meetings.py +200 -0
- formist_cli/messages.py +414 -0
- formist_cli/participants.py +402 -0
- formist_cli/prefs.py +328 -0
- formist_cli/privileges.py +375 -0
- formist_cli/projects.py +127 -0
- formist_cli/resolve.py +443 -0
- formist_cli/settings.py +199 -0
- formist_cli/skill_library.py +246 -0
- formist_cli/skills.py +308 -0
- formist_cli/space_plan.py +411 -0
- formist_cli/spaces.py +693 -0
- formist_cli/targets.py +153 -0
- formist_cli/textnorm.py +26 -0
- formist_cli/tokens.py +339 -0
- formist_cli/update.py +433 -0
- formist_cli/uploads.py +928 -0
- formist_cli/usage_tokens.py +152 -0
- formist_cli/users.py +384 -0
- formist_cli/whatsapp.py +451 -0
- formist_cli-0.1.0.dist-info/METADATA +176 -0
- formist_cli-0.1.0.dist-info/RECORD +44 -0
- formist_cli-0.1.0.dist-info/WHEEL +4 -0
- formist_cli-0.1.0.dist-info/entry_points.txt +2 -0
- formist_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
formist_cli/context.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Resolve which deployment, space and token a command uses, in one place.
|
|
2
|
+
|
|
3
|
+
Every command that talks to the API needs the same three answers (env,
|
|
4
|
+
target, token) and should report where each came from. Centralising
|
|
5
|
+
the resolution here keeps the logic, and the local "no token yet"
|
|
6
|
+
refusal, from drifting between call sites.
|
|
7
|
+
|
|
8
|
+
``api.admin_get`` keeps its own "no stored session" guard rather than
|
|
9
|
+
relying on this module: it also covers a caller that imports
|
|
10
|
+
``formist_cli.api`` directly, without going through the CLI resolver
|
|
11
|
+
at all.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import sys
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from datetime import datetime, timedelta, timezone
|
|
19
|
+
|
|
20
|
+
from formist_cli import prefs, targets, tokens
|
|
21
|
+
from formist_cli.targets import Target
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class Context:
|
|
26
|
+
"""A resolved destination plus the credential for it."""
|
|
27
|
+
|
|
28
|
+
target: Target
|
|
29
|
+
space: str | None
|
|
30
|
+
token: str | None
|
|
31
|
+
#: Where the space came from (flag, remembered, unset). Already
|
|
32
|
+
#: rendered into the banner; kept so a caller can say it again
|
|
33
|
+
#: without re-deriving it.
|
|
34
|
+
space_source: str
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def resolve(
|
|
38
|
+
args,
|
|
39
|
+
*,
|
|
40
|
+
for_write: bool = False,
|
|
41
|
+
need_space: bool = False,
|
|
42
|
+
need_token: bool = False,
|
|
43
|
+
) -> Context:
|
|
44
|
+
"""Resolve the target, space and token, printing provenance to stderr.
|
|
45
|
+
|
|
46
|
+
The banner is printed on every command, including reads, since the
|
|
47
|
+
target alone tells the operator little; where each value came
|
|
48
|
+
from, and how stale a remembered one is, is what catches a wrong
|
|
49
|
+
assumption. It goes to stderr so a piped command stays pipeable.
|
|
50
|
+
|
|
51
|
+
``need_token`` refuses locally instead of sending a request and
|
|
52
|
+
reading back a 401: it costs no socket, works when the server is
|
|
53
|
+
unreachable, and reports the same problem sooner. It defaults to
|
|
54
|
+
off because ``fm health`` is public and must work before a token
|
|
55
|
+
exists.
|
|
56
|
+
"""
|
|
57
|
+
eff = prefs.effective(
|
|
58
|
+
env_flag=getattr(args, "host", None),
|
|
59
|
+
space_flag=getattr(args, "space", None),
|
|
60
|
+
for_write=for_write,
|
|
61
|
+
)
|
|
62
|
+
print(eff.banner(), file=sys.stderr)
|
|
63
|
+
|
|
64
|
+
if need_space and not eff.space:
|
|
65
|
+
raise prefs.PrefsError(
|
|
66
|
+
"no space given and none remembered.\n"
|
|
67
|
+
"Run `fm use --space <uuid>`, or pass `--space <uuid>` on this command.\n"
|
|
68
|
+
"Find the id with `fm spaces --host evane`."
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
token, origin = tokens.load_for(eff.target.name)
|
|
72
|
+
if origin == tokens.ENV_TOKEN_ORIGIN:
|
|
73
|
+
# Used exactly as supplied: no metadata file exists to read an
|
|
74
|
+
# expiry from, and nothing here may write one, since refreshing
|
|
75
|
+
# or storing an env-sourced credential would defeat the point
|
|
76
|
+
# of using one.
|
|
77
|
+
print(
|
|
78
|
+
f" token: environment ({tokens.ENV_TOKEN_VAR}), used as-is, not "
|
|
79
|
+
"stored, not refreshed",
|
|
80
|
+
file=sys.stderr,
|
|
81
|
+
)
|
|
82
|
+
else:
|
|
83
|
+
token = _refresh_if_stale(eff.target.name, token)
|
|
84
|
+
if origin and origin != eff.target.name:
|
|
85
|
+
# Borrowing a credential from another region's file is said
|
|
86
|
+
# out loud rather than left as invisible state.
|
|
87
|
+
print(
|
|
88
|
+
f" using the {origin} token ({eff.target.name} has none stored), "
|
|
89
|
+
f"both prod regions share one signing secret",
|
|
90
|
+
file=sys.stderr,
|
|
91
|
+
)
|
|
92
|
+
if need_token and not token:
|
|
93
|
+
raise tokens.TokenError(
|
|
94
|
+
f"no stored session for {eff.target.name}. "
|
|
95
|
+
f"Run `fm login --host {eff.target.name}`"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
return Context(
|
|
99
|
+
target=eff.target,
|
|
100
|
+
space=eff.space,
|
|
101
|
+
token=token,
|
|
102
|
+
space_source=getattr(eff, "space_source", "unknown"),
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
#: Refresh this many seconds before the access token actually expires,
|
|
107
|
+
#: so a token that is valid when the request is built does not expire
|
|
108
|
+
#: by the time the server reads it.
|
|
109
|
+
_REFRESH_SKEW_SECONDS = 120
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _refresh_if_stale(env: str, token: str | None) -> str | None:
|
|
113
|
+
"""Trade the refresh token for a fresh access token if the stored one is
|
|
114
|
+
about to expire. Returns the token to use now.
|
|
115
|
+
|
|
116
|
+
Refreshing happens here, before any request is built, rather than
|
|
117
|
+
as a retry-on-401 in the API layer, so the credential is either
|
|
118
|
+
fresh or absent by the time a socket is opened.
|
|
119
|
+
|
|
120
|
+
A failed refresh is not fatal: it leaves the old token in place
|
|
121
|
+
and prints why, since raising here would break ``fm health``,
|
|
122
|
+
which is public and must work when nothing else does.
|
|
123
|
+
"""
|
|
124
|
+
if not token:
|
|
125
|
+
return token
|
|
126
|
+
meta = tokens.load_meta(env)
|
|
127
|
+
expires_at = meta.get("expires_at")
|
|
128
|
+
if not expires_at:
|
|
129
|
+
return token
|
|
130
|
+
try:
|
|
131
|
+
deadline = datetime.fromisoformat(str(expires_at))
|
|
132
|
+
except ValueError:
|
|
133
|
+
return token
|
|
134
|
+
if deadline.tzinfo is None:
|
|
135
|
+
deadline = deadline.replace(tzinfo=timezone.utc)
|
|
136
|
+
if deadline - timedelta(seconds=_REFRESH_SKEW_SECONDS) > datetime.now(timezone.utc):
|
|
137
|
+
return token
|
|
138
|
+
|
|
139
|
+
refresh_token = tokens.load_refresh(env)
|
|
140
|
+
client_id = meta.get("client_id")
|
|
141
|
+
if not refresh_token or not client_id:
|
|
142
|
+
print(
|
|
143
|
+
f" the {env} credential has expired and there is nothing to refresh it "
|
|
144
|
+
f"with. Run `fm login --host {env}`",
|
|
145
|
+
file=sys.stderr,
|
|
146
|
+
)
|
|
147
|
+
return token
|
|
148
|
+
|
|
149
|
+
from formist_cli import login as login_mod
|
|
150
|
+
|
|
151
|
+
try:
|
|
152
|
+
target = login_mod.auth_target(targets.resolve(env))
|
|
153
|
+
payload = login_mod.refresh(
|
|
154
|
+
target, login_mod.discover(target),
|
|
155
|
+
client_id=str(client_id), refresh_token=refresh_token,
|
|
156
|
+
)
|
|
157
|
+
except Exception as exc: # noqa: BLE001 - a failed refresh must not be fatal here
|
|
158
|
+
print(f" could not refresh the {env} credential: {exc}", file=sys.stderr)
|
|
159
|
+
return token
|
|
160
|
+
|
|
161
|
+
stored = login_mod.store_payload(env, payload, client_id=str(client_id),
|
|
162
|
+
client_name=meta.get("client_name"))
|
|
163
|
+
print(f" refreshed the {env} credential (valid again for ~1h)", file=sys.stderr)
|
|
164
|
+
return stored
|