use crate::output;
use canic_backup::{
manifest::FleetBackupManifest,
persistence::BackupLayout,
restore::{
RestoreApplyDryRun, RestoreApplyJournal, RestoreMapping, RestorePlan, RestoreRunResponse,
},
};
use std::path::PathBuf;
use super::{RestoreApplyOptions, RestoreCommandError, RestorePlanOptions, RestoreRunOptions};
pub(super) fn verify_backup_layout_if_required(
options: &RestorePlanOptions,
) -> Result<(), RestoreCommandError> {
if !options.require_verified {
return Ok(());
}
let Some(dir) = &options.backup_dir else {
return Err(RestoreCommandError::RequireVerifiedNeedsBackupDir);
};
BackupLayout::new(dir.clone()).verify_integrity()?;
Ok(())
}
pub(super) fn read_manifest_source(
options: &RestorePlanOptions,
) -> Result<FleetBackupManifest, RestoreCommandError> {
if let Some(path) = &options.manifest {
return read_manifest(path);
}
let Some(dir) = &options.backup_dir else {
return Err(RestoreCommandError::MissingOption(
"--manifest or --backup-dir",
));
};
BackupLayout::new(dir.clone())
.read_manifest()
.map_err(RestoreCommandError::from)
}
fn read_manifest(path: &PathBuf) -> Result<FleetBackupManifest, RestoreCommandError> {
output::read_json_file::<FleetBackupManifest, RestoreCommandError>(path)
}
pub(super) fn read_mapping(path: &PathBuf) -> Result<RestoreMapping, RestoreCommandError> {
output::read_json_file::<RestoreMapping, RestoreCommandError>(path)
}
pub(super) fn read_plan(path: &PathBuf) -> Result<RestorePlan, RestoreCommandError> {
output::read_json_file::<RestorePlan, RestoreCommandError>(path)
}
pub(super) fn write_plan(
options: &RestorePlanOptions,
plan: &RestorePlan,
) -> Result<(), RestoreCommandError> {
output::write_pretty_json(options.out.as_ref(), plan)
}
pub(super) fn write_apply_dry_run(
options: &RestoreApplyOptions,
dry_run: &RestoreApplyDryRun,
) -> Result<(), RestoreCommandError> {
output::write_pretty_json(options.out.as_ref(), dry_run)
}
pub(super) fn write_apply_journal_if_requested(
options: &RestoreApplyOptions,
dry_run: &RestoreApplyDryRun,
) -> Result<(), RestoreCommandError> {
let Some(path) = &options.journal_out else {
return Ok(());
};
output::write_pretty_json_file(path, &RestoreApplyJournal::from_dry_run(dry_run))
}
pub(super) fn write_restore_run(
options: &RestoreRunOptions,
run: &RestoreRunResponse,
) -> Result<(), RestoreCommandError> {
output::write_pretty_json(options.out.as_ref(), run)
}