|
| 1 | +import * as clc from "colorette"; |
| 2 | + |
| 3 | +import { Command } from "../command"; |
| 4 | +import { calculateRetention } from "../firestore/backupUtils"; |
| 5 | +import { |
| 6 | + BackupSchedule, |
| 7 | + DayOfWeek, |
| 8 | + WeeklyRecurrence, |
| 9 | + createBackupSchedule, |
| 10 | +} from "../gcp/firestore"; |
| 11 | +import * as types from "../firestore/api-types"; |
| 12 | +import { logger } from "../logger"; |
| 13 | +import { requirePermissions } from "../requirePermissions"; |
| 14 | +import { Emulators } from "../emulator/types"; |
| 15 | +import { warnEmulatorNotSupported } from "../emulator/commandUtils"; |
| 16 | +import { FirestoreOptions } from "../firestore/options"; |
| 17 | +import { PrettyPrint } from "../firestore/pretty-print"; |
| 18 | + |
| 19 | +export const command = new Command("firestore:backups:schedules:create") |
| 20 | + .description("Create a backup schedule under your Cloud Firestore database.") |
| 21 | + .option( |
| 22 | + "-db, --database <databaseId>", |
| 23 | + "Database under which you want to create a schedule. Defaults to the (default) database", |
| 24 | + ) |
| 25 | + .option("-rt, --retention <duration>", "duration string (e.g. 12h or 30d) for backup retention") |
| 26 | + .option("-rc, --recurrence <recurrence>", "Recurrence settings; either DAILY or WEEKLY") |
| 27 | + .option( |
| 28 | + "-dw, --day-of-week <dayOfWeek>", |
| 29 | + "On which day of the week to perform backups; one of MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, or SUNDAY", |
| 30 | + ) |
| 31 | + .before(requirePermissions, ["datastore.backupSchedules.create"]) |
| 32 | + .before(warnEmulatorNotSupported, Emulators.FIRESTORE) |
| 33 | + .action(async (options: FirestoreOptions) => { |
| 34 | + const printer = new PrettyPrint(); |
| 35 | + |
| 36 | + const databaseId = options.database || "(default)"; |
| 37 | + |
| 38 | + if (!options.retention) { |
| 39 | + logger.error( |
| 40 | + "Missing required flag --retention. See firebase firestore:backups:schedules:create --help for more info", |
| 41 | + ); |
| 42 | + return; |
| 43 | + } |
| 44 | + const retention = calculateRetention(options.retention); |
| 45 | + |
| 46 | + if (!options.recurrence) { |
| 47 | + logger.error( |
| 48 | + "Missing required flag --recurrence. See firebase firestore:backups:schedules:create --help for more info", |
| 49 | + ); |
| 50 | + return; |
| 51 | + } |
| 52 | + const recurrenceType: types.RecurrenceType = options.recurrence; |
| 53 | + if ( |
| 54 | + recurrenceType !== types.RecurrenceType.DAILY && |
| 55 | + recurrenceType !== types.RecurrenceType.WEEKLY |
| 56 | + ) { |
| 57 | + logger.error( |
| 58 | + "Invalid value for flag --recurrence. See firebase firestore:backups:schedules:create --help for more info", |
| 59 | + ); |
| 60 | + return; |
| 61 | + } |
| 62 | + let dailyRecurrence: Record<string, never> | undefined; |
| 63 | + let weeklyRecurrence: WeeklyRecurrence | undefined; |
| 64 | + if (options.recurrence === types.RecurrenceType.DAILY) { |
| 65 | + dailyRecurrence = {}; |
| 66 | + if (options.dayOfWeek) { |
| 67 | + logger.error("--day-of-week should not be provided if --recurrence is DAILY"); |
| 68 | + return; |
| 69 | + } |
| 70 | + } else if (options.recurrence === types.RecurrenceType.WEEKLY) { |
| 71 | + if (!options.dayOfWeek) { |
| 72 | + logger.error( |
| 73 | + "If --recurrence is WEEKLY, --day-of-week must be provided. See firebase firestore:backups:schedules:create --help for more info", |
| 74 | + ); |
| 75 | + return; |
| 76 | + } |
| 77 | + const day: DayOfWeek = options.dayOfWeek; |
| 78 | + weeklyRecurrence = { |
| 79 | + day, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + const backupSchedule: BackupSchedule = await createBackupSchedule( |
| 84 | + options.project, |
| 85 | + databaseId, |
| 86 | + retention, |
| 87 | + dailyRecurrence, |
| 88 | + weeklyRecurrence, |
| 89 | + ); |
| 90 | + |
| 91 | + if (options.json) { |
| 92 | + logger.info(JSON.stringify(backupSchedule, undefined, 2)); |
| 93 | + } else { |
| 94 | + logger.info( |
| 95 | + clc.bold(`Successfully created ${printer.prettyBackupScheduleString(backupSchedule)}`), |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return backupSchedule; |
| 100 | + }); |
0 commit comments