From cb799ae8b71372a53b050f3d74ff88b78bd4d4d6 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 27 Jul 2026 18:09:50 +0100 Subject: [PATCH] Add command line handling for server status and authentication - Introduce a new command line utility in commandline.js to manage server status checks and OTP authentication. - Refactor index.js to utilize the new command line handling, improving clarity and separation of concerns. - Enhance localserver.js to include connection status in the server response, providing more detailed information about the server state. --- src/commandline.js | 96 ++++++++++++++++++++++++++++++++++ src/index.js | 84 +++-------------------------- src/localserver/localserver.js | 1 + 3 files changed, 103 insertions(+), 78 deletions(-) create mode 100644 src/commandline.js diff --git a/src/commandline.js b/src/commandline.js new file mode 100644 index 0000000..f5ec082 --- /dev/null +++ b/src/commandline.js @@ -0,0 +1,96 @@ +import { + authenticateWithOtp, + checkRunning, + isInfo, + otpCode, + printAuthCommand, + printerHostInfo, + startWaiting, + stopWaiting, +} from "./commandlineutils.js"; + +export async function handleCommandLine(cli, logger) { + const isCommandLines = otpCode != undefined || isInfo == true; + let runningServer; + try { + startWaiting("Checking if Farm Control Server is running...", logger); + runningServer = await checkRunning(); + await stopWaiting(); + if (runningServer == false && isCommandLines == true) { + cli.failure("Farm Control Server is not running."); + cli.blank(); + return { handled: true, result: false }; + } + } catch (err) { + await stopWaiting(); + cli.failure("Failed to check if Farm Control Server is running:", err); + cli.blank(); + return { handled: true, result: false }; + } + + if (isCommandLines == true) { + cli.success("Farm Control Server is running."); + } + + if (isInfo) { + const buildNumber = + runningServer.buildNumber ?? runningServer.build ?? "n/a"; + cli.blank(); + cli.sectionHeader("FarmControl Server"); + cli.label( + "Version:", + runningServer.version ? `v${runningServer.version}` : "n/a", + ); + cli.label("Build:", buildNumber === "dev" ? "dev" : `b${buildNumber}`); + cli.label("Connected:", cli.yesNo(runningServer.connected == true)); + cli.label("Authenticated:", cli.yesNo(runningServer.authenticated == true)); + cli.sectionFooter(); + cli.blank(); + if ( + runningServer.authenticated == false && + runningServer.connected == true + ) { + cli.sectionHeader("Authenticate"); + printAuthCommand(cli); + cli.sectionFooter(); + } else if ( + runningServer.connected == true && + runningServer.authenticated == true + ) { + cli.sectionHeader("Host"); + printerHostInfo(runningServer.host, cli); + cli.sectionFooter(); + } + cli.blank(); + return { handled: true, result: runningServer }; + } + + if (otpCode != undefined) { + cli.blank(); + startWaiting("Authenticating with OTP...", logger); + let authenticatedServer; + try { + authenticatedServer = await authenticateWithOtp(otpCode); + await stopWaiting(); + } catch (err) { + await stopWaiting(); + throw err; + } + if (authenticatedServer.valid == false) { + cli.failure("Failed to authenticate!"); + cli.failure(authenticatedServer.error); + } else { + cli.success("Authenticated with OTP."); + cli.blank(); + cli.sectionHeader("Host"); + printerHostInfo(authenticatedServer.host, cli); + cli.sectionFooter(); + cli.blank(); + return { handled: true, result: authenticatedServer }; + } + cli.blank(); + return { handled: true, result: authenticatedServer }; + } + + return { handled: false, runningServer }; +} diff --git a/src/index.js b/src/index.js index b827987..f2a84f2 100644 --- a/src/index.js +++ b/src/index.js @@ -7,17 +7,12 @@ import { setupIPC } from "./electron/ipc.js"; import { LocalServer } from "./localserver/localserver.js"; import { SocketClient } from "./socket/socketclient.js"; import { - authenticateWithOtp, - checkRunning, createCliLogger, isHeadless, - isInfo, - otpCode, - printAuthCommand, - printerHostInfo, startWaiting, stopWaiting, } from "./commandlineutils.js"; +import { handleCommandLine } from "./commandline.js"; if (!process.env.NODE_ENV) { process.env.NODE_ENV = "production"; @@ -33,80 +28,13 @@ const cli = createCliLogger(logger); export async function init(options = {}) { const headless = options.headless ?? isHeadless; cli.blank(); - startWaiting("Checking if Farm Control Server is running...", logger); - const isCommandLines = otpCode != undefined || isInfo == true; - let runningServer; - try { - runningServer = await checkRunning(); - await stopWaiting(); - if (runningServer == false && isCommandLines == true) { - cli.failure("Farm Control Server is not running."); - cli.blank(); - return false; - } - } catch (err) { - await stopWaiting(); - cli.failure("Failed to check if Farm Control Server is running:", err); - cli.blank(); - return false; + + const commandLineResult = await handleCommandLine(cli, logger); + if (commandLineResult.handled) { + return commandLineResult.result; } - if (isCommandLines == true) { - cli.success("Farm Control Server is running."); - } - - if (isInfo) { - const buildNumber = - runningServer.buildNumber ?? runningServer.build ?? "n/a"; - cli.blank(); - cli.sectionHeader("FarmControl Server"); - cli.label( - "Version:", - runningServer.version ? `v${runningServer.version}` : "n/a", - ); - cli.label("Build:", buildNumber === "dev" ? "dev" : `b${buildNumber}`); - cli.label("Authenticated:", cli.yesNo(runningServer.authenticated == true)); - cli.sectionFooter(); - cli.blank(); - if (runningServer.authenticated == false) { - cli.sectionHeader("Authenticate"); - printAuthCommand(cli); - cli.sectionFooter(); - } else { - cli.sectionHeader("Host"); - printerHostInfo(runningServer.host, cli); - cli.sectionFooter(); - } - cli.blank(); - return runningServer; - } - - if (otpCode != undefined) { - cli.blank(); - startWaiting("Authenticating with OTP...", logger); - let authenticatedServer; - try { - authenticatedServer = await authenticateWithOtp(otpCode); - await stopWaiting(); - } catch (err) { - await stopWaiting(); - throw err; - } - if (authenticatedServer.valid == false) { - cli.failure("Failed to authenticate!"); - cli.failure(authenticatedServer.error); - } else { - cli.success("Authenticated with OTP."); - cli.blank(); - cli.sectionHeader("Host"); - printerHostInfo(authenticatedServer.host, cli); - cli.sectionFooter(); - cli.blank(); - return authenticatedServer; - } - cli.blank(); - return authenticatedServer; - } + const runningServer = commandLineResult.runningServer; if (runningServer != false) { cli.warn("✘ Farm Control Server is already running."); diff --git a/src/localserver/localserver.js b/src/localserver/localserver.js index c3021cc..e62c598 100644 --- a/src/localserver/localserver.js +++ b/src/localserver/localserver.js @@ -27,6 +27,7 @@ export class LocalServer { res.json({ running: true, authenticated: this.socketClient.authenticated, + connected: this.socketClient.connected, host: this.socketClient.host, ...getServerVersionInfo(), });