refactor(update-cli): share timeout option validation
This commit is contained in:
@@ -37,6 +37,18 @@ export type UpdateWizardOptions = {
|
||||
timeout?: string;
|
||||
};
|
||||
|
||||
const INVALID_TIMEOUT_ERROR = "--timeout must be a positive integer (seconds)";
|
||||
|
||||
export function parseTimeoutMsOrExit(timeout?: string): number | undefined | null {
|
||||
const timeoutMs = timeout ? Number.parseInt(timeout, 10) * 1000 : undefined;
|
||||
if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) {
|
||||
defaultRuntime.error(INVALID_TIMEOUT_ERROR);
|
||||
defaultRuntime.exit(1);
|
||||
return null;
|
||||
}
|
||||
return timeoutMs;
|
||||
}
|
||||
|
||||
const OPENCLAW_REPO_URL = "https://github.com/openclaw/openclaw.git";
|
||||
const MAX_LOG_CHARS = 8000;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { checkUpdateStatus } from "../../infra/update-check.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { renderTable } from "../../terminal/table.js";
|
||||
import { theme } from "../../terminal/theme.js";
|
||||
import { resolveUpdateRoot, type UpdateStatusOptions } from "./shared.js";
|
||||
import { parseTimeoutMsOrExit, resolveUpdateRoot, type UpdateStatusOptions } from "./shared.js";
|
||||
|
||||
function formatGitStatusLine(params: {
|
||||
branch: string | null;
|
||||
@@ -31,10 +31,8 @@ function formatGitStatusLine(params: {
|
||||
}
|
||||
|
||||
export async function updateStatusCommand(opts: UpdateStatusOptions): Promise<void> {
|
||||
const timeoutMs = opts.timeout ? Number.parseInt(opts.timeout, 10) * 1000 : undefined;
|
||||
if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) {
|
||||
defaultRuntime.error("--timeout must be a positive integer (seconds)");
|
||||
defaultRuntime.exit(1);
|
||||
const timeoutMs = parseTimeoutMsOrExit(opts.timeout);
|
||||
if (timeoutMs === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
DEFAULT_PACKAGE_NAME,
|
||||
ensureGitCheckout,
|
||||
normalizeTag,
|
||||
parseTimeoutMsOrExit,
|
||||
readPackageName,
|
||||
readPackageVersion,
|
||||
resolveGitInstallDir,
|
||||
@@ -468,12 +469,9 @@ async function maybeRestartService(params: {
|
||||
export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
|
||||
suppressDeprecations();
|
||||
|
||||
const timeoutMs = opts.timeout ? Number.parseInt(opts.timeout, 10) * 1000 : undefined;
|
||||
const timeoutMs = parseTimeoutMsOrExit(opts.timeout);
|
||||
const shouldRestart = opts.restart !== false;
|
||||
|
||||
if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) {
|
||||
defaultRuntime.error("--timeout must be a positive integer (seconds)");
|
||||
defaultRuntime.exit(1);
|
||||
if (timeoutMs === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import { pathExists } from "../../utils.js";
|
||||
import {
|
||||
isEmptyDir,
|
||||
isGitCheckout,
|
||||
parseTimeoutMsOrExit,
|
||||
resolveGitInstallDir,
|
||||
resolveUpdateRoot,
|
||||
type UpdateWizardOptions,
|
||||
@@ -29,10 +30,8 @@ export async function updateWizardCommand(opts: UpdateWizardOptions = {}): Promi
|
||||
return;
|
||||
}
|
||||
|
||||
const timeoutMs = opts.timeout ? Number.parseInt(opts.timeout, 10) * 1000 : undefined;
|
||||
if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) {
|
||||
defaultRuntime.error("--timeout must be a positive integer (seconds)");
|
||||
defaultRuntime.exit(1);
|
||||
const timeoutMs = parseTimeoutMsOrExit(opts.timeout);
|
||||
if (timeoutMs === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user