fix(striker-ui-api): simplify local host name/uuid getter

main
Tsu-ba-me 2 years ago
parent 448a956313
commit b0e7859d2d
  1. 28
      striker-ui-api/src/lib/accessModule.ts
  2. 4
      striker-ui-api/src/lib/consts/SERVER_PATHS.ts
  3. 4
      striker-ui-api/src/lib/consts/index.ts

@ -1,6 +1,7 @@
import { spawnSync, SpawnSyncOptions } from 'child_process'; 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'; import { stderr as sherr, stdout as shout } from './shell';
@ -8,15 +9,18 @@ const formatQuery = (query: string) => query.replace(/\s+/g, ' ');
const execAnvilAccessModule = ( const execAnvilAccessModule = (
args: string[], args: string[],
options: SpawnSyncOptions = { options: SpawnSyncOptions = {},
encoding: 'utf-8',
timeout: 10000,
},
) => { ) => {
const {
encoding = 'utf-8',
timeout = 10000,
...restSpawnSyncOptions
} = options;
const { error, stdout, stderr } = spawnSync( const { error, stdout, stderr } = spawnSync(
SERVER_PATHS.usr.sbin['anvil-access-module'].self, SERVER_PATHS.usr.sbin['anvil-access-module'].self,
args, args,
options, { encoding, timeout, ...restSpawnSyncOptions },
); );
if (error) { if (error) {
@ -152,9 +156,9 @@ const getLocalHostName = () => {
let result: string; let result: string;
try { try {
result = execModuleSubroutine('host_name', { result = readFileSync(SERVER_PATHS.etc.hostname.self, {
subModuleName: 'Get', encoding: 'utf-8',
}).stdout; }).trim();
} catch (subError) { } catch (subError) {
throw new Error(`Failed to get local host name; CAUSE: ${subError}`); throw new Error(`Failed to get local host name; CAUSE: ${subError}`);
} }
@ -168,9 +172,9 @@ const getLocalHostUUID = () => {
let result: string; let result: string;
try { try {
result = execModuleSubroutine('host_uuid', { result = readFileSync(SERVER_PATHS.etc.anvil['host.uuid'].self, {
subModuleName: 'Get', encoding: 'utf-8',
}).stdout; }).trim();
} catch (subError) { } catch (subError) {
throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`); throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`);
} }

@ -1,6 +1,10 @@
import path from 'path'; import path from 'path';
const EMPTY_SERVER_PATHS: ServerPath = { const EMPTY_SERVER_PATHS: ServerPath = {
etc: {
anvil: { 'host.uuid': {} },
hostname: {},
},
mnt: { mnt: {
shared: { shared: {
incoming: {}, incoming: {},

@ -1,3 +1,7 @@
import SERVER_PATHS from './SERVER_PATHS';
export { SERVER_PATHS };
export * from './AN_VARIABLE_NAME_LIST'; export * from './AN_VARIABLE_NAME_LIST';
export * from './EXIT_CODE_LIST'; export * from './EXIT_CODE_LIST';
export * from './PROCESS_OWNER'; export * from './PROCESS_OWNER';

Loading…
Cancel
Save