Skip to content

Commit 0ba1559

Browse files
committed
Added CLI to the script app
1 parent 4f8ff51 commit 0ba1559

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

agent/apps/ztm/proxy/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function ({ api, utils }) {
1717
}
1818

1919
function flush() {
20-
return [buffer, new StreamEnd]
20+
return Promise.resolve([buffer, new StreamEnd])
2121
}
2222

2323
try {
@@ -87,7 +87,7 @@ export default function ({ api, utils }) {
8787

8888
} catch (err) {
8989
error(err)
90-
return Promise.resolve(flush())
90+
return flush()
9191
}
9292
}
9393
}

agent/apps/ztm/script/cli.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,60 @@ export default function ({ app, api, utils }) {
22
var $handler
33

44
return pipeline($=>$
5-
.onStart(ctx => main(ctx).then(h => void ($handler = h)))
6-
.pipe(() => $handler)
5+
.onStart(ctx => main(ctx))
76
)
87

9-
function main({ argv, endpoint }) {
8+
function main({ argv, cwd, endpoint }) {
109
var buffer = new Data
1110

1211
function output(str) {
1312
buffer.push(str)
1413
}
1514

15+
function flush() {
16+
return Promise.resolve([buffer, new StreamEnd])
17+
}
18+
1619
function error(err) {
1720
output('ztm: ')
1821
output(err.message || err.toString())
1922
output('\n')
23+
return flush()
2024
}
2125

22-
function flush() {
23-
return Promise.resolve(
24-
pipeline($=>$
25-
.replaceStreamStart([buffer, new StreamEnd])
26-
)
27-
)
28-
}
26+
try {
27+
return utils.parseArgv(['ztm proxy', ...argv], {
28+
help: text => Promise.resolve(output(text + '\n')),
29+
commands: [
30+
{
31+
title: 'Execute a script on a remote endpoint',
32+
usage: '<filename>',
33+
options: `
34+
--, --args ... Pass all options afterwards to the script
35+
`,
36+
action: (args) => {
37+
var pathname = os.path.resolve(cwd, args['<filename>'])
38+
var script = os.read(pathname)
39+
var argv = args['--args'] || []
40+
return pipeline($=>$
41+
.onStart(new Message(
42+
{
43+
method: 'POST',
44+
path: `/api/script?argv=${URL.encodeComponent(JSON.stringify(argv))}`
45+
},
46+
script
47+
))
48+
.pipe(api.executeScriptRemote, () => endpoint.id)
49+
.replaceMessage(res => {
50+
output(res?.body || new Data)
51+
return new StreamEnd
52+
})
53+
).spawn()
54+
}
55+
}
56+
]
57+
}).then(flush).catch(error)
2958

30-
return flush()
59+
} catch (err) { return error(err) }
3160
}
3261
}

agent/cmdline.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function parseOptions(format, argv) {
131131
if (t.endsWith(',')) t = t.substring(0, t.length - 1)
132132
aliases.push(t)
133133
} else {
134-
if (t.endsWith('...]') || t.endsWith('...>')) type = 'array'
134+
if (t === '...') type = 'remainder array'
135+
else if (t.endsWith('...]') || t.endsWith('...>')) type = 'array'
135136
else if (t.startsWith('[')) type = 'optional string'
136137
else if (t.startsWith('<')) type = 'string'
137138
else type = 'boolean'
@@ -147,6 +148,10 @@ function parseOptions(format, argv) {
147148

148149
argv.forEach(arg => {
149150
if (currentOption) {
151+
if (options[currentOption]?.type === 'remainder array') {
152+
addOption(currentOption, arg)
153+
return
154+
}
150155
if (arg.startsWith('-')) {
151156
endOption(currentOption)
152157
currentOption = undefined
@@ -182,6 +187,7 @@ function parseOptions(format, argv) {
182187
option.value = value
183188
break
184189
case 'array':
190+
case 'remainder array':
185191
option.value ??= []
186192
option.value.push(value)
187193
break

agent/main.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,15 @@ pipy.listen(listen, $=>$
463463
}
464464
),
465465
'app': ($=>$
466-
.muxHTTP(() => {
467-
$appPipeline = api.connectApp(
468-
$params.mesh,
469-
$params.provider,
470-
$params.app,
471-
) || appNotFound
472-
return $appPipeline
473-
}, { version: 2 }).to($=>$
474-
.pipe(() => $appPipeline, () => ({ source: 'user' }))
466+
.muxHTTP().to($=>$
467+
.pipe(
468+
() => api.connectApp(
469+
$params.mesh,
470+
$params.provider,
471+
$params.app,
472+
) || appNotFound,
473+
() => ({ source: 'user' })
474+
)
475475
)
476476
),
477477
'gui': $=>$.replaceMessage(

agent/service.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function cliResponder(p) {
4343
var url = new URL(req.head.path)
4444
$cliCtx = {
4545
argv: JSON.parse(URL.decodeComponent(url.searchParams.get('argv'))),
46+
cwd: URL.decodeComponent(url.searchParams.get('cwd')),
4647
endpoint: {
4748
id: url.searchParams.get('ep_id'),
4849
name: URL.decodeComponent(url.searchParams.get('ep_name')),

cli/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ function callApp(argv, mesh, ep) {
10561056

10571057
var url = `/api/meshes/${mesh.name}/apps/${app.provider}/${tagname}/cli`
10581058
url += '?argv=' + URL.encodeComponent(JSON.stringify(argv))
1059+
url += '&cwd=' + URL.encodeComponent(os.path.resolve())
10591060
url += '&ep_id=' + ep.id
10601061
url += '&ep_name=' + URL.encodeComponent(ep.name)
10611062

0 commit comments

Comments
 (0)