Report CLI errors as exit codes

master
Vitaliy Filippov 2024-05-09 13:48:35 +03:00
parent 1734b422cb
commit 5aa81a5c45
1 changed files with 29 additions and 11 deletions

View File

@ -83,6 +83,7 @@ class AntiEtcdCli
{
await this.del(cmd.slice(1));
}
process.exit(0);
}
async get(keys)
@ -93,7 +94,7 @@ class AntiEtcdCli
}
const txn = { success: keys.map(key => ({ request_range: this.options.prefix ? { key: b64(key+'/'), range_end: b64(key+'0') } : { key: b64(key) } })) };
const res = await this.request('/v3/kv/txn', txn);
for (const r of (res||{}).responses||[])
for (const r of res.responses||[])
{
if (r.response_range)
{
@ -119,7 +120,7 @@ class AntiEtcdCli
value = await fsp.readFile(0, { encoding: 'utf-8' });
}
const res = await this.request('/v3/kv/put', { key: b64(key), value: b64(value) });
if (res && res.header)
if (res.header)
{
console.log('OK');
}
@ -133,7 +134,7 @@ class AntiEtcdCli
}
const txn = { success: keys.map(key => ({ request_delete_range: this.options.prefix ? { key: b64(key+'/'), range_end: b64(key+'0') } : { key: b64(key) } })) };
const res = await this.request('/v3/kv/txn', txn);
for (const r of (res||{}).responses||[])
for (const r of res.responses||[])
{
if (r.response_delete_range)
{
@ -147,16 +148,33 @@ class AntiEtcdCli
for (const url of this.options.endpoints)
{
const cur_url = url.replace(/\/+$/, '')+path;
try
const res = await POST(cur_url, body, this.options.timeout||1000);
let ok = false;
if (res.json)
{
return (await POST(cur_url, body, this.options.timeout||1000)).json;
if (res.json.error)
{
console.error(cur_url+': '+res.json.error);
process.exit(1);
}
return res.json;
}
catch (e)
if (res.body)
{
console.error(cur_url+': '+e.message);
console.error(cur_url+': '+res.body);
}
if (res.error)
{
console.error(cur_url+': '+res.error);
if (!res.response || !res.response.statusCode)
{
// This URL is unavailable
continue;
}
}
break;
}
return null;
process.exit(1);
}
}
@ -188,9 +206,9 @@ function POST(url, body, timeout)
res.on('data', chunk => { res_body += chunk; });
res.on('end', () =>
{
if (res.statusCode != 200)
if (res.statusCode != 200 || !/application\/json/i.exec(res.headers['content-type']))
{
ok({ error: res_body, code: res.statusCode });
ok({ response: res, body: res_body, code: res.statusCode });
return;
}
try
@ -200,7 +218,7 @@ function POST(url, body, timeout)
}
catch (e)
{
ok({ error: e, response: res, body: res_body });
ok({ response: res, error: e, body: res_body });
}
});
});