insomnia/plugins/insomnia-plugin-os/index.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-12-22 22:10:38 +00:00
const os = require('os');
const { JSONPath } = require('jsonpath-plus');
2017-12-22 22:10:38 +00:00
2018-06-25 17:42:50 +00:00
const FILTERABLE = ['userInfo', 'cpus'];
2017-12-22 22:10:38 +00:00
2018-06-25 17:42:50 +00:00
module.exports.templateTags = [
{
displayName: 'OS',
name: 'os',
description: 'get OS info',
args: [
{
displayName: 'Function',
type: 'enum',
options: [
{ displayName: 'arch', value: 'arch' },
{ displayName: 'cpus', value: 'cpus' },
{ displayName: 'freemem', value: 'freemem' },
{ displayName: 'hostname', value: 'hostname' },
{ displayName: 'platform', value: 'platform' },
{ displayName: 'release', value: 'release' },
{ displayName: 'userInfo', value: 'userInfo' },
],
2018-06-25 17:42:50 +00:00
},
{
displayName: 'JSONPath Filter',
2018-10-17 16:42:33 +00:00
help: 'Some OS functions return objects. Use JSONPath queries to extract desired values.',
2018-06-25 17:42:50 +00:00
hide: args => !FILTERABLE.includes(args[0].value),
type: 'string',
},
2018-06-25 17:42:50 +00:00
],
run(context, fnName, filter) {
let value = os[fnName]();
2017-12-22 22:10:38 +00:00
if (JSONPath && FILTERABLE.includes(fnName)) {
2018-06-25 17:42:50 +00:00
try {
value = JSONPath({ json: value, path: filter })[0];
2018-06-25 17:42:50 +00:00
} catch (err) {}
2017-12-22 22:10:38 +00:00
}
2018-06-25 17:42:50 +00:00
if (typeof value !== 'string') {
return JSON.stringify(value);
} else {
return value;
}
},
},
2018-06-25 17:42:50 +00:00
];