From 303942ca91bba54d348c6e6db9eb7e2b3c370fde Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 12 Jan 2021 23:31:12 +0100 Subject: [PATCH] get function: added filter, test script modification --- docs/general.html | 56 ++++++++++++++++++++++++++++++++++++++-- docs/gettingstarted.html | 2 +- docs/main.js | 2 +- lib/index.js | 38 ++++++++++++++++++++++++--- test/si.js | 10 +++---- test/test.js | 26 +++++++++---------- 6 files changed, 109 insertions(+), 25 deletions(-) diff --git a/docs/general.html b/docs/general.html index dfb4fd1..ee3e53e 100644 --- a/docs/general.html +++ b/docs/general.html @@ -205,7 +205,7 @@ si.get(valueObject).then(data => console.log(data));

The key names of the valueObject must be exactly the same as the representing function within systeminformation.

Providing parameters to the get() function

-

Now you can also provide parameters to get() functions (where needed). Just pass the parameters in parentheses right after the wanted keys: have a look at the folloging example:

+

Now you can also provide parameters to get() functions (where needed). Just pass the parameters in parentheses right after the wanted keys: have a look at the following example:

@@ -240,7 +240,7 @@ si.get(valueObject).then(data => console.log(data)); console.log(data));
@@ -258,6 +258,58 @@ si.get(valueObject).then(data => console.log(data));
+

Filter results in get() function

+

You can get even further: if the desired result object is an array, you can filter the object to get only the wanted array item: have a look at the following example:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionResult objectLinuxBSDMacWinSunComments
si.get(valueObject,cb){...}XXXXXexample with filter:
add a pipe symbol with the filter definition
to the given function:
+
Example
+
const si = require('systeminformation');
+
+// define all values, you want to get back
+// here after the keys we define a filter (pipe symbol after the keys)
+// to get only one specific item of the result array
+
+valueObject = {
+  networkInterfaces: 'iface, ip4 | iface:en0'
+}
+si.get(valueObject).then(data => console.log(data));
+
+{
+  networkInterfaces: [
+    {
+      iface: 'en0',
+      ip4: '192.168.0.10'
+    }
+  ]
+}
+              
+

Get All At Once

The following three functions si.getStaticData(), si.getDynamicData() and si.getAllData() will return most of the available data in a single result object:

diff --git a/docs/gettingstarted.html b/docs/gettingstarted.html index 3d0e24a..6bab402 100644 --- a/docs/gettingstarted.html +++ b/docs/gettingstarted.html @@ -47,7 +47,7 @@
Quick Start
-

Lightweight collection of 40+ functions to retrieve detailed hardware, system and OS information.

+

