-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
10 changed files
with
512 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {nextArg} from '@junobuild/cli-tools'; | ||
import {red} from 'kleur'; | ||
import {logHelpBackup} from '../help/backup.help'; | ||
import {logHelpDev} from '../help/dev.help'; | ||
import { | ||
createSnapshotMissionControl, | ||
deleteSnapshotMissionControl, | ||
restoreSnapshotMissionControl | ||
} from '../services/backup/backup.mission-control.services'; | ||
import { | ||
createSnapshotOrbiter, | ||
deleteSnapshotOrbiter, | ||
restoreSnapshotOrbiter | ||
} from '../services/backup/backup.orbiter.services'; | ||
import { | ||
createSnapshotSatellite, | ||
deleteSnapshotSatellite, | ||
restoreSnapshotSatellite | ||
} from '../services/backup/backup.satellite.services'; | ||
|
||
export const backup = async (args?: string[]) => { | ||
const [subCommand] = args ?? []; | ||
|
||
switch (subCommand) { | ||
case 'create': | ||
await executeBackupFn({ | ||
args, | ||
satelliteFn: createSnapshotSatellite, | ||
missionControlFn: createSnapshotMissionControl, | ||
orbiterFn: createSnapshotOrbiter | ||
}); | ||
break; | ||
case 'restore': | ||
await executeBackupFn({ | ||
args, | ||
satelliteFn: restoreSnapshotSatellite, | ||
missionControlFn: restoreSnapshotMissionControl, | ||
orbiterFn: restoreSnapshotOrbiter | ||
}); | ||
break; | ||
case 'delete': | ||
await executeBackupFn({ | ||
args, | ||
satelliteFn: deleteSnapshotSatellite, | ||
missionControlFn: deleteSnapshotMissionControl, | ||
orbiterFn: deleteSnapshotOrbiter | ||
}); | ||
break; | ||
default: | ||
console.log(`${red('Unknown subcommand.')}`); | ||
logHelpDev(); | ||
} | ||
}; | ||
|
||
const executeBackupFn = async ({ | ||
args, | ||
satelliteFn, | ||
missionControlFn, | ||
orbiterFn | ||
}: { | ||
args?: string[]; | ||
satelliteFn: (params: {args?: string[]}) => Promise<void>; | ||
missionControlFn: () => Promise<void>; | ||
orbiterFn: () => Promise<void>; | ||
}) => { | ||
const target = nextArg({args, option: '-t'}) ?? nextArg({args, option: '--target'}); | ||
|
||
switch (target) { | ||
case 's': | ||
case 'satellite': | ||
await satelliteFn({args}); | ||
break; | ||
case 'm': | ||
case 'mission-control': | ||
await missionControlFn(); | ||
break; | ||
case 'o': | ||
case 'orbiter': | ||
await orbiterFn(); | ||
break; | ||
default: | ||
console.log(`${red('Unknown target.')}`); | ||
logHelpBackup(args); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {cyan, green, magenta, yellow} from 'kleur'; | ||
import {helpMode, helpOutput} from './common.help'; | ||
import {TITLE} from './help'; | ||
import {TARGET_OPTION_NOTE, targetOption} from './target.help'; | ||
|
||
export const BACKUP_DESCRIPTION = 'Handle backup-related tasks.'; | ||
|
||
const usage = `Usage: ${green('juno')} ${cyan('dev')} ${magenta('<subcommand>')} ${yellow('[options]')} | ||
Subcommands: | ||
${magenta('create')} Create a backup of your current state. | ||
${magenta('restore')} Restore a previously created backup. | ||
${magenta('delete')} Delete an existing backup | ||
Options: | ||
${targetOption('backup')} | ||
${helpMode} | ||
${yellow('-h, --help')} Output usage information. | ||
Notes: | ||
${TARGET_OPTION_NOTE}`; | ||
|
||
const doc = `${BACKUP_DESCRIPTION} | ||
\`\`\`bash | ||
${usage} | ||
\`\`\` | ||
`; | ||
|
||
const help = `${TITLE} | ||
${BACKUP_DESCRIPTION} | ||
${usage} | ||
`; | ||
|
||
export const logHelpBackup = (args?: string[]) => { | ||
console.log(helpOutput(args) === 'doc' ? doc : help); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {magenta, yellow} from 'kleur'; | ||
|
||
export const targetOption = (action: 'upgrade' | 'backup'): string => | ||
`${yellow('-t, --target')} Which module type should be ${action === 'backup' ? 'backed up' : 'upgraded'}? Valid targets are ${magenta('satellite')}, ${magenta('mission-control')} or ${magenta('orbiter')}.`; | ||
|
||
export const TARGET_OPTION_NOTE = `- Targets can be shortened to ${magenta('s')} for satellite, ${magenta('m')} for mission-control and ${magenta('o')} for orbiter.`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {isNullish} from '@junobuild/utils'; | ||
import {red} from 'kleur'; | ||
import {getCliMissionControl} from '../../configs/cli.config'; | ||
import type {AssetKey} from '../../types/asset-key'; | ||
import {createSnapshot, deleteSnapshot, restoreSnapshot} from './backup.services'; | ||
|
||
export const createSnapshotMissionControl = async () => { | ||
await executeBackupFn({ | ||
fn: createSnapshot | ||
}); | ||
}; | ||
|
||
export const restoreSnapshotMissionControl = async () => { | ||
await executeBackupFn({ | ||
fn: restoreSnapshot | ||
}); | ||
}; | ||
|
||
export const deleteSnapshotMissionControl = async () => { | ||
await executeBackupFn({ | ||
fn: deleteSnapshot | ||
}); | ||
}; | ||
|
||
const executeBackupFn = async ({ | ||
fn | ||
}: { | ||
fn: (params: {canisterId: string; segment: AssetKey}) => Promise<void>; | ||
}) => { | ||
const missionControl = await getCliMissionControl(); | ||
|
||
// TODO: this can be a common assertion | ||
if (isNullish(missionControl)) { | ||
console.log( | ||
`${red( | ||
'No mission control found.' | ||
)} Ignore this error if your controller does not control your mission control.` | ||
); | ||
return; | ||
} | ||
|
||
await fn({ | ||
canisterId: missionControl, | ||
segment: 'mission_control' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {getCliOrbiters} from '../../configs/cli.config'; | ||
import type {AssetKey} from '../../types/asset-key'; | ||
import {createSnapshot, deleteSnapshot, restoreSnapshot} from './backup.services'; | ||
|
||
export const createSnapshotOrbiter = async () => { | ||
await executeBackupFn({ | ||
fn: createSnapshot | ||
}); | ||
}; | ||
|
||
export const restoreSnapshotOrbiter = async () => { | ||
await executeBackupFn({ | ||
fn: restoreSnapshot | ||
}); | ||
}; | ||
|
||
export const deleteSnapshotOrbiter = async () => { | ||
await executeBackupFn({ | ||
fn: deleteSnapshot | ||
}); | ||
}; | ||
|
||
const executeBackupFn = async ({ | ||
fn | ||
}: { | ||
fn: (params: {canisterId: string; segment: AssetKey}) => Promise<void>; | ||
}) => { | ||
const authOrbiters = await getCliOrbiters(); | ||
|
||
if (authOrbiters === undefined || authOrbiters.length === 0) { | ||
return; | ||
} | ||
|
||
for (const orbiter of authOrbiters) { | ||
await fn({ | ||
canisterId: orbiter.p, | ||
segment: 'orbiter' | ||
}); | ||
} | ||
}; |
Oops, something went wrong.