Update DAB to include dynamic settings
- Settings will now get the active device(s) available on Linux (UNTESTED) - Added CLI handler for interacting with the CLI - Added wrapper for extracting values with regex -
This commit is contained in:
@@ -61,4 +61,17 @@ export const generateUniqueID = () => {
|
||||
.digest('hex');
|
||||
|
||||
return uniqueID;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the value after a specific pattern from a string using regular expressions.
|
||||
* @param {string} input - The input string.
|
||||
* @param {string} pattern - The pattern to match.
|
||||
* @returns {string|null} The value found after the pattern, or null if not found.
|
||||
*/
|
||||
export const extractValue = (input, pattern) => {
|
||||
const regex = new RegExp(`${pattern}\\s+(\\S+)`);
|
||||
const match = input.match(regex);
|
||||
return match ? match[1] : null;
|
||||
};
|
||||
39
client/modules/cliHandler.mjs
Normal file
39
client/modules/cliHandler.mjs
Normal file
@@ -0,0 +1,39 @@
|
||||
import { spawn } from "child_process";
|
||||
|
||||
/**
|
||||
* Executes a command and retrieves its output.
|
||||
* @param {string} command - The command to execute.
|
||||
* @param {string[]} args - The arguments to pass to the command.
|
||||
* @returns {Promise<string>} A promise that resolves with the output of the command.
|
||||
*/
|
||||
export const executeCommand = (command, args) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const childProcess = spawn(command, args);
|
||||
|
||||
let commandOutput = '';
|
||||
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
commandOutput += data.toString();
|
||||
});
|
||||
|
||||
childProcess.stderr.on('data', (data) => {
|
||||
// Log any errors to stderr
|
||||
console.error(data.toString());
|
||||
});
|
||||
|
||||
childProcess.on('error', (error) => {
|
||||
// Reject the promise if there's an error executing the command
|
||||
reject(error);
|
||||
});
|
||||
|
||||
childProcess.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
// Resolve the promise with the command output if it exits successfully
|
||||
resolve(commandOutput.trim());
|
||||
} else {
|
||||
// Reject the promise if the command exits with a non-zero code
|
||||
reject(new Error(`Command '${command}' exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user