tryorchard-cli 0.0.0__tar.gz → 0.0.1__tar.gz
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.
- tryorchard_cli-0.0.1/PKG-INFO +44 -0
- tryorchard_cli-0.0.1/README.md +32 -0
- tryorchard_cli-0.0.1/orchard/__init__.py +2 -0
- tryorchard_cli-0.0.1/orchard/cli.py +94 -0
- tryorchard_cli-0.0.1/orchard/client.py +166 -0
- tryorchard_cli-0.0.1/orchard/cmd_access.py +148 -0
- tryorchard_cli-0.0.1/orchard/cmd_debugger.py +70 -0
- tryorchard_cli-0.0.1/orchard/cmd_exec.py +143 -0
- tryorchard_cli-0.0.1/orchard/cmd_frida.py +165 -0
- tryorchard_cli-0.0.1/orchard/cmd_input.py +90 -0
- tryorchard_cli-0.0.1/orchard/cmd_kernel.py +85 -0
- tryorchard_cli-0.0.1/orchard/cmd_lifecycle.py +166 -0
- tryorchard_cli-0.0.1/orchard/cmd_net.py +357 -0
- tryorchard_cli-0.0.1/orchard/cmd_record.py +117 -0
- tryorchard_cli-0.0.1/orchard/cmd_relay.py +67 -0
- tryorchard_cli-0.0.1/orchard/cmd_remote.py +237 -0
- tryorchard_cli-0.0.1/orchard/cmd_sandbox.py +169 -0
- tryorchard_cli-0.0.1/orchard/cmd_telephony.py +50 -0
- tryorchard_cli-0.0.1/orchard/config.py +45 -0
- tryorchard_cli-0.0.1/orchard/context.py +16 -0
- tryorchard_cli-0.0.1/orchard/output.py +51 -0
- tryorchard_cli-0.0.1/orchard/ptyshell.py +118 -0
- tryorchard_cli-0.0.1/orchard/selector.py +55 -0
- tryorchard_cli-0.0.1/orchard/viewers.py +92 -0
- tryorchard_cli-0.0.1/orchard/wsbridge.py +212 -0
- tryorchard_cli-0.0.1/pyproject.toml +29 -0
- tryorchard_cli-0.0.1/tests/test_client.py +56 -0
- tryorchard_cli-0.0.1/tests/test_config.py +33 -0
- tryorchard_cli-0.0.1/tests/test_debugger.py +66 -0
- tryorchard_cli-0.0.1/tests/test_frida.py +109 -0
- tryorchard_cli-0.0.1/tests/test_input.py +60 -0
- tryorchard_cli-0.0.1/tests/test_net.py +35 -0
- tryorchard_cli-0.0.1/tests/test_remote.py +117 -0
- tryorchard_cli-0.0.1/tests/test_selector.py +56 -0
- tryorchard_cli-0.0.1/tests/test_viewers.py +56 -0
- tryorchard_cli-0.0.1/tests/test_wsbridge.py +75 -0
- tryorchard_cli-0.0.1/tryorchard_cli.egg-info/PKG-INFO +44 -0
- tryorchard_cli-0.0.1/tryorchard_cli.egg-info/SOURCES.txt +41 -0
- tryorchard_cli-0.0.1/tryorchard_cli.egg-info/entry_points.txt +4 -0
- tryorchard_cli-0.0.1/tryorchard_cli.egg-info/requires.txt +4 -0
- tryorchard_cli-0.0.1/tryorchard_cli.egg-info/top_level.txt +1 -0
- tryorchard_cli-0.0.0/LICENSE +0 -21
- tryorchard_cli-0.0.0/PKG-INFO +0 -24
- tryorchard_cli-0.0.0/README.md +0 -9
- tryorchard_cli-0.0.0/pyproject.toml +0 -21
- tryorchard_cli-0.0.0/tryorchard_cli.egg-info/PKG-INFO +0 -24
- tryorchard_cli-0.0.0/tryorchard_cli.egg-info/SOURCES.txt +0 -7
- tryorchard_cli-0.0.0/tryorchard_cli.egg-info/top_level.txt +0 -1
- {tryorchard_cli-0.0.0 → tryorchard_cli-0.0.1}/setup.cfg +0 -0
- {tryorchard_cli-0.0.0 → tryorchard_cli-0.0.1}/tryorchard_cli.egg-info/dependency_links.txt +0 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tryorchard-cli
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Orchard CLI — drive sandboxes (sandbox/snapshot/template/team/key management and live adb/scrcpy/vnc/shell/logs/files) from the terminal.
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: typer>=0.12
|
|
9
|
+
Requires-Dist: rich>=13
|
|
10
|
+
Requires-Dist: httpx<0.29,>=0.28
|
|
11
|
+
Requires-Dist: websockets<15,>=14
|
|
12
|
+
|
|
13
|
+
# Orchard CLI
|
|
14
|
+
|
|
15
|
+
Drive Orchard iOS and Android device sandboxes from your terminal — launch and
|
|
16
|
+
manage devices, stream the screen, run `adb`/`scrcpy`/`vnc`/shell, tail logs,
|
|
17
|
+
push/pull files, instrument with Frida, and attach the on-device debugger.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
pip install tryorchard-cli
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This installs the `orchard` command (plus `orchard-adb` and `orchard-tunnel`).
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
orchard key set <your-api-key> # authenticate to your control plane
|
|
31
|
+
orchard sandbox launch --platform android
|
|
32
|
+
orchard sandbox list
|
|
33
|
+
orchard sandbox shell <device> # interactive shell
|
|
34
|
+
orchard debugger attach <device> --process <package> # on-device lldb
|
|
35
|
+
orchard frida ... # instrument an app
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Command groups: `sandbox`, `tag`, `snapshot`, `template`, `kernel`, `input`,
|
|
39
|
+
`telephony`, `relay`, `frida`, `record`, `debugger`, `key`, `team`, `member`.
|
|
40
|
+
Run `orchard --help` (or `orchard <group> --help`) for the full set.
|
|
41
|
+
|
|
42
|
+
## Links
|
|
43
|
+
|
|
44
|
+
- Orchard: https://tryorchard.com
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Orchard CLI
|
|
2
|
+
|
|
3
|
+
Drive Orchard iOS and Android device sandboxes from your terminal — launch and
|
|
4
|
+
manage devices, stream the screen, run `adb`/`scrcpy`/`vnc`/shell, tail logs,
|
|
5
|
+
push/pull files, instrument with Frida, and attach the on-device debugger.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install tryorchard-cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This installs the `orchard` command (plus `orchard-adb` and `orchard-tunnel`).
|
|
14
|
+
|
|
15
|
+
## Quickstart
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
orchard key set <your-api-key> # authenticate to your control plane
|
|
19
|
+
orchard sandbox launch --platform android
|
|
20
|
+
orchard sandbox list
|
|
21
|
+
orchard sandbox shell <device> # interactive shell
|
|
22
|
+
orchard debugger attach <device> --process <package> # on-device lldb
|
|
23
|
+
orchard frida ... # instrument an app
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Command groups: `sandbox`, `tag`, `snapshot`, `template`, `kernel`, `input`,
|
|
27
|
+
`telephony`, `relay`, `frida`, `record`, `debugger`, `key`, `team`, `member`.
|
|
28
|
+
Run `orchard --help` (or `orchard <group> --help`) for the full set.
|
|
29
|
+
|
|
30
|
+
## Links
|
|
31
|
+
|
|
32
|
+
- Orchard: https://tryorchard.com
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Orchard CLI root: global connection options + the command tree.
|
|
2
|
+
|
|
3
|
+
orchard sandbox list
|
|
4
|
+
orchard tag set <sel> payments --color "#22c55e"
|
|
5
|
+
orchard adb <sel> # bridge adb to a local port
|
|
6
|
+
orchard scrcpy <sel> # Android live screen (adb + scrcpy)
|
|
7
|
+
orchard vnc <sel> # iOS VM live screen (RFB + viewer)
|
|
8
|
+
orchard shell <sel> # interactive device shell
|
|
9
|
+
orchard key add k --sandbox <sel> --cap shell --cap logs --expires 7
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import typer
|
|
14
|
+
|
|
15
|
+
from . import (cmd_access, cmd_debugger, cmd_exec, cmd_frida, cmd_input,
|
|
16
|
+
cmd_kernel, cmd_lifecycle, cmd_net, cmd_record, cmd_relay,
|
|
17
|
+
cmd_remote, cmd_sandbox, cmd_telephony)
|
|
18
|
+
from .client import ApiError
|
|
19
|
+
from .config import Config
|
|
20
|
+
from .output import err_console
|
|
21
|
+
from .selector import SelectorError
|
|
22
|
+
|
|
23
|
+
app = typer.Typer(no_args_is_help=True, add_completion=True,
|
|
24
|
+
help="Orchard CLI — manage sandboxes and reach them live.")
|
|
25
|
+
|
|
26
|
+
app.add_typer(cmd_sandbox.app, name="sandbox")
|
|
27
|
+
app.add_typer(cmd_sandbox.tag_app, name="tag")
|
|
28
|
+
app.add_typer(cmd_kernel.app, name="kernel")
|
|
29
|
+
app.add_typer(cmd_input.app, name="input")
|
|
30
|
+
app.add_typer(cmd_telephony.app, name="telephony")
|
|
31
|
+
app.add_typer(cmd_relay.app, name="relay")
|
|
32
|
+
app.add_typer(cmd_frida.app, name="frida")
|
|
33
|
+
app.add_typer(cmd_record.app, name="record")
|
|
34
|
+
app.add_typer(cmd_debugger.app, name="debugger")
|
|
35
|
+
app.add_typer(cmd_lifecycle.snapshot_app, name="snapshot")
|
|
36
|
+
app.add_typer(cmd_lifecycle.template_app, name="template")
|
|
37
|
+
app.add_typer(cmd_access.key_app, name="key")
|
|
38
|
+
app.add_typer(cmd_access.team_app, name="team")
|
|
39
|
+
app.add_typer(cmd_access.member_app, name="member")
|
|
40
|
+
cmd_net.register(app) # adb, tunnel, scrcpy, vnc, screen
|
|
41
|
+
cmd_exec.register(app) # shell, logs, app *, file *
|
|
42
|
+
cmd_remote.register(app) # remote (cloud) sandboxes: create/ls/shell/push/pull/logs/metrics
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@app.callback()
|
|
46
|
+
def main(
|
|
47
|
+
ctx: typer.Context,
|
|
48
|
+
url: str = typer.Option(None, "--url", envvar=["ORCHARD_API_URL", "ORCHARD_URL"],
|
|
49
|
+
help="Control-plane URL (or $ORCHARD_API_URL)."),
|
|
50
|
+
token: str = typer.Option(None, "--token", envvar="ORCHARD_API_KEY",
|
|
51
|
+
help="Orchard API key (or $ORCHARD_API_KEY)."),
|
|
52
|
+
org: str = typer.Option(None, "--org", envvar="ORCHARD_ORG", help="Org slug."),
|
|
53
|
+
ws: str = typer.Option(None, "--ws", envvar="ORCHARD_WS", help="Workspace slug."),
|
|
54
|
+
json_output: bool = typer.Option(False, "--json", help="Machine-readable JSON output."),
|
|
55
|
+
):
|
|
56
|
+
"""Resolve connection config once and hang it on the context."""
|
|
57
|
+
if not url or not token:
|
|
58
|
+
err_console.print(
|
|
59
|
+
"[red]error:[/red] set --url/$ORCHARD_API_URL and --token/$ORCHARD_API_KEY")
|
|
60
|
+
raise typer.Exit(2)
|
|
61
|
+
ctx.obj = Config(url=url, token=token, org=org, ws=ws, json=json_output)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _run() -> None:
|
|
65
|
+
"""Entry wrapper: turn API/selector errors into clean one-line messages
|
|
66
|
+
instead of tracebacks, while letting Click handle usage/help/abort itself.
|
|
67
|
+
Typer vendors click; fall back to a real click install on older versions."""
|
|
68
|
+
try:
|
|
69
|
+
from typer._click import exceptions as cexc
|
|
70
|
+
except Exception: # pragma: no cover - older typer uses the real click
|
|
71
|
+
from click import exceptions as cexc
|
|
72
|
+
# typer's vendored click shim doesn't always re-export Exit; only reference
|
|
73
|
+
# the ones that exist so a normal exit never trips an AttributeError.
|
|
74
|
+
exit_types = tuple(t for t in (typer.Exit, getattr(cexc, "Exit", None)) if t)
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
app(standalone_mode=False)
|
|
78
|
+
except (ApiError, SelectorError) as e:
|
|
79
|
+
err_console.print(f"[red]error:[/red] {e}")
|
|
80
|
+
raise SystemExit(1)
|
|
81
|
+
except typer.Abort:
|
|
82
|
+
err_console.print("aborted")
|
|
83
|
+
raise SystemExit(1)
|
|
84
|
+
except cexc.ClickException as e: # usage errors, bad options
|
|
85
|
+
e.show()
|
|
86
|
+
raise SystemExit(e.exit_code)
|
|
87
|
+
except exit_types as e: # normal exits incl. --help/--version
|
|
88
|
+
raise SystemExit(getattr(e, "exit_code", 0))
|
|
89
|
+
except KeyboardInterrupt:
|
|
90
|
+
raise SystemExit(130)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
_run()
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""The one place that knows Orchard's REST wire: base URL, auth header, and
|
|
2
|
+
how a server error turns into a message a human can act on."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from .config import Config
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ApiError(Exception):
|
|
13
|
+
"""A non-2xx REST response, with a message mapped for the terminal."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, status: int, detail: str):
|
|
16
|
+
self.status = status
|
|
17
|
+
self.detail = detail
|
|
18
|
+
super().__init__(self.user_message)
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def user_message(self) -> str:
|
|
22
|
+
if self.status == 401:
|
|
23
|
+
return "the API key is invalid or expired (check ORCHARD_API_KEY)"
|
|
24
|
+
if self.status == 404:
|
|
25
|
+
return self.detail or "no such sandbox, or this key may not reach it"
|
|
26
|
+
return self.detail or f"request failed ({self.status})"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _detail(r: httpx.Response) -> str:
|
|
30
|
+
try:
|
|
31
|
+
body = r.json()
|
|
32
|
+
except Exception:
|
|
33
|
+
return r.text[:300].strip()
|
|
34
|
+
if isinstance(body, dict):
|
|
35
|
+
return str(body.get("detail") or body)
|
|
36
|
+
return str(body)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Client:
|
|
40
|
+
"""Thin httpx wrapper. Transport only — commands own presentation."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, cfg: Config):
|
|
43
|
+
self.cfg = cfg
|
|
44
|
+
headers = {"Authorization": f"Bearer {cfg.token}"}
|
|
45
|
+
if cfg.org:
|
|
46
|
+
headers["X-Orchard-Org"] = cfg.org
|
|
47
|
+
if cfg.ws:
|
|
48
|
+
headers["X-Orchard-Workspace"] = cfg.ws
|
|
49
|
+
self._c = httpx.Client(base_url=cfg.api_base, headers=headers, timeout=60.0)
|
|
50
|
+
|
|
51
|
+
# -- lifecycle --------------------------------------------------------
|
|
52
|
+
def close(self) -> None:
|
|
53
|
+
self._c.close()
|
|
54
|
+
|
|
55
|
+
def __enter__(self) -> "Client":
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
def __exit__(self, *exc) -> None:
|
|
59
|
+
self.close()
|
|
60
|
+
|
|
61
|
+
# -- verbs ------------------------------------------------------------
|
|
62
|
+
def _request(self, method: str, path: str, **kw):
|
|
63
|
+
r = self._c.request(method, path, **kw)
|
|
64
|
+
if r.status_code >= 400:
|
|
65
|
+
raise ApiError(r.status_code, _detail(r))
|
|
66
|
+
if r.status_code == 204 or not r.content:
|
|
67
|
+
return None
|
|
68
|
+
return r.json()
|
|
69
|
+
|
|
70
|
+
def get(self, path: str, **kw):
|
|
71
|
+
return self._request("GET", path, **kw)
|
|
72
|
+
|
|
73
|
+
def get_content(self, path: str, **kw) -> bytes:
|
|
74
|
+
"""A GET whose body is raw bytes (e.g. a screenshot), not JSON."""
|
|
75
|
+
r = self._c.request("GET", path, **kw)
|
|
76
|
+
if r.status_code >= 400:
|
|
77
|
+
raise ApiError(r.status_code, _detail(r))
|
|
78
|
+
return r.content
|
|
79
|
+
|
|
80
|
+
def post(self, path: str, json=None, **kw):
|
|
81
|
+
return self._request("POST", path, json=json, **kw)
|
|
82
|
+
|
|
83
|
+
def patch(self, path: str, json=None, **kw):
|
|
84
|
+
return self._request("PATCH", path, json=json, **kw)
|
|
85
|
+
|
|
86
|
+
def delete(self, path: str, **kw):
|
|
87
|
+
return self._request("DELETE", path, **kw)
|
|
88
|
+
|
|
89
|
+
def post_files(self, path: str, files: list[tuple[str, Path]], data: dict | None = None,
|
|
90
|
+
timeout: float | None = None):
|
|
91
|
+
"""multipart POST (APK install, file upload). `files` is [(field, path)].
|
|
92
|
+
`timeout` overrides the default for large uploads (an AVD profile is GBs)."""
|
|
93
|
+
handles = [(field, (p.name, p.open("rb"))) for field, p in files]
|
|
94
|
+
kw = {"files": handles, "data": data or {}}
|
|
95
|
+
if timeout is not None:
|
|
96
|
+
kw["timeout"] = timeout
|
|
97
|
+
try:
|
|
98
|
+
return self._request("POST", path, **kw)
|
|
99
|
+
finally:
|
|
100
|
+
for _, (_, fh) in handles:
|
|
101
|
+
fh.close()
|
|
102
|
+
|
|
103
|
+
def post_stream(self, path: str, source: Path, params: dict | None = None,
|
|
104
|
+
timeout: float = 7200.0):
|
|
105
|
+
"""POST a file as a raw streaming body (no multipart) — for a multi-GB upload
|
|
106
|
+
that must not be buffered. Reads the file in chunks via a generator so neither
|
|
107
|
+
the client nor the server ever holds the whole file. Metadata rides the query."""
|
|
108
|
+
size = source.stat().st_size
|
|
109
|
+
|
|
110
|
+
def chunks():
|
|
111
|
+
with source.open("rb") as fh:
|
|
112
|
+
while data := fh.read(1 << 20):
|
|
113
|
+
yield data
|
|
114
|
+
|
|
115
|
+
return self._request("POST", path, params=params or {}, content=chunks(),
|
|
116
|
+
headers={"Content-Type": "application/octet-stream",
|
|
117
|
+
"Content-Length": str(size)},
|
|
118
|
+
timeout=timeout)
|
|
119
|
+
|
|
120
|
+
def stream_file_to(self, base_url: str, path: str, token: str, source: Path,
|
|
121
|
+
timeout: float = 7200.0):
|
|
122
|
+
"""Stream a file as a raw body straight to a node agent (not the control plane),
|
|
123
|
+
chunked so nothing is buffered. Used for direct multi-GB AVD uploads."""
|
|
124
|
+
size = source.stat().st_size
|
|
125
|
+
|
|
126
|
+
def chunks():
|
|
127
|
+
with source.open("rb") as fh:
|
|
128
|
+
while data := fh.read(1 << 20):
|
|
129
|
+
yield data
|
|
130
|
+
|
|
131
|
+
with httpx.Client(timeout=httpx.Timeout(timeout, connect=10)) as c:
|
|
132
|
+
r = c.post(f"{base_url.rstrip('/')}{path}", content=chunks(),
|
|
133
|
+
headers={"Authorization": f"Bearer {token}",
|
|
134
|
+
"Content-Type": "application/octet-stream",
|
|
135
|
+
"Content-Length": str(size)})
|
|
136
|
+
if r.status_code >= 400:
|
|
137
|
+
raise ApiError(r.status_code, _detail(r))
|
|
138
|
+
return r.json()
|
|
139
|
+
|
|
140
|
+
def stream_to_file(self, path: str, dest: Path, timeout: float = 3600.0) -> int:
|
|
141
|
+
"""Stream an authed endpoint (relative path) to a file — for large downloads
|
|
142
|
+
like an AVD profile. Carries the normal Authorization header."""
|
|
143
|
+
total = 0
|
|
144
|
+
with self._c.stream("GET", path, timeout=timeout) as r:
|
|
145
|
+
if r.status_code >= 400:
|
|
146
|
+
r.read()
|
|
147
|
+
raise ApiError(r.status_code, _detail(r))
|
|
148
|
+
with dest.open("wb") as fh:
|
|
149
|
+
for chunk in r.iter_bytes(1 << 20):
|
|
150
|
+
fh.write(chunk)
|
|
151
|
+
total += len(chunk)
|
|
152
|
+
return total
|
|
153
|
+
|
|
154
|
+
def download(self, absolute_url: str, dest: Path) -> int:
|
|
155
|
+
"""Stream a signed-grant URL (full URL incl. ?token=) to a file. The
|
|
156
|
+
grant is the credential, so this call carries no Authorization header."""
|
|
157
|
+
total = 0
|
|
158
|
+
with httpx.Client(timeout=300.0) as raw, \
|
|
159
|
+
raw.stream("GET", absolute_url) as r:
|
|
160
|
+
if r.status_code >= 400:
|
|
161
|
+
raise ApiError(r.status_code, _detail(r))
|
|
162
|
+
with dest.open("wb") as fh:
|
|
163
|
+
for chunk in r.iter_bytes(65536):
|
|
164
|
+
fh.write(chunk)
|
|
165
|
+
total += len(chunk)
|
|
166
|
+
return total
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""`orchard key …`, `orchard team …`, `orchard member …`."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
|
|
6
|
+
from .context import cfg, client
|
|
7
|
+
from .output import console, emit, fail, ok, table
|
|
8
|
+
from .selector import SelectorError, resolve
|
|
9
|
+
|
|
10
|
+
key_app = typer.Typer(no_args_is_help=True, help="API keys, including fine-grained scoped keys.")
|
|
11
|
+
team_app = typer.Typer(no_args_is_help=True, help="Workspaces (teams) in your org.")
|
|
12
|
+
member_app = typer.Typer(no_args_is_help=True, help="Org members and invites.")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# -- keys -----------------------------------------------------------------
|
|
16
|
+
def _scope_summary(t: dict) -> str:
|
|
17
|
+
bits = []
|
|
18
|
+
if t.get("sandbox_uuids"):
|
|
19
|
+
bits.append(f"{len(t['sandbox_uuids'])} sandbox(es)")
|
|
20
|
+
if t.get("tag"):
|
|
21
|
+
bits.append(f"tag:{t['tag']}")
|
|
22
|
+
if t.get("allowed_features") is not None:
|
|
23
|
+
bits.append("caps:" + ",".join(t["allowed_features"]) if t["allowed_features"] else "caps:none")
|
|
24
|
+
return "; ".join(bits) or "all (org-wide)"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@key_app.command("list")
|
|
28
|
+
def key_list(ctx: typer.Context):
|
|
29
|
+
with client(ctx) as c:
|
|
30
|
+
rows = c.get("/tokens") or []
|
|
31
|
+
if cfg(ctx).json:
|
|
32
|
+
return emit(cfg(ctx), rows)
|
|
33
|
+
table("API keys", ["id", "name", "hint", "scope", "expires", "last used"],
|
|
34
|
+
[[t.get("id"), t.get("name"), f"{t.get('hint')}…", _scope_summary(t),
|
|
35
|
+
t.get("expires_at") or "never", t.get("last_used") or "-"] for t in rows])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@key_app.command("add")
|
|
39
|
+
def key_add(ctx: typer.Context, name: str,
|
|
40
|
+
sandbox: list[str] = typer.Option(None, "--sandbox",
|
|
41
|
+
help="Limit the key to this sandbox (uuid/name/tag). Repeatable."),
|
|
42
|
+
tag: str = typer.Option(None, "--tag",
|
|
43
|
+
help="Limit the key to all sandboxes carrying this tag (incl. future)."),
|
|
44
|
+
cap: list[str] = typer.Option(None, "--cap",
|
|
45
|
+
help="Allow only these capabilities (feature keys). Repeatable."),
|
|
46
|
+
expires: int = typer.Option(None, "--expires", help="Days until the key expires.")):
|
|
47
|
+
"""Mint an API key. Prints the secret once — store it now."""
|
|
48
|
+
body: dict = {"name": name}
|
|
49
|
+
if expires is not None:
|
|
50
|
+
body["expires_in_days"] = expires
|
|
51
|
+
if tag:
|
|
52
|
+
body["tag"] = tag
|
|
53
|
+
if cap:
|
|
54
|
+
body["capabilities"] = cap
|
|
55
|
+
if sandbox:
|
|
56
|
+
with client(ctx) as c:
|
|
57
|
+
uuids = []
|
|
58
|
+
for sel in sandbox:
|
|
59
|
+
try:
|
|
60
|
+
uuids.append(resolve(c, sel)["uuid"])
|
|
61
|
+
except SelectorError as e:
|
|
62
|
+
raise fail(str(e))
|
|
63
|
+
body["sandbox_uuids"] = uuids
|
|
64
|
+
with client(ctx) as c:
|
|
65
|
+
res = c.post("/tokens", json=body)
|
|
66
|
+
if cfg(ctx).json:
|
|
67
|
+
return emit(cfg(ctx), res)
|
|
68
|
+
ok(f"created key {name} — scope: {_scope_summary(res)}")
|
|
69
|
+
console.print(f"\n [bold]{res['token']}[/bold]\n")
|
|
70
|
+
console.print("[yellow]This is shown once. Export it as ORCHARD_API_KEY.[/yellow]")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@key_app.command("revoke")
|
|
74
|
+
def key_revoke(ctx: typer.Context, key_id: int):
|
|
75
|
+
with client(ctx) as c:
|
|
76
|
+
c.delete(f"/tokens/{key_id}")
|
|
77
|
+
ok(f"revoked key {key_id}")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# -- teams (workspaces) ---------------------------------------------------
|
|
81
|
+
@team_app.command("list")
|
|
82
|
+
def team_list(ctx: typer.Context):
|
|
83
|
+
with client(ctx) as c:
|
|
84
|
+
me = c.get("/me") or {}
|
|
85
|
+
rows = me.get("workspaces", [])
|
|
86
|
+
if cfg(ctx).json:
|
|
87
|
+
return emit(cfg(ctx), rows)
|
|
88
|
+
table("Workspaces", ["slug", "name", "role"],
|
|
89
|
+
[[w.get("slug"), w.get("name"), w.get("role")] for w in rows])
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@team_app.command("create")
|
|
93
|
+
def team_create(ctx: typer.Context, name: str):
|
|
94
|
+
with client(ctx) as c:
|
|
95
|
+
res = c.post("/workspaces", json={"name": name})
|
|
96
|
+
if cfg(ctx).json:
|
|
97
|
+
return emit(cfg(ctx), res)
|
|
98
|
+
ok(f"created workspace {res.get('slug', name)}")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@team_app.command("rm")
|
|
102
|
+
def team_rm(ctx: typer.Context, slug: str,
|
|
103
|
+
yes: bool = typer.Option(False, "--yes", "-y")):
|
|
104
|
+
if not yes:
|
|
105
|
+
typer.confirm(f"Delete workspace {slug}?", abort=True)
|
|
106
|
+
with client(ctx) as c:
|
|
107
|
+
c.delete(f"/workspaces/{slug}")
|
|
108
|
+
ok(f"deleted workspace {slug}")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# -- members --------------------------------------------------------------
|
|
112
|
+
@member_app.command("list")
|
|
113
|
+
def member_list(ctx: typer.Context):
|
|
114
|
+
with client(ctx) as c:
|
|
115
|
+
rows = c.get("/org/members") or []
|
|
116
|
+
if cfg(ctx).json:
|
|
117
|
+
return emit(cfg(ctx), rows)
|
|
118
|
+
table("Members", ["id", "username", "role", "active"],
|
|
119
|
+
[[m.get("id"), m.get("username"), m.get("role"), m.get("is_active", True)] for m in rows])
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@member_app.command("invite")
|
|
123
|
+
def member_invite(ctx: typer.Context,
|
|
124
|
+
role: str = typer.Option("member", "--role", help="member | admin")):
|
|
125
|
+
"""Create an org invite token."""
|
|
126
|
+
with client(ctx) as c:
|
|
127
|
+
res = c.post("/org/invites", json={"role": role})
|
|
128
|
+
if cfg(ctx).json:
|
|
129
|
+
return emit(cfg(ctx), res)
|
|
130
|
+
ok("invite created")
|
|
131
|
+
console.print(f"\n token: [bold]{res.get('token')}[/bold] (expires {res.get('expires_at')})\n")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@member_app.command("role")
|
|
135
|
+
def member_role(ctx: typer.Context, member_id: int, role: str):
|
|
136
|
+
with client(ctx) as c:
|
|
137
|
+
c.patch(f"/org/members/{member_id}", json={"role": role})
|
|
138
|
+
ok(f"member {member_id} is now {role}")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@member_app.command("rm")
|
|
142
|
+
def member_rm(ctx: typer.Context, member_id: int,
|
|
143
|
+
yes: bool = typer.Option(False, "--yes", "-y")):
|
|
144
|
+
if not yes:
|
|
145
|
+
typer.confirm(f"Remove member {member_id} from the org?", abort=True)
|
|
146
|
+
with client(ctx) as c:
|
|
147
|
+
c.delete(f"/org/members/{member_id}")
|
|
148
|
+
ok(f"removed member {member_id}")
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""`orchard debugger …` — the on-device lldb (lldb-server + attach).
|
|
2
|
+
|
|
3
|
+
Every verb takes a sandbox selector (uuid / prefix / name / tag). `status` is a
|
|
4
|
+
GET; `install` and `start` POST the `/sandboxes/<uuid>/debugger` endpoint with
|
|
5
|
+
an `action`. `console` opens the interactive lldb stream. Needs a rootable image
|
|
6
|
+
for ptrace, so on Cuttlefish use a debug sandbox (the cuttlefish-debug image).
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import typer
|
|
11
|
+
|
|
12
|
+
from . import ptyshell
|
|
13
|
+
from .context import cfg, client
|
|
14
|
+
from .output import emit, fail, ok, table
|
|
15
|
+
from .selector import SelectorError, resolve
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
app = typer.Typer(no_args_is_help=True,
|
|
19
|
+
help="On-device lldb debugger (lldb-server + attach).")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _debugger(ctx: typer.Context, selector: str, *, get: bool = False, **body: object):
|
|
23
|
+
"""Resolve the selector, then GET or POST the /debugger endpoint."""
|
|
24
|
+
with client(ctx) as c:
|
|
25
|
+
try:
|
|
26
|
+
inst = resolve(c, selector)
|
|
27
|
+
except SelectorError as e:
|
|
28
|
+
raise fail(str(e))
|
|
29
|
+
path = f"/sandboxes/{inst['uuid']}/debugger"
|
|
30
|
+
res = c.get(path) if get else c.post(path, json=body)
|
|
31
|
+
return inst, res
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@app.command()
|
|
35
|
+
def status(ctx: typer.Context, selector: str) -> None:
|
|
36
|
+
"""Report lldb-server state on the device."""
|
|
37
|
+
_, res = _debugger(ctx, selector, get=True)
|
|
38
|
+
if cfg(ctx).json:
|
|
39
|
+
return emit(cfg(ctx), res)
|
|
40
|
+
table("debugger", ["field", "value"],
|
|
41
|
+
[[k, res.get(k)] for k in ("installed", "running", "port", "listening", "connect_hint")])
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@app.command()
|
|
45
|
+
def install(ctx: typer.Context, selector: str) -> None:
|
|
46
|
+
"""Push lldb-server to the device (needs root) and start it."""
|
|
47
|
+
inst, res = _debugger(ctx, selector, action="install")
|
|
48
|
+
emit(cfg(ctx), res) if cfg(ctx).json else ok(f"lldb-server install → {inst.get('name')}")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@app.command()
|
|
52
|
+
def start(ctx: typer.Context, selector: str,
|
|
53
|
+
process: str = typer.Option(None, "--process", "-p",
|
|
54
|
+
help="Process to attach to on start.")) -> None:
|
|
55
|
+
"""Start lldb-server, optionally attached to a process."""
|
|
56
|
+
inst, res = _debugger(ctx, selector, action="start", process=process)
|
|
57
|
+
emit(cfg(ctx), res) if cfg(ctx).json else ok(f"lldb-server start → {inst.get('name')}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@app.command()
|
|
61
|
+
def console(ctx: typer.Context, selector: str,
|
|
62
|
+
process: str = typer.Option(None, "--process", "-p",
|
|
63
|
+
help="Process to attach the console to.")) -> None:
|
|
64
|
+
"""Open an interactive lldb console attached to the device (Ctrl-D to exit)."""
|
|
65
|
+
with client(ctx) as c:
|
|
66
|
+
try:
|
|
67
|
+
inst = resolve(c, selector)
|
|
68
|
+
except SelectorError as e:
|
|
69
|
+
raise fail(str(e))
|
|
70
|
+
ptyshell.interactive_console(cfg(ctx), inst["uuid"], process)
|