From b0e7859d2dfd0226449d790ab5e0391e2c9b12c3 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Mon, 17 Apr 2023 18:18:01 -0400 Subject: [PATCH] fix(striker-ui-api): simplify local host name/uuid getter --- striker-ui-api/src/lib/accessModule.ts | 28 +++++++++++-------- striker-ui-api/src/lib/consts/SERVER_PATHS.ts | 4 +++ striker-ui-api/src/lib/consts/index.ts | 4 +++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/striker-ui-api/src/lib/accessModule.ts b/striker-ui-api/src/lib/accessModule.ts index cd406ff2..3a252bc3 100644 --- a/striker-ui-api/src/lib/accessModule.ts +++ b/striker-ui-api/src/lib/accessModule.ts @@ -1,6 +1,7 @@ import { spawnSync, SpawnSyncOptions } from 'child_process'; +import { readFileSync } from 'fs'; -import SERVER_PATHS from './consts/SERVER_PATHS'; +import { SERVER_PATHS } from './consts'; import { stderr as sherr, stdout as shout } from './shell'; @@ -8,15 +9,18 @@ const formatQuery = (query: string) => query.replace(/\s+/g, ' '); const execAnvilAccessModule = ( args: string[], - options: SpawnSyncOptions = { - encoding: 'utf-8', - timeout: 10000, - }, + options: SpawnSyncOptions = {}, ) => { + const { + encoding = 'utf-8', + timeout = 10000, + ...restSpawnSyncOptions + } = options; + const { error, stdout, stderr } = spawnSync( SERVER_PATHS.usr.sbin['anvil-access-module'].self, args, - options, + { encoding, timeout, ...restSpawnSyncOptions }, ); if (error) { @@ -152,9 +156,9 @@ const getLocalHostName = () => { let result: string; try { - result = execModuleSubroutine('host_name', { - subModuleName: 'Get', - }).stdout; + result = readFileSync(SERVER_PATHS.etc.hostname.self, { + encoding: 'utf-8', + }).trim(); } catch (subError) { throw new Error(`Failed to get local host name; CAUSE: ${subError}`); } @@ -168,9 +172,9 @@ const getLocalHostUUID = () => { let result: string; try { - result = execModuleSubroutine('host_uuid', { - subModuleName: 'Get', - }).stdout; + result = readFileSync(SERVER_PATHS.etc.anvil['host.uuid'].self, { + encoding: 'utf-8', + }).trim(); } catch (subError) { throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`); } diff --git a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts index f6428321..2d089eef 100644 --- a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts +++ b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts @@ -1,6 +1,10 @@ import path from 'path'; const EMPTY_SERVER_PATHS: ServerPath = { + etc: { + anvil: { 'host.uuid': {} }, + hostname: {}, + }, mnt: { shared: { incoming: {}, diff --git a/striker-ui-api/src/lib/consts/index.ts b/striker-ui-api/src/lib/consts/index.ts index e5746d3f..ee112bfe 100644 --- a/striker-ui-api/src/lib/consts/index.ts +++ b/striker-ui-api/src/lib/consts/index.ts @@ -1,3 +1,7 @@ +import SERVER_PATHS from './SERVER_PATHS'; + +export { SERVER_PATHS }; + export * from './AN_VARIABLE_NAME_LIST'; export * from './EXIT_CODE_LIST'; export * from './PROCESS_OWNER';