Lightweight collection of 45+ functions to retrieve detailed hardware, system and OS information.

  • simple to use
  • get detailed information about system, cpu, baseboard, battery, memory, disks/filesystem, network, docker, software, services and processes
  • diff --git a/docs/main.js b/docs/main.js index 4078ded..6c90ca3 100644 --- a/docs/main.js +++ b/docs/main.js @@ -23,7 +23,7 @@ function createMenu() { [0, '', 'More'], [1, 'security', 'Security Advisories'], [1, 'issues', 'Known Issues'], - [1, 'v5changes', 'Version 5 Changes'], + [1, 'changes', 'Version 5 Changes'], [1, 'history', 'Version history'], [1, 'copyright', 'Copyright & License'], [1, 'contributors', 'Contributors'], diff --git a/lib/index.js b/lib/index.js index 3470e2c..a5e4b2b 100755 --- a/lib/index.js +++ b/lib/index.js @@ -324,7 +324,8 @@ function get(valueObject, callback) { .filter(func => ({}.hasOwnProperty.call(exports, func))) .map(func => { const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')')); - const funcWithoutParams = func.split('(')[0]; + let funcWithoutParams = func.indexOf(')') >= 0 ? func.split(')')[1].trim() : func; + funcWithoutParams = func.indexOf('|') >= 0 ? func.split('|')[0].trim() : funcWithoutParams; if (params) { return exports[funcWithoutParams](params) } else { @@ -340,7 +341,22 @@ function get(valueObject, callback) { if (valueObject[key] === '*' || valueObject[key] === 'all') { result[key] = data[i]; } else { - const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' '); + let keys = valueObject[key]; + // let params = ''; + let filter = ''; + let filterParts = []; + // remove params + if (keys.indexOf(')') >= 0) { + keys = keys.split(')')[1].trim(); + } + // extract filter and remove it from keys + if (keys.indexOf('|') >= 0) { + filter = keys.split('|')[1].trim(); + filterParts = filter.split(':'); + + keys = keys.split('|')[0].trim(); + } + keys = keys.replace(/,/g, ' ').replace(/ +/g, ' ').split(' '); if (data[i]) { if (Array.isArray(data[i])) { // result is in an array, go through all elements of array and pick only the right ones @@ -352,7 +368,23 @@ function get(valueObject, callback) { partialRes[k] = element[k]; } }); - partialArray.push(partialRes); + if (filter && filterParts.length === 2) { + if ({}.hasOwnProperty.call(partialRes, filterParts[0].trim())) { + const val = partialRes[filterParts[0].trim()]; + if (typeof val == 'number') { + if (val === parseFloat(filterParts[1].trim())) { + partialArray.push(partialRes); + } + } else if (typeof val == 'string') { + if (val.toLowerCase() === filterParts[1].trim().toLowerCase()) { + partialArray.push(partialRes); + } + } + } + } else { + partialArray.push(partialRes); + } + }); result[key] = partialArray; } else { diff --git a/test/si.js b/test/si.js index 3d58f58..33f4489 100644 --- a/test/si.js +++ b/test/si.js @@ -34,7 +34,6 @@ function test(f) { else if (f === 'u') { si.usb().then(data => { if (data !== null) { resolve({ data, title: 'USB' }); } else { resolve('not_supported') } }) } else if (f === 'U') { si.uuid().then(data => { if (data !== null) { resolve({ data, title: 'UUID' }); } else { resolve('not_supported') } }) } else if (f === 'v') { si.versions().then(data => { if (data !== null) { resolve({ data, title: 'Versions' }); } else { resolve('not_supported') } }) } - else if (f === 'V') { si.vboxInfo().then(data => { if (data !== null) { resolve({ data, title: 'Virtual Box' }); } else { resolve('not_supported') } }) } else if (f === 'w') { si.wifiNetworks().then(data => { if (data !== null) { resolve({ data, title: 'WIFI Networks' }); } else { resolve('not_supported') } }) } else if (f === 'y') { si.battery().then(data => { if (data !== null) { resolve({ data, title: 'Battery' }); } else { resolve('not_supported') } }) } else if (f === 'z') { si.users().then(data => { if (data !== null) { resolve({ data, title: 'Users' }); } else { resolve('not_supported') } }) } @@ -48,10 +47,11 @@ function test(f) { else if (f === '8') { si.dockerContainerStats('1').then(data => { if (data !== null) { resolve({ data, title: 'Docker Cont Stats' }); } else { resolve('not_supported') } }) } else if (f === '9') { si.dockerContainerProcesses('1').then(data => { if (data !== null) { resolve({ data, title: 'Docker Cont Processes' }); } else { resolve('not_supported') } }) } else if (f === '0') { si.dockerAll().then(data => { if (data !== null) { resolve({ data, title: 'Docker All' }); } else { resolve('not_supported') } }) } - else if (f === '+') { si.getStaticData().then(data => { if (data !== null) { resolve({ data, title: 'All Static Data' }); } else { resolve('not_supported') } }) } - else if (f === '-') { si.getDynamicData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Dynamic Data' }); } else { resolve('not_supported') } }) } - else if (f === '#') { si.getAllData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Data' }); } else { resolve('not_supported') } }) } - else if (f === '.') { + else if (f === '-') { si.vboxInfo().then(data => { if (data !== null) { resolve({ data, title: 'Virtual Box' }); } else { resolve('not_supported') } }) } + else if (f === ',') { si.getStaticData().then(data => { if (data !== null) { resolve({ data, title: 'All Static Data' }); } else { resolve('not_supported') } }) } + else if (f === '.') { si.getDynamicData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Dynamic Data' }); } else { resolve('not_supported') } }) } + else if (f === '/') { si.getAllData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Data' }); } else { resolve('not_supported') } }) } + else if (f === '?') { const valueObject = { cpu: '*', osInfo: 'platform, release', diff --git a/test/test.js b/test/test.js index c4856f0..9ff4049 100644 --- a/test/test.js +++ b/test/test.js @@ -14,19 +14,19 @@ function printHeader() { function printMenu() { console.log(''); - console.log('┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐'); - console.log('│ a ... Audio g ... Graphics p ... Processes w ... WIFI networks 1 ... NET Iface Default │'); - console.log('│ b ... BIOS h ... Bluetooth P ... Process Load y ... Battery 2 ... NET Gateway Default │'); - console.log('│ B ... Baseboard i ... INET Latency r ... Printer z ... Users 3 ... NET Interfaces │'); - console.log('│ C ... Chassis I ... INET Check Site s ... Services 4 ... NET Stats │'); - console.log('│ c ... CPU l ... CPU Load S ... Shell 5 ... NET Connections │'); - console.log('│ d ... DiskLayout L ... Full Load t ... time 6 ... Docker Info │'); - console.log('│ D ... DiskIO n ... T ... CPU Temperature 7 ... Docker Container │'); - console.log('│ e ... Block Devices m ... Memory u ... USB + ... All Static 8 ... Docker Cont Stats │'); - console.log('│ E ... Open Files M ... MEM Layout U ... UUID - ... All Dynamic 9 ... Docker Cont Proc │'); - console.log('│ f ... FS Size o ... OS Info v ... Versions # ... All 0 ... Docker All │'); - console.log('│ F ... FS Stats O ... V ... VirtualBox . ... Get Object q >>> Quit │'); - console.log('└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘'); + console.log('┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐'); + console.log('│ a ... Audio g ... Graphics r ... Printer 1 ... NET Iface Default ? ... Get Object │'); + console.log('│ b ... BIOS h ... Bluetooth s ... Services 2 ... NET Gateway Default , ... All Static │'); + console.log('│ B ... Baseboard i ... INET Latency S ... Shell 3 ... NET Interfaces . ... All Dynamic │'); + console.log('│ C ... Chassis I ... INET Check Site t ... time 4 ... NET Stats / ... All │'); + console.log('│ c ... CPU l ... CPU Load T ... CPU Temperature 5 ... NET Connections │'); + console.log('│ d ... DiskLayout L ... Full Load u ... USB 6 ... Docker Info │'); + console.log('│ D ... DiskIO m ... Memory U ... UUID 7 ... Docker Container │'); + console.log('│ e ... Block Devices M ... MEM Layout v ... Versions 8 ... Docker Cont Stats │'); + console.log('│ E ... Open Files o ... OS Info w ... WIFI networks 9 ... Docker Cont Proc │'); + console.log('│ f ... FS Size p ... Processes y ... Battery 0 ... Docker All │'); + console.log('│ F ... FS Stats P ... Process Load z ... Users - ... Virtual Box q >>> QUIT │'); + console.log('└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘'); } function EnableUserInput() {