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.
This commit is contained in:
Tom Butcher 2026-07-27 18:09:50 +01:00
parent 9a68358432
commit cb799ae8b7
3 changed files with 103 additions and 78 deletions

96
src/commandline.js Normal file
View File

@ -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 };
}

View File

@ -7,17 +7,12 @@ import { setupIPC } from "./electron/ipc.js";
import { LocalServer } from "./localserver/localserver.js"; import { LocalServer } from "./localserver/localserver.js";
import { SocketClient } from "./socket/socketclient.js"; import { SocketClient } from "./socket/socketclient.js";
import { import {
authenticateWithOtp,
checkRunning,
createCliLogger, createCliLogger,
isHeadless, isHeadless,
isInfo,
otpCode,
printAuthCommand,
printerHostInfo,
startWaiting, startWaiting,
stopWaiting, stopWaiting,
} from "./commandlineutils.js"; } from "./commandlineutils.js";
import { handleCommandLine } from "./commandline.js";
if (!process.env.NODE_ENV) { if (!process.env.NODE_ENV) {
process.env.NODE_ENV = "production"; process.env.NODE_ENV = "production";
@ -33,80 +28,13 @@ const cli = createCliLogger(logger);
export async function init(options = {}) { export async function init(options = {}) {
const headless = options.headless ?? isHeadless; const headless = options.headless ?? isHeadless;
cli.blank(); cli.blank();
startWaiting("Checking if Farm Control Server is running...", logger);
const isCommandLines = otpCode != undefined || isInfo == true; const commandLineResult = await handleCommandLine(cli, logger);
let runningServer; if (commandLineResult.handled) {
try { return commandLineResult.result;
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;
} }
if (isCommandLines == true) { const runningServer = commandLineResult.runningServer;
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;
}
if (runningServer != false) { if (runningServer != false) {
cli.warn("✘ Farm Control Server is already running."); cli.warn("✘ Farm Control Server is already running.");

View File

@ -27,6 +27,7 @@ export class LocalServer {
res.json({ res.json({
running: true, running: true,
authenticated: this.socketClient.authenticated, authenticated: this.socketClient.authenticated,
connected: this.socketClient.connected,
host: this.socketClient.host, host: this.socketClient.host,
...getServerVersionInfo(), ...getServerVersionInfo(),
}); });