Repository navigation
Expand file tree
/
Copy pathqr-cli.ts
More file actions
183 lines (167 loc) 路 6.18 KB
/
Copy pathqr-cli.ts
File metadata and controls
183 lines (167 loc) 路 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { Command } from "commander";
import qrcode from "qrcode-terminal";
import { loadConfig } from "../config/config.js";
import { resolvePairingSetupFromConfig, encodePairingSetupCode } from "../pairing/setup-code.js";
import { runCommandWithTimeout } from "../process/exec.js";
import { defaultRuntime } from "../runtime.js";
import { theme } from "../terminal/theme.js";
type QrCliOptions = {
json?: boolean;
setupCodeOnly?: boolean;
ascii?: boolean;
remote?: boolean;
url?: string;
publicUrl?: string;
token?: string;
password?: string;
};
function renderQrAscii(data: string): Promise<string> {
return new Promise((resolve) => {
qrcode.generate(data, { small: true }, (output: string) => {
resolve(output);
});
});
}
function readDevicePairPublicUrlFromConfig(cfg: ReturnType<typeof loadConfig>): string | undefined {
const value = cfg.plugins?.entries?.["device-pair"]?.config?.["publicUrl"];
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
export function registerQrCli(program: Command) {
program
.command("qr")
.description("Generate an iOS pairing QR code and setup code")
.option(
"--remote",
"Use gateway.remote.url and gateway.remote token/password (ignores device-pair publicUrl)",
false,
)
.option("--url <url>", "Override gateway URL used in the setup payload")
.option("--public-url <url>", "Override gateway public URL used in the setup payload")
.option("--token <token>", "Override gateway token for setup payload")
.option("--password <password>", "Override gateway password for setup payload")
.option("--setup-code-only", "Print only the setup code", false)
.option("--no-ascii", "Skip ASCII QR rendering")
.option("--json", "Output JSON", false)
.action(async (opts: QrCliOptions) => {
try {
if (opts.token && opts.password) {
throw new Error("Use either --token or --password, not both.");
}
const loaded = loadConfig();
const cfg = {
...loaded,
gateway: {
...loaded.gateway,
auth: {
...loaded.gateway?.auth,
},
},
};
const token = typeof opts.token === "string" ? opts.token.trim() : "";
const password = typeof opts.password === "string" ? opts.password.trim() : "";
const wantsRemote = opts.remote === true;
if (token) {
cfg.gateway.auth.mode = "token";
cfg.gateway.auth.token = token;
}
if (password) {
cfg.gateway.auth.mode = "password";
cfg.gateway.auth.password = password;
}
if (wantsRemote && !token && !password) {
const remoteToken =
typeof cfg.gateway?.remote?.token === "string" ? cfg.gateway.remote.token.trim() : "";
const remotePassword =
typeof cfg.gateway?.remote?.password === "string"
? cfg.gateway.remote.password.trim()
: "";
if (remoteToken) {
cfg.gateway.auth.mode = "token";
cfg.gateway.auth.token = remoteToken;
cfg.gateway.auth.password = undefined;
} else if (remotePassword) {
cfg.gateway.auth.mode = "password";
cfg.gateway.auth.password = remotePassword;
cfg.gateway.auth.token = undefined;
}
}
if (wantsRemote && !opts.url && !opts.publicUrl) {
const tailscaleMode = cfg.gateway?.tailscale?.mode ?? "off";
const remoteUrl = cfg.gateway?.remote?.url;
const hasRemoteUrl = typeof remoteUrl === "string" && remoteUrl.trim().length > 0;
const hasTailscaleServe = tailscaleMode === "serve" || tailscaleMode === "funnel";
if (!hasRemoteUrl && !hasTailscaleServe) {
throw new Error(
"qr --remote requires gateway.remote.url (or gateway.tailscale.mode=serve/funnel).",
);
}
}
const explicitUrl =
typeof opts.url === "string" && opts.url.trim()
? opts.url.trim()
: typeof opts.publicUrl === "string" && opts.publicUrl.trim()
? opts.publicUrl.trim()
: undefined;
const publicUrl =
explicitUrl ?? (wantsRemote ? undefined : readDevicePairPublicUrlFromConfig(cfg));
const resolved = await resolvePairingSetupFromConfig(cfg, {
publicUrl,
preferRemoteUrl: wantsRemote,
runCommandWithTimeout: async (argv, runOpts) =>
await runCommandWithTimeout(argv, {
timeoutMs: runOpts.timeoutMs,
}),
});
if (!resolved.ok) {
throw new Error(resolved.error);
}
const setupCode = encodePairingSetupCode(resolved.payload);
if (opts.setupCodeOnly) {
defaultRuntime.log(setupCode);
return;
}
if (opts.json) {
defaultRuntime.log(
JSON.stringify(
{
setupCode,
gatewayUrl: resolved.payload.url,
auth: resolved.authLabel,
urlSource: resolved.urlSource,
},
null,
2,
),
);
return;
}
const lines: string[] = [
theme.heading("Pairing QR"),
"Scan this with the OpenClaw iOS app (Onboarding -> Scan QR).",
"",
];
if (opts.ascii !== false) {
const qrAscii = await renderQrAscii(setupCode);
lines.push(qrAscii.trimEnd(), "");
}
lines.push(
`${theme.muted("Setup code:")} ${setupCode}`,
`${theme.muted("Gateway:")} ${resolved.payload.url}`,
`${theme.muted("Auth:")} ${resolved.authLabel}`,
`${theme.muted("Source:")} ${resolved.urlSource}`,
"",
"Approve after scan with:",
` ${theme.command("openclaw devices list")}`,
` ${theme.command("openclaw devices approve <requestId>")}`,
);
defaultRuntime.log(lines.join("\n"));
} catch (err) {
defaultRuntime.error(String(err));
defaultRuntime.exit(1);
}
});
}