@linpo/linpo-mount-cli 0.0.1
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.
- package/bin/linpo-mount.js +99 -0
- package/linpo-mount-cli-0.0.1.tgz +0 -0
- package/package.json +12 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execSync } from 'node:child_process';
|
|
3
|
+
|
|
4
|
+
function parseArgs(argv) {
|
|
5
|
+
const out = {};
|
|
6
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
7
|
+
const k = argv[i];
|
|
8
|
+
if (!k.startsWith('--')) continue;
|
|
9
|
+
const key = k.slice(2);
|
|
10
|
+
const val = argv[i + 1] && !argv[i + 1].startsWith('--') ? argv[++i] : 'true';
|
|
11
|
+
out[key] = val;
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function run(cmd) {
|
|
17
|
+
try {
|
|
18
|
+
return execSync(cmd, { stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf8' }).trim();
|
|
19
|
+
} catch {
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function tokenFromDashboard() {
|
|
25
|
+
const raw = run('openclaw dashboard --no-open');
|
|
26
|
+
const m = raw.match(/#token=([^\s]+)/);
|
|
27
|
+
return m ? m[1] : '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getToken() {
|
|
31
|
+
const envToken = (process.env.OPENCLAW_GATEWAY_TOKEN || '').trim();
|
|
32
|
+
if (envToken) return envToken;
|
|
33
|
+
const t1 = run('openclaw config get gateway.auth.token');
|
|
34
|
+
if (t1) return t1;
|
|
35
|
+
const t2 = run('openclaw config get gateway.token');
|
|
36
|
+
if (t2) return t2;
|
|
37
|
+
return tokenFromDashboard();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getEndpoint(override) {
|
|
41
|
+
if (override) return override;
|
|
42
|
+
const bind = run('openclaw config get gateway.bind') || 'loopback';
|
|
43
|
+
const port = run('openclaw config get gateway.port') || '18789';
|
|
44
|
+
if (bind === 'loopback') return `ws://127.0.0.1:${port}`;
|
|
45
|
+
const ip = (run('hostname -i') || '127.0.0.1').split(/\s+/)[0];
|
|
46
|
+
return `ws://${ip}:${port}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function main() {
|
|
50
|
+
const args = parseArgs(process.argv.slice(2));
|
|
51
|
+
const shortCode = args['short-code'] || '';
|
|
52
|
+
const attachApi = args['attach-api'] || '';
|
|
53
|
+
const name = args['name'] || 'claw-session-mount';
|
|
54
|
+
const endpoint = getEndpoint(args['endpoint'] || '');
|
|
55
|
+
const token = getToken();
|
|
56
|
+
|
|
57
|
+
if (!shortCode || !attachApi) {
|
|
58
|
+
console.error('usage: linpo-mount --short-code <code> --attach-api <url> [--name <name>] [--endpoint <ws://...>]');
|
|
59
|
+
process.exit(2);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!token) {
|
|
63
|
+
console.log(`ENDPOINT=${endpoint}`);
|
|
64
|
+
console.log('TOKEN_LEN=0');
|
|
65
|
+
console.log('CURL_RESULT={"ok":false,"stage":"token","error":"token empty"}');
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let result = null;
|
|
70
|
+
let httpStatus = 0;
|
|
71
|
+
try {
|
|
72
|
+
const resp = await fetch(attachApi, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: { 'Content-Type': 'application/json' },
|
|
75
|
+
body: JSON.stringify({ shortCode, name, endpoint, gatewayToken: token }),
|
|
76
|
+
});
|
|
77
|
+
httpStatus = resp.status;
|
|
78
|
+
const text = await resp.text();
|
|
79
|
+
try {
|
|
80
|
+
result = JSON.parse(text);
|
|
81
|
+
} catch {
|
|
82
|
+
result = { ok: false, stage: 'attach', raw: text };
|
|
83
|
+
}
|
|
84
|
+
} catch (err) {
|
|
85
|
+
result = { ok: false, stage: 'network', error: String(err && err.message ? err.message : err) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
console.log(`ENDPOINT=${endpoint}`);
|
|
89
|
+
console.log(`TOKEN_LEN=${token.length}`);
|
|
90
|
+
console.log(`CURL_RESULT=${JSON.stringify({ ...result, httpStatus })}`);
|
|
91
|
+
|
|
92
|
+
const status = result && typeof result === 'object' ? result.status : '';
|
|
93
|
+
if (httpStatus >= 200 && httpStatus < 300 && (status === 'bound' || status === 'attached')) {
|
|
94
|
+
process.exit(0);
|
|
95
|
+
}
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
main();
|
|
Binary file
|
package/package.json
ADDED