openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.06 kB
JavaScript
import{r as terminalText}from"./safe-text-B_QTgn0s.mjs";import{f as shortenHomePath}from"./utils-Dlk0IYAa.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{h as resolveConfiguredAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{t as parseDurationMs}from"./parse-duration-BueP0M46.mjs";import{t as captureRuntimeConfigAsyncReader}from"./io.runtime-BvSeVFIY.mjs";import"./config-DkZ8y8Gv.mjs";import{a as executeGitCommand}from"./git-exec-BRQo7oSy.mjs";import{a as isImplicitLocalGatewayTargetFromCli,n as callGatewayFromCli}from"./gateway-rpc-17pTfL8i.mjs";import{r as listCronJobsFromGateway}from"./list-jobs-frxceh8V.mjs";import{u as resolveRequiredBackupPath}from"./backup-shared-CVcoDGTC.mjs";import{i as resolveBackupNamespace,n as normalizeBackupRetention}from"./backup-retention-CCINX3jT.mjs";import{t as GIT_BACKUP_PUSH_CREDENTIAL_WARNING}from"./backup-git-de-biw5d.mjs";import{n as buildBackupScheduleJob,t as backupScheduleModeForDeclaration}from"./backup-command-SjLQXBkh.mjs";
//!src/commands/backup-schedule.ts
const LOCAL_GATEWAY_REQUIRED_ERROR=`backup enable manages backups on the Gateway host and currently requires a local Gateway. Create the cron job manually with openclaw cron add for remote Gateways.`;function resolveScheduledRedaction(e){if(e.excludeSecrets&&e.includeSecrets)throw new Error(`Use either --exclude-secrets or --include-secrets, not both.`);if(!e.push)return e.excludeSecrets===true;return e.includeSecrets!==true}async function resolveScheduleSpec(e,t){if(e.to!==void 0){if(e.repository!==void 0||e.push||e.excludeSecrets||e.includeSecrets||e.globalOnly||e.agent!==void 0)throw new Error(`--to cannot be combined with Git backup options (--repository, --push, --exclude-secrets, --include-secrets, --global-only, --agent).`);const n=e.to.trim();if(!n)throw new Error(`--to must name a configured storage location.`);if(!(await captureRuntimeConfigAsyncReader({skipPluginValidation:true})()).storage?.locations?.[n])throw new Error(`Storage location "${n}" is not configured. Run openclaw storage list.`);return{mode:`offsite`,everyMs:t,location:n,namespace:resolveBackupNamespace(e.namespace),claimNamespace:e.claimNamespace===true,includeWorkspace:e.includeWorkspace!==false,...normalizeBackupRetention(e)}}if(e.namespace!==void 0||e.claimNamespace||e.includeWorkspace===false||e.keepDaily!==void 0||e.keepWeekly!==void 0||e.keepMonthly!==void 0)throw new Error(`--namespace, --claim-namespace, --no-include-workspace, and --keep-* require --to <location>.`);const n=resolveRequiredBackupPath(e.repository,`--repository`);const r=e.agent?.trim();if(e.agent!==void 0&&!r)throw new Error(`--agent must not be blank`);if(e.globalOnly&&r)throw new Error(`Use either --global-only or --agent <id>, not both.`);const i=r?resolveConfiguredAgentId(await captureRuntimeConfigAsyncReader({skipPluginValidation:true})(),normalizeAgentId(r)):void 0;return{mode:`git`,everyMs:t,repository:n,scope:e.globalOnly?{kind:`global`}:i?{kind:`agent`,agentId:i}:{kind:`all`},push:e.push===true,excludeSecrets:resolveScheduledRedaction(e)}}async function assertLocalGatewayScheduleTarget(e){if(!await isImplicitLocalGatewayTargetFromCli(e))throw new Error(LOCAL_GATEWAY_REQUIRED_ERROR)}async function backupEnableCommand(e,t){await assertLocalGatewayScheduleTarget(t);const n=t.every?.trim()??`24h`;const r=parseDurationMs(n,{defaultUnit:`ms`});if(!Number.isSafeInteger(r)||r<=0)throw new Error(`--every must be a positive duration such as 6h or 24h.`);const i=await resolveScheduleSpec(t,r);if(i.mode===`git`&&i.push){if((await executeGitCommand(i.repository,[`remote`,`get-url`,`origin`])).code!==0)throw new Error(terminalText`--push requires an origin remote. Run: openclaw backup git init --repository ${shortenHomePath(i.repository)} --remote <url>`);if(!i.excludeSecrets)e.error(GIT_BACKUP_PUSH_CREDENTIAL_WARNING)}const a=await callGatewayFromCli(`cron.add`,t,buildBackupScheduleJob(i));const o=a.job?.id;if(!o)throw new Error(`cron.add returned no scheduled backup job id.`);const s=a.created===false;e.log(terminalText`Scheduled ${i.mode===`git`?`Git`:`offsite`} backups ${s?`updated`:`enabled`}: every ${n} to ${i.mode===`git`?shortenHomePath(i.repository):i.location}`);return{id:o,updated:s}}async function backupDisableCommand(e,t){await assertLocalGatewayScheduleTarget(t);if(t.git&&t.offsite)throw new Error(`Use either --git or --offsite, or omit both to disable all backup schedules.`);const{jobs:n}=await listCronJobsFromGateway(t,{includeDisabled:true});const r=t.git?`git`:t.offsite?`offsite`:void 0;const i=n.filter(e=>{const t=backupScheduleModeForDeclaration(e.declarationKey);return t!==void 0&&(r===void 0||t===r)});const a=r===`git`?`Scheduled Git backups`:r===`offsite`?`Scheduled offsite backups`:`Scheduled backups`;if(i.length===0){e.log(`${a} are already disabled.`);return{removed:false}}for(const e of i)await callGatewayFromCli(`cron.remove`,t,{id:e.id});e.log(`${a} disabled.`);return{removed:true}}export{backupDisableCommand,backupEnableCommand};