ms-cli 0.2.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.
- ad_cli/__init__.py +6 -0
- ad_cli/__main__.py +6 -0
- ad_cli/auth.py +351 -0
- ad_cli/cli.py +278 -0
- ad_cli/discovery.py +243 -0
- ad_cli/dynamic.py +816 -0
- ad_cli/errors.py +45 -0
- ad_cli/http.py +424 -0
- ad_cli/invoke.py +652 -0
- ad_cli/main.py +703 -0
- cann_cli/__init__.py +6 -0
- cann_cli/__main__.py +6 -0
- cann_cli/auth.py +351 -0
- cann_cli/cli.py +278 -0
- cann_cli/discovery.py +243 -0
- cann_cli/dynamic.py +816 -0
- cann_cli/errors.py +45 -0
- cann_cli/http.py +424 -0
- cann_cli/invoke.py +652 -0
- cann_cli/main.py +703 -0
- ms_cli/__init__.py +6 -0
- ms_cli/__main__.py +6 -0
- ms_cli/auth.py +351 -0
- ms_cli/cli.py +278 -0
- ms_cli/discovery.py +243 -0
- ms_cli/dynamic.py +816 -0
- ms_cli/errors.py +45 -0
- ms_cli/http.py +424 -0
- ms_cli/invoke.py +652 -0
- ms_cli/main.py +703 -0
- ms_cli-0.2.0.dist-info/METADATA +444 -0
- ms_cli-0.2.0.dist-info/RECORD +67 -0
- ms_cli-0.2.0.dist-info/WHEEL +5 -0
- ms_cli-0.2.0.dist-info/entry_points.txt +2 -0
- ms_cli-0.2.0.dist-info/licenses/LICENSE +18 -0
- ms_cli-0.2.0.dist-info/top_level.txt +5 -0
- oed_cli/__init__.py +6 -0
- oed_cli/__main__.py +6 -0
- oed_cli/auth.py +1629 -0
- oed_cli/cli.py +297 -0
- oed_cli/defaults.toml +44 -0
- oed_cli/discovery.py +243 -0
- oed_cli/dynamic.py +816 -0
- oed_cli/errors.py +45 -0
- oed_cli/http.py +423 -0
- oed_cli/invoke.py +651 -0
- oed_cli/main.py +782 -0
- ogd_cli/__init__.py +6 -0
- ogd_cli/__main__.py +6 -0
- ogd_cli/auth.py +351 -0
- ogd_cli/cli.py +278 -0
- ogd_cli/discovery.py +243 -0
- ogd_cli/dynamic.py +816 -0
- ogd_cli/errors.py +45 -0
- ogd_cli/http.py +424 -0
- ogd_cli/invoke.py +652 -0
- ogd_cli/main.py +703 -0
- oud_cli/__init__.py +6 -0
- oud_cli/__main__.py +6 -0
- oud_cli/auth.py +351 -0
- oud_cli/cli.py +278 -0
- oud_cli/discovery.py +243 -0
- oud_cli/dynamic.py +816 -0
- oud_cli/errors.py +45 -0
- oud_cli/http.py +424 -0
- oud_cli/invoke.py +652 -0
- oud_cli/main.py +703 -0
oed_cli/main.py
ADDED
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
"""Entry point for the ``oed`` console script.
|
|
2
|
+
|
|
3
|
+
This module implements the top-level dispatch:
|
|
4
|
+
|
|
5
|
+
* Reserved sub-commands (``info``, ``services``, ``schema``, ``cache``,
|
|
6
|
+
``completion``, ``--version``, ``--help`` …) go to the click tree in
|
|
7
|
+
:mod:`oed_cli.cli`.
|
|
8
|
+
* Anything else is treated as ``oed <service> [<method>] [flags]`` and
|
|
9
|
+
resolved dynamically via :mod:`oed_cli.dynamic`.
|
|
10
|
+
|
|
11
|
+
Dynamic dispatch is what makes ``oed`` interesting — a new service shows
|
|
12
|
+
up in the discovery feed, and ``oed <new-service> -- ...`` works on the
|
|
13
|
+
next TTL refresh without any code change.
|
|
14
|
+
|
|
15
|
+
Per-parameter flag surface
|
|
16
|
+
--------------------------
|
|
17
|
+
|
|
18
|
+
For each declared ``query`` / ``path`` parameter on an operation,
|
|
19
|
+
``oed`` exposes a dedicated ``--<kebab-case>`` flag — derived from the
|
|
20
|
+
spec name (``cveId`` → ``--cve-id``, ``page_num`` → ``--page-num``).
|
|
21
|
+
``oed <service> <operation> --help`` enumerates them all.
|
|
22
|
+
|
|
23
|
+
The legacy ``--params '{...}'`` JSON form is preserved as an escape
|
|
24
|
+
hatch: useful for rarely-used parameters and for piping bulk data.
|
|
25
|
+
Per-parameter flags and ``--params`` can be mixed; per-parameter flags
|
|
26
|
+
override matching keys from ``--params``.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import json
|
|
32
|
+
import sys
|
|
33
|
+
from collections.abc import Sequence
|
|
34
|
+
from typing import Any
|
|
35
|
+
|
|
36
|
+
import click
|
|
37
|
+
|
|
38
|
+
from .auth import clear_token, read_token, store_token, token_backend, token_info, token_path
|
|
39
|
+
from .cli import cli as click_cli
|
|
40
|
+
from .dynamic import (
|
|
41
|
+
coerce_flag_value,
|
|
42
|
+
coerce_param_types,
|
|
43
|
+
collect_operations,
|
|
44
|
+
fetch_service_spec,
|
|
45
|
+
operations_table,
|
|
46
|
+
param_flag_index,
|
|
47
|
+
parse_json_arg,
|
|
48
|
+
resolve_operation,
|
|
49
|
+
resolve_runtime_gateway,
|
|
50
|
+
resolve_service_by_name,
|
|
51
|
+
)
|
|
52
|
+
from .errors import NotFoundError, OedError
|
|
53
|
+
from .invoke import (
|
|
54
|
+
GATEWAY_MANAGED_PARAMS,
|
|
55
|
+
call_operation,
|
|
56
|
+
describe_operation_help,
|
|
57
|
+
describe_service,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _resolve_auth() -> tuple[str | None, str | None]:
|
|
62
|
+
"""Resolve the runtime token + cookie from env (priority) or auth.json.
|
|
63
|
+
|
|
64
|
+
Mirrors :func:`oed_cli.http._resolve_user_agent` precedence: explicit env
|
|
65
|
+
vars (``OED_TOKEN`` / ``OED_COOKIE``) win over the stored auth file.
|
|
66
|
+
Returns ``(None, None)`` when no credential is available.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
from .auth import auth_headers_from_storage
|
|
70
|
+
|
|
71
|
+
headers = auth_headers_from_storage()
|
|
72
|
+
token = headers.get("Authorization")
|
|
73
|
+
if token and token.startswith("Bearer "):
|
|
74
|
+
token = token[len("Bearer "):]
|
|
75
|
+
cookie = headers.get("Cookie")
|
|
76
|
+
return token, cookie
|
|
77
|
+
|
|
78
|
+
# Tokens that always go through the click sub-tree, regardless of whether
|
|
79
|
+
# they happen to match a discovered service. Recognised single tokens:
|
|
80
|
+
RESERVED_FIRST_TOKENS: frozenset[str] = frozenset(
|
|
81
|
+
{
|
|
82
|
+
"info",
|
|
83
|
+
"services",
|
|
84
|
+
"schema",
|
|
85
|
+
"cache",
|
|
86
|
+
"auth",
|
|
87
|
+
"completion",
|
|
88
|
+
"help",
|
|
89
|
+
"login",
|
|
90
|
+
# the user typed only the binary with no args
|
|
91
|
+
"",
|
|
92
|
+
# passthrough flags
|
|
93
|
+
}
|
|
94
|
+
)
|
|
95
|
+
# Click passes these verbatim when they're the first argv item
|
|
96
|
+
LEADING_FLAGS: frozenset[str] = frozenset({"-h", "--help", "-V", "--version"})
|
|
97
|
+
|
|
98
|
+
# Services that are always allowed, regardless of the per-user oneid
|
|
99
|
+
# allow-list. These are public / non-oneid-gated services the CLI must keep
|
|
100
|
+
# usable without a login: ``ag`` authenticates with its own PAT (AtomGit),
|
|
101
|
+
# the rest are open community services. Matched by ``ServiceMeta.service_name``
|
|
102
|
+
# (case-sensitive). See issue: allowlist 两来源(登录来源 + 默认放行).
|
|
103
|
+
DEFAULT_ALLOWLIST: frozenset[str] = frozenset(
|
|
104
|
+
{
|
|
105
|
+
"search",
|
|
106
|
+
"cve",
|
|
107
|
+
"easysoftware",
|
|
108
|
+
"mailman",
|
|
109
|
+
"ag",
|
|
110
|
+
"CI",
|
|
111
|
+
"meeting"
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
# Built-in control flags handled by the dispatcher itself. Everything
|
|
116
|
+
# else is treated as a candidate per-parameter flag, validated later
|
|
117
|
+
# against the resolved operation's declared parameters.
|
|
118
|
+
_VALUE_FLAGS: frozenset[str] = frozenset({"params", "json", "path", "user-agent"})
|
|
119
|
+
_BOOL_FLAGS: frozenset[str] = frozenset({"dry-run"})
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _looks_like_reserved(argv: Sequence[str]) -> bool:
|
|
123
|
+
if not argv:
|
|
124
|
+
return True
|
|
125
|
+
head = argv[0]
|
|
126
|
+
if head in LEADING_FLAGS:
|
|
127
|
+
return True
|
|
128
|
+
if head.startswith("-"):
|
|
129
|
+
return True
|
|
130
|
+
return head in RESERVED_FIRST_TOKENS
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _split_dispatch_argv(rest: list[str]) -> tuple[dict[str, str], set[str], bool, list[str]]:
|
|
134
|
+
"""First-pass split of argv (after ``service_name``) into:
|
|
135
|
+
|
|
136
|
+
- ``raw_flags``: ``--key value`` (or ``--key=value``) pairs
|
|
137
|
+
- ``bool_flags``: ``--key`` with no value (e.g. ``--dry-run``)
|
|
138
|
+
- ``help_requested``: ``--help`` / ``-h`` was seen
|
|
139
|
+
- ``positional``: non-flag arguments
|
|
140
|
+
|
|
141
|
+
Unknown ``--<key>`` flags are kept in ``raw_flags`` so they can be
|
|
142
|
+
matched against the resolved operation's declared parameters before
|
|
143
|
+
being rejected.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
raw_flags: dict[str, str] = {}
|
|
147
|
+
bool_flags: set[str] = set()
|
|
148
|
+
help_requested = False
|
|
149
|
+
positional: list[str] = []
|
|
150
|
+
|
|
151
|
+
while rest:
|
|
152
|
+
tok = rest.pop(0)
|
|
153
|
+
if tok == "--":
|
|
154
|
+
positional.extend(rest)
|
|
155
|
+
break
|
|
156
|
+
if tok in ("-h", "--help"):
|
|
157
|
+
help_requested = True
|
|
158
|
+
continue
|
|
159
|
+
if tok.startswith("--"):
|
|
160
|
+
body = tok[2:]
|
|
161
|
+
if "=" in body:
|
|
162
|
+
k, v = body.split("=", 1)
|
|
163
|
+
if k in _BOOL_FLAGS:
|
|
164
|
+
bool_flags.add(k)
|
|
165
|
+
else:
|
|
166
|
+
raw_flags[k] = v
|
|
167
|
+
continue
|
|
168
|
+
if body in _VALUE_FLAGS:
|
|
169
|
+
if not rest:
|
|
170
|
+
raise OedError(f"--{body} requires a value", kind="missing_flag_value")
|
|
171
|
+
raw_flags[body] = rest.pop(0)
|
|
172
|
+
continue
|
|
173
|
+
if body in _BOOL_FLAGS:
|
|
174
|
+
bool_flags.add(body)
|
|
175
|
+
continue
|
|
176
|
+
# Candidate per-parameter flag: consume the next token as its
|
|
177
|
+
# value unless that token is itself a flag.
|
|
178
|
+
if rest and not rest[0].startswith("-"):
|
|
179
|
+
raw_flags[body] = rest.pop(0)
|
|
180
|
+
else:
|
|
181
|
+
bool_flags.add(body)
|
|
182
|
+
continue
|
|
183
|
+
if tok.startswith("-"):
|
|
184
|
+
raise OedError(f"unknown short flag: {tok}", kind="unknown_flag")
|
|
185
|
+
positional.append(tok)
|
|
186
|
+
|
|
187
|
+
return raw_flags, bool_flags, help_requested, positional
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _merge_params(
|
|
191
|
+
op,
|
|
192
|
+
raw_flags: dict[str, str],
|
|
193
|
+
) -> tuple[dict, dict | None, int]:
|
|
194
|
+
"""Build the final params + body from ``--params`` JSON and per-param flags.
|
|
195
|
+
|
|
196
|
+
Returns ``(params, body, exit_code)`` — exit_code is non-zero when a
|
|
197
|
+
JSON parse error short-circuits the call.
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
params: dict = {}
|
|
201
|
+
if "params" in raw_flags:
|
|
202
|
+
try:
|
|
203
|
+
parsed = parse_json_arg(raw_flags["params"], flag="params") or {}
|
|
204
|
+
except OedError as exc:
|
|
205
|
+
return {}, None, exc.code
|
|
206
|
+
if not isinstance(parsed, dict):
|
|
207
|
+
return {}, None, 1
|
|
208
|
+
params.update(parsed)
|
|
209
|
+
|
|
210
|
+
body = None
|
|
211
|
+
if "json" in raw_flags:
|
|
212
|
+
try:
|
|
213
|
+
body = parse_json_arg(raw_flags["json"], flag="json")
|
|
214
|
+
except OedError as exc:
|
|
215
|
+
return params, None, exc.code
|
|
216
|
+
|
|
217
|
+
# Per-parameter flags override matching keys from --params.
|
|
218
|
+
declared: dict[str, dict] = param_flag_index(op)
|
|
219
|
+
declared_names = {
|
|
220
|
+
p["name"] for p in op.parameters if p.get("in") in {"query", "path"}
|
|
221
|
+
}
|
|
222
|
+
for flag_key, value in raw_flags.items():
|
|
223
|
+
if flag_key in _VALUE_FLAGS or flag_key in _BOOL_FLAGS:
|
|
224
|
+
continue
|
|
225
|
+
if flag_key not in declared:
|
|
226
|
+
if flag_key.lower() in {p.lower() for p in GATEWAY_MANAGED_PARAMS}:
|
|
227
|
+
raise OedError(
|
|
228
|
+
f"--{flag_key} is injected by the gateway for forum calls",
|
|
229
|
+
kind="gateway_managed_param",
|
|
230
|
+
hint=(
|
|
231
|
+
"Do not pass Api-Key / Api-Username — oed auto-fills "
|
|
232
|
+
"the placeholder headers on every forum call and the "
|
|
233
|
+
"gateway converts them."
|
|
234
|
+
),
|
|
235
|
+
)
|
|
236
|
+
raise OedError(
|
|
237
|
+
f"unknown flag: --{flag_key}",
|
|
238
|
+
kind="unknown_flag",
|
|
239
|
+
hint=(
|
|
240
|
+
f"Declared parameters for {op.operation_id}: "
|
|
241
|
+
f"{sorted(declared_names) or '(none)'}. "
|
|
242
|
+
"Use `--params '{...}'` for arbitrary JSON."
|
|
243
|
+
),
|
|
244
|
+
)
|
|
245
|
+
param_def = declared[flag_key]
|
|
246
|
+
params[param_def["name"]] = coerce_flag_value(param_def, value)
|
|
247
|
+
|
|
248
|
+
# Final pass for any --params-derived values that need string→int coercion.
|
|
249
|
+
params = coerce_param_types(op, params)
|
|
250
|
+
return params, body, 0
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _check_allowlist(service_name: str) -> None:
|
|
254
|
+
"""Local per-user allow-list gate (no HTTP).
|
|
255
|
+
|
|
256
|
+
Two sources of "allowed" (union, either suffices):
|
|
257
|
+
|
|
258
|
+
1. :data:`DEFAULT_ALLOWLIST` — services the CLI always permits regardless
|
|
259
|
+
of login state (public / non-oneid-gated services, incl. ``ag`` which
|
|
260
|
+
authenticates with its own PAT via ``oed ag login``).
|
|
261
|
+
2. The per-user ``allowlist`` field on ``auth.json`` (populated by
|
|
262
|
+
:func:`oed_cli.auth._fetch_user_allowlist` at login success).
|
|
263
|
+
|
|
264
|
+
Raises :class:`UserError` when the service is in neither source. Fail-open
|
|
265
|
+
when no per-user allow-list is available (not logged in, or missing/empty
|
|
266
|
+
field) — never blocks unconfigured / legacy state. The default list still
|
|
267
|
+
applies in every case, so a default-listed service is never blocked.
|
|
268
|
+
"""
|
|
269
|
+
|
|
270
|
+
from .auth import load_auth
|
|
271
|
+
from .errors import UserError
|
|
272
|
+
|
|
273
|
+
if service_name in DEFAULT_ALLOWLIST:
|
|
274
|
+
return # always allowed (public service / PAT-authed `ag`)
|
|
275
|
+
|
|
276
|
+
stored = load_auth()
|
|
277
|
+
if not stored:
|
|
278
|
+
return # not logged in → don't block (reserved cmds would also miss this)
|
|
279
|
+
allowlist = stored.get("allowlist")
|
|
280
|
+
if not isinstance(allowlist, list) or not allowlist:
|
|
281
|
+
# None (never fetched), [] (user cleared), or non-list (malformed) →
|
|
282
|
+
# all treated as fail-open. The "no allowlist configured" hint in
|
|
283
|
+
# `oed auth status` makes this discoverable.
|
|
284
|
+
return
|
|
285
|
+
if service_name in allowlist:
|
|
286
|
+
return
|
|
287
|
+
raise UserError(
|
|
288
|
+
f"service '{service_name}' is not in your CLI allow-list",
|
|
289
|
+
kind="service_blocked_by_allowlist",
|
|
290
|
+
hint=(
|
|
291
|
+
f"Allowed services: {sorted(allowlist)}. "
|
|
292
|
+
"Update your allow-list in oneid, then re-run `oed auth login` "
|
|
293
|
+
"to refresh the local copy."
|
|
294
|
+
),
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
_AG_LOGIN_INSTRUCTIONS = (
|
|
299
|
+
"AtomGit login\n"
|
|
300
|
+
"=============\n"
|
|
301
|
+
"1. Open https://atomgit.com and sign in.\n"
|
|
302
|
+
"2. Create a personal access token under Settings "
|
|
303
|
+
"(https://atomgit.com/setting/token-classic/create)\n"
|
|
304
|
+
"3. Paste the token below. It is stored encrypted for this Windows/Linux user "
|
|
305
|
+
"and is never echoed back.\n"
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _ag_login_help() -> int:
|
|
310
|
+
click.echo(
|
|
311
|
+
json.dumps(
|
|
312
|
+
{
|
|
313
|
+
"ok": True,
|
|
314
|
+
"help_for": "ag login",
|
|
315
|
+
"usage": "oed ag login [--token <pat>] [--no-verify] [--status]",
|
|
316
|
+
"flags": {
|
|
317
|
+
"--token": "provide the AtomGit personal access token non-interactively",
|
|
318
|
+
"--no-verify": "skip validating the token against AtomGit before storing",
|
|
319
|
+
"--status": "report whether a token is configured, without prompting",
|
|
320
|
+
},
|
|
321
|
+
"examples": [
|
|
322
|
+
"oed ag login",
|
|
323
|
+
"oed ag login --token <pat> --no-verify",
|
|
324
|
+
"oed ag login --status",
|
|
325
|
+
"oed ag logout",
|
|
326
|
+
],
|
|
327
|
+
},
|
|
328
|
+
ensure_ascii=False,
|
|
329
|
+
indent=2,
|
|
330
|
+
)
|
|
331
|
+
)
|
|
332
|
+
return 0
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def _ag_login(raw_flags: dict[str, str], bool_flags: set[str], help_requested: bool) -> int:
|
|
336
|
+
"""Configure the AtomGit access token used by ``oed ag`` requests."""
|
|
337
|
+
|
|
338
|
+
if help_requested:
|
|
339
|
+
return _ag_login_help()
|
|
340
|
+
|
|
341
|
+
if "status" in bool_flags:
|
|
342
|
+
return _ag_status()
|
|
343
|
+
|
|
344
|
+
token = raw_flags.pop("token", None)
|
|
345
|
+
if token == "":
|
|
346
|
+
click.echo(
|
|
347
|
+
json.dumps(
|
|
348
|
+
{
|
|
349
|
+
"ok": False,
|
|
350
|
+
"code": 1,
|
|
351
|
+
"error": "missing_flag_value",
|
|
352
|
+
"message": "--token requires a non-empty value",
|
|
353
|
+
},
|
|
354
|
+
ensure_ascii=False,
|
|
355
|
+
),
|
|
356
|
+
err=True,
|
|
357
|
+
)
|
|
358
|
+
return 1
|
|
359
|
+
|
|
360
|
+
if token is None:
|
|
361
|
+
click.echo(_AG_LOGIN_INSTRUCTIONS, err=True)
|
|
362
|
+
try:
|
|
363
|
+
token = click.prompt("AtomGit personal access token", hide_input=True, err=True)
|
|
364
|
+
except click.Abort:
|
|
365
|
+
click.echo(
|
|
366
|
+
json.dumps(
|
|
367
|
+
{"ok": False, "code": 1, "error": "aborted", "message": "login cancelled"},
|
|
368
|
+
ensure_ascii=False,
|
|
369
|
+
),
|
|
370
|
+
err=True,
|
|
371
|
+
)
|
|
372
|
+
return 1
|
|
373
|
+
|
|
374
|
+
token = token.strip()
|
|
375
|
+
if not token:
|
|
376
|
+
click.echo(
|
|
377
|
+
json.dumps(
|
|
378
|
+
{
|
|
379
|
+
"ok": False,
|
|
380
|
+
"code": 1,
|
|
381
|
+
"error": "missing_token",
|
|
382
|
+
"message": "no token provided",
|
|
383
|
+
},
|
|
384
|
+
ensure_ascii=False,
|
|
385
|
+
),
|
|
386
|
+
err=True,
|
|
387
|
+
)
|
|
388
|
+
return 1
|
|
389
|
+
|
|
390
|
+
if "no-verify" not in bool_flags:
|
|
391
|
+
verify_code = _verify_ag_token(token)
|
|
392
|
+
if verify_code != 0:
|
|
393
|
+
return verify_code
|
|
394
|
+
|
|
395
|
+
path = store_token(token)
|
|
396
|
+
backend = token_backend("ag")
|
|
397
|
+
if backend == "keyring":
|
|
398
|
+
# The token landed in the OS credential manager (Keychain / DPAPI /
|
|
399
|
+
# SecretService); the plaintext fallback file was deleted on the
|
|
400
|
+
# successful keyring write. Pointing the user at that (now absent)
|
|
401
|
+
# file path would be misleading — say where it actually is.
|
|
402
|
+
click.echo(
|
|
403
|
+
json.dumps(
|
|
404
|
+
{
|
|
405
|
+
"ok": True,
|
|
406
|
+
"service": "ag",
|
|
407
|
+
"configured": True,
|
|
408
|
+
"backend": "keyring",
|
|
409
|
+
"note": "token saved to the OS credential manager; "
|
|
410
|
+
"no local file is written, and the token is never echoed back",
|
|
411
|
+
},
|
|
412
|
+
ensure_ascii=False,
|
|
413
|
+
indent=2,
|
|
414
|
+
)
|
|
415
|
+
)
|
|
416
|
+
else:
|
|
417
|
+
# Plaintext fallback (headless / CI / Docker without an OS keystore):
|
|
418
|
+
# the 0600 file at `path` is the real store, so its path is accurate.
|
|
419
|
+
click.echo(
|
|
420
|
+
json.dumps(
|
|
421
|
+
{
|
|
422
|
+
"ok": True,
|
|
423
|
+
"service": "ag",
|
|
424
|
+
"configured": True,
|
|
425
|
+
"backend": "plaintext",
|
|
426
|
+
"token_path": str(path),
|
|
427
|
+
"note": "token stored locally (0600 file); it is never echoed back",
|
|
428
|
+
},
|
|
429
|
+
ensure_ascii=False,
|
|
430
|
+
indent=2,
|
|
431
|
+
)
|
|
432
|
+
)
|
|
433
|
+
return 0
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def _verify_ag_token(token: str) -> int:
|
|
437
|
+
"""Best-effort validation: hit an authenticated ``ag`` endpoint with ``token``.
|
|
438
|
+
|
|
439
|
+
Returns ``0`` when the token looks valid (or validation is unavailable for
|
|
440
|
+
the current spec); a nonzero exit code when the token was rejected or the
|
|
441
|
+
check itself failed.
|
|
442
|
+
"""
|
|
443
|
+
|
|
444
|
+
try:
|
|
445
|
+
service = resolve_service_by_name("ag")
|
|
446
|
+
spec = fetch_service_spec(service)
|
|
447
|
+
base_url = resolve_runtime_gateway(service)
|
|
448
|
+
table = operations_table(spec, service.service_name, base_url=base_url)
|
|
449
|
+
op = resolve_operation(table, "listAuthenticatedUserIssues")
|
|
450
|
+
except NotFoundError:
|
|
451
|
+
return 0
|
|
452
|
+
except OedError as exc:
|
|
453
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
454
|
+
return exc.code
|
|
455
|
+
|
|
456
|
+
try:
|
|
457
|
+
result = call_operation(op, params={"access_token": token}, include_request=False)
|
|
458
|
+
except OedError as exc:
|
|
459
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
460
|
+
return exc.code
|
|
461
|
+
|
|
462
|
+
if not result.get("ok"):
|
|
463
|
+
click.echo(
|
|
464
|
+
json.dumps(
|
|
465
|
+
{
|
|
466
|
+
"ok": False,
|
|
467
|
+
"code": 1,
|
|
468
|
+
"error": "invalid_token",
|
|
469
|
+
"message": "AtomGit rejected the token",
|
|
470
|
+
"hint": (
|
|
471
|
+
"Check the token is active, or create a new one at "
|
|
472
|
+
"AtomGit > Settings > Access Tokens, then run `oed ag login` again."
|
|
473
|
+
),
|
|
474
|
+
},
|
|
475
|
+
ensure_ascii=False,
|
|
476
|
+
),
|
|
477
|
+
err=True,
|
|
478
|
+
)
|
|
479
|
+
return 1
|
|
480
|
+
return 0
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def _ag_status() -> int:
|
|
484
|
+
"""Report whether a token is configured, without prompting or network access."""
|
|
485
|
+
|
|
486
|
+
configured = read_token("ag") is not None
|
|
487
|
+
meta = token_info("ag") or {}
|
|
488
|
+
encryption = meta.get("encryption")
|
|
489
|
+
status: dict[str, Any] = {
|
|
490
|
+
"ok": True,
|
|
491
|
+
"service": "ag",
|
|
492
|
+
"configured": configured,
|
|
493
|
+
"encryption": encryption,
|
|
494
|
+
"created_at": meta.get("created_at"),
|
|
495
|
+
"token": None,
|
|
496
|
+
}
|
|
497
|
+
# Show the on-disk path only when plaintext is the real backend; in keyring
|
|
498
|
+
# mode that file does not exist (it is deleted on a successful keyring write),
|
|
499
|
+
# so reporting it would mislead the user about where the token lives.
|
|
500
|
+
if encryption != "keyring":
|
|
501
|
+
status["token_path"] = str(token_path("ag"))
|
|
502
|
+
click.echo(
|
|
503
|
+
json.dumps(status, ensure_ascii=False, indent=2)
|
|
504
|
+
)
|
|
505
|
+
return 0
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def _ag_logout(help_requested: bool) -> int:
|
|
509
|
+
"""Delete the stored ``ag`` token."""
|
|
510
|
+
|
|
511
|
+
if help_requested:
|
|
512
|
+
click.echo(
|
|
513
|
+
json.dumps(
|
|
514
|
+
{
|
|
515
|
+
"ok": True,
|
|
516
|
+
"help_for": "ag logout",
|
|
517
|
+
"usage": "oed ag logout",
|
|
518
|
+
"examples": ["oed ag logout"],
|
|
519
|
+
},
|
|
520
|
+
ensure_ascii=False,
|
|
521
|
+
indent=2,
|
|
522
|
+
)
|
|
523
|
+
)
|
|
524
|
+
return 0
|
|
525
|
+
was_set = clear_token("ag")
|
|
526
|
+
click.echo(
|
|
527
|
+
json.dumps(
|
|
528
|
+
{"ok": True, "service": "ag", "configured": False, "was_set": was_set},
|
|
529
|
+
ensure_ascii=False,
|
|
530
|
+
indent=2,
|
|
531
|
+
)
|
|
532
|
+
)
|
|
533
|
+
return 0
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def _dispatch_dynamic(argv: Sequence[str]) -> int:
|
|
537
|
+
"""Parse ``oed <service> [<method>] [flags]`` and run the resolved call."""
|
|
538
|
+
|
|
539
|
+
if not argv:
|
|
540
|
+
click.echo(_usage_dynamic(), err=True)
|
|
541
|
+
return 1
|
|
542
|
+
|
|
543
|
+
service_name = argv[0]
|
|
544
|
+
rest = list(argv[1:])
|
|
545
|
+
|
|
546
|
+
try:
|
|
547
|
+
raw_flags, bool_flags, help_requested, positional = _split_dispatch_argv(rest)
|
|
548
|
+
except OedError as exc:
|
|
549
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
550
|
+
return exc.code
|
|
551
|
+
|
|
552
|
+
user_agent = raw_flags.pop("user-agent", None)
|
|
553
|
+
method = positional[0] if positional else None
|
|
554
|
+
|
|
555
|
+
# `ag login`/`ag logout` are credential-management commands that must not
|
|
556
|
+
# reach the dispatch backend. They run before the allow-list gate (which
|
|
557
|
+
# exempts `ag` anyway — ag is PAT-authenticated, not oneid-governed).
|
|
558
|
+
if service_name == "ag" and method in ("login", "logout"):
|
|
559
|
+
if len(positional) > 1:
|
|
560
|
+
click.echo(
|
|
561
|
+
json.dumps(
|
|
562
|
+
{
|
|
563
|
+
"ok": False,
|
|
564
|
+
"code": 1,
|
|
565
|
+
"error": "too_many_positional",
|
|
566
|
+
"message": "`oed ag login` takes flags, not extra positionals",
|
|
567
|
+
"extra_args": positional[1:],
|
|
568
|
+
},
|
|
569
|
+
ensure_ascii=False,
|
|
570
|
+
),
|
|
571
|
+
err=True,
|
|
572
|
+
)
|
|
573
|
+
return 1
|
|
574
|
+
if method == "login":
|
|
575
|
+
return _ag_login(raw_flags, bool_flags, help_requested)
|
|
576
|
+
return _ag_logout(help_requested)
|
|
577
|
+
|
|
578
|
+
# Per-user allow-list gate (local check against auth.json, no HTTP).
|
|
579
|
+
# Runs only for real service calls — after the ag credential-management
|
|
580
|
+
# short-circuit above, which must stay outside the gate.
|
|
581
|
+
try:
|
|
582
|
+
_check_allowlist(service_name)
|
|
583
|
+
except OedError as exc:
|
|
584
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
585
|
+
return exc.code
|
|
586
|
+
|
|
587
|
+
try:
|
|
588
|
+
service = resolve_service_by_name(service_name)
|
|
589
|
+
except OedError as exc:
|
|
590
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
591
|
+
return exc.code
|
|
592
|
+
|
|
593
|
+
try:
|
|
594
|
+
spec = fetch_service_spec(service)
|
|
595
|
+
except OedError as exc:
|
|
596
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
597
|
+
return exc.code
|
|
598
|
+
|
|
599
|
+
base_url = resolve_runtime_gateway(service)
|
|
600
|
+
ops = collect_operations(spec, service.service_name, base_url=base_url)
|
|
601
|
+
|
|
602
|
+
if not method:
|
|
603
|
+
# ``oed <service>`` alone → list every available operation.
|
|
604
|
+
if help_requested:
|
|
605
|
+
click.echo(
|
|
606
|
+
json.dumps(_service_help_payload(service, ops), ensure_ascii=False, indent=2)
|
|
607
|
+
)
|
|
608
|
+
return 0
|
|
609
|
+
click.echo(json.dumps(describe_service(service, ops), ensure_ascii=False, indent=2))
|
|
610
|
+
return 0
|
|
611
|
+
|
|
612
|
+
table = operations_table(spec, service.service_name, base_url=base_url)
|
|
613
|
+
if method not in table:
|
|
614
|
+
for key in table:
|
|
615
|
+
if key.lower() == method.lower():
|
|
616
|
+
method = key
|
|
617
|
+
break
|
|
618
|
+
|
|
619
|
+
try:
|
|
620
|
+
op = resolve_operation(table, method)
|
|
621
|
+
except OedError as exc:
|
|
622
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
623
|
+
return exc.code
|
|
624
|
+
|
|
625
|
+
if help_requested:
|
|
626
|
+
click.echo(
|
|
627
|
+
json.dumps(describe_operation_help(op, service), ensure_ascii=False, indent=2)
|
|
628
|
+
)
|
|
629
|
+
return 0
|
|
630
|
+
|
|
631
|
+
try:
|
|
632
|
+
params, body, exit_code = _merge_params(op, raw_flags)
|
|
633
|
+
except OedError as exc:
|
|
634
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
635
|
+
return exc.code
|
|
636
|
+
if exit_code:
|
|
637
|
+
# Re-parse the params we tried to load so the user sees the error.
|
|
638
|
+
if "params" in raw_flags:
|
|
639
|
+
try:
|
|
640
|
+
parse_json_arg(raw_flags["params"], flag="params")
|
|
641
|
+
except OedError as exc:
|
|
642
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
643
|
+
return exc.code
|
|
644
|
+
if "json" in raw_flags:
|
|
645
|
+
try:
|
|
646
|
+
parse_json_arg(raw_flags["json"], flag="json")
|
|
647
|
+
except OedError as exc:
|
|
648
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
649
|
+
return exc.code
|
|
650
|
+
return exit_code
|
|
651
|
+
|
|
652
|
+
dry_run = "dry-run" in bool_flags
|
|
653
|
+
|
|
654
|
+
try:
|
|
655
|
+
token, cookie = _resolve_auth()
|
|
656
|
+
result = call_operation(
|
|
657
|
+
op,
|
|
658
|
+
params=params,
|
|
659
|
+
body=body,
|
|
660
|
+
dry_run=dry_run,
|
|
661
|
+
include_request=True,
|
|
662
|
+
user_agent=user_agent,
|
|
663
|
+
token=token,
|
|
664
|
+
cookie=cookie,
|
|
665
|
+
)
|
|
666
|
+
except OedError as exc:
|
|
667
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
668
|
+
return exc.code
|
|
669
|
+
|
|
670
|
+
click.echo(json.dumps(result, ensure_ascii=False, indent=2))
|
|
671
|
+
return 0 if result.get("ok") else 3
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def _usage_dynamic() -> str:
|
|
675
|
+
return json.dumps(
|
|
676
|
+
{
|
|
677
|
+
"ok": False,
|
|
678
|
+
"code": 1,
|
|
679
|
+
"error": "missing_service",
|
|
680
|
+
"message": "dynamic dispatch: pass <service> as the first argument",
|
|
681
|
+
"examples": [
|
|
682
|
+
"oed <service> # list operations",
|
|
683
|
+
"oed <service> <operation> # operation-level help",
|
|
684
|
+
"oed <service> <operation> --<flag> <value> # call with one flag",
|
|
685
|
+
"oed <service> <operation> --params '{...}' # bulk JSON params",
|
|
686
|
+
"oed <service> <operation> --dry-run # preview only",
|
|
687
|
+
],
|
|
688
|
+
},
|
|
689
|
+
ensure_ascii=False,
|
|
690
|
+
indent=2,
|
|
691
|
+
)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def _service_help_payload(service, ops: list) -> dict:
|
|
695
|
+
"""Payload for ``oed <service> --help``: enumerate operations + flag cheatsheet."""
|
|
696
|
+
|
|
697
|
+
return {
|
|
698
|
+
"ok": True,
|
|
699
|
+
"help_for": service.name,
|
|
700
|
+
"title": service.title,
|
|
701
|
+
"operations": [op.display_name for op in ops],
|
|
702
|
+
"operation_aliases": {
|
|
703
|
+
op.display_name: op.operation_id
|
|
704
|
+
for op in ops
|
|
705
|
+
if op.display_name != op.operation_id
|
|
706
|
+
} or None,
|
|
707
|
+
"usage": (
|
|
708
|
+
"oed <service> <operation> --<flag> <value>\n"
|
|
709
|
+
"Each declared query / path parameter is exposed as its own "
|
|
710
|
+
"--<kebab-case> flag. Use `oed <service> <operation> --help` "
|
|
711
|
+
"to list them, or pass `--params '{...}'` for bulk JSON."
|
|
712
|
+
),
|
|
713
|
+
"examples": [
|
|
714
|
+
f"oed {service.service_name} {ops[0].display_name} --help",
|
|
715
|
+
f"oed {service.service_name} {ops[0].display_name} --dry-run",
|
|
716
|
+
],
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
def _dispatch_dynamic_help(service_name: str, positional: list[str]) -> int:
|
|
721
|
+
"""Legacy ``oed <service> --help`` entry point — kept for tests.
|
|
722
|
+
|
|
723
|
+
New code path goes through :func:`_service_help_payload` directly
|
|
724
|
+
inside :func:`_dispatch_dynamic`; this thin wrapper preserves the
|
|
725
|
+
public function name so existing tests stay in sync.
|
|
726
|
+
"""
|
|
727
|
+
|
|
728
|
+
if positional:
|
|
729
|
+
click.echo(
|
|
730
|
+
json.dumps(
|
|
731
|
+
{
|
|
732
|
+
"ok": False,
|
|
733
|
+
"code": 1,
|
|
734
|
+
"error": "too_many_positional",
|
|
735
|
+
"message": "`oed <service> --help` lists operations; remove the extra argument",
|
|
736
|
+
"extra_args": positional,
|
|
737
|
+
},
|
|
738
|
+
ensure_ascii=False,
|
|
739
|
+
),
|
|
740
|
+
err=True,
|
|
741
|
+
)
|
|
742
|
+
return 1
|
|
743
|
+
|
|
744
|
+
try:
|
|
745
|
+
service = resolve_service_by_name(service_name)
|
|
746
|
+
except OedError as exc:
|
|
747
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
748
|
+
return exc.code
|
|
749
|
+
try:
|
|
750
|
+
spec = fetch_service_spec(service)
|
|
751
|
+
except OedError as exc:
|
|
752
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
753
|
+
return exc.code
|
|
754
|
+
|
|
755
|
+
base_url = resolve_runtime_gateway(service)
|
|
756
|
+
ops = collect_operations(spec, service.service_name, base_url=base_url)
|
|
757
|
+
click.echo(json.dumps(_service_help_payload(service, ops), ensure_ascii=False, indent=2))
|
|
758
|
+
return 0
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
762
|
+
"""Top-level entry point. Returns a Unix-style exit code."""
|
|
763
|
+
|
|
764
|
+
raw = list(argv if argv is not None else sys.argv[1:])
|
|
765
|
+
try:
|
|
766
|
+
if _looks_like_reserved(raw):
|
|
767
|
+
try:
|
|
768
|
+
click_cli.main(args=raw, standalone_mode=False)
|
|
769
|
+
except click.exceptions.ClickException as exc:
|
|
770
|
+
exc.show()
|
|
771
|
+
return exc.exit_code
|
|
772
|
+
except SystemExit as exc:
|
|
773
|
+
return int(exc.code or 0)
|
|
774
|
+
return 0
|
|
775
|
+
return _dispatch_dynamic(raw)
|
|
776
|
+
except OedError as exc:
|
|
777
|
+
click.echo(json.dumps(exc.to_dict(), ensure_ascii=False), err=True)
|
|
778
|
+
return exc.code
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
if __name__ == "__main__":
|
|
782
|
+
sys.exit(main())
|