Skip to content

Commit

Permalink
Only pass Retention where it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Nov 8, 2024
1 parent 8c76c24 commit a8c3fcb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 182 deletions.
26 changes: 10 additions & 16 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,9 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
prepare_backup_target(&backup_dir)?;
}

let mut retention = config.backup.retention.clone();
if let Some(full_limit) = full_limit {
retention.full = full_limit;
}
if let Some(differential_limit) = differential_limit {
retention.differential = differential_limit;
}
let retention = config.backup.retention.with_limits(full_limit, differential_limit);

let layout = BackupLayout::new(backup_dir.clone(), retention);
let layout = BackupLayout::new(backup_dir.clone());
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games_specified = !games.is_empty();
Expand Down Expand Up @@ -291,7 +285,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)

layout
.game_layout(name)
.back_up(&scan_info, &chrono::Utc::now(), &backup_format)
.back_up(&scan_info, &chrono::Utc::now(), &backup_format, retention)
};
log::trace!("step {i} completed");
if !scan_info.can_report_game() {
Expand Down Expand Up @@ -388,7 +382,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
}
}

let layout = BackupLayout::new(restore_dir.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(restore_dir.clone());

if backup.is_some() && games.len() != 1 {
return Err(Error::CliBackupIdWithMultipleGames);
Expand Down Expand Up @@ -553,7 +547,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
Some(p) => p,
};

let layout = BackupLayout::new(restore_dir.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(restore_dir.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

Expand Down Expand Up @@ -607,7 +601,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
None => config.restore.path.clone(),
Some(p) => p,
};
let layout = BackupLayout::new(restore_dir.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(restore_dir.clone());

let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());
let found = title_finder.find(TitleQuery {
Expand Down Expand Up @@ -761,7 +755,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
let finality = if preview { Finality::Preview } else { Finality::Final };
let direction = SyncDirection::Upload;

let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(config.restore.path.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

Expand Down Expand Up @@ -802,7 +796,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
let finality = if preview { Finality::Preview } else { Finality::Final };
let direction = SyncDirection::Download;

let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(config.restore.path.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

Expand Down Expand Up @@ -835,7 +829,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
commands,
} => {
let manifest = load_manifest(&config, &mut cache, no_manifest_update, try_manifest_update)?;
let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(config.restore.path.clone());
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

// Determine raw game identifiers
Expand Down Expand Up @@ -1103,7 +1097,7 @@ fn sync_cloud(
let remote = crate::cloud::validate_cloud_config(config, cloud)?;

let games = if !games.is_empty() {
let layout = BackupLayout::new(local.clone(), config.backup.retention.clone());
let layout = BackupLayout::new(local.clone());
let games: Vec<_> = games.iter().filter_map(|x| layout.game_folder(x).leaf()).collect();
games
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn process(input: Option<String>, config: &Config, manifest: &Manifest) -> R
let mut responses = vec![];

let backup_path = input.config.backup_path.unwrap_or_else(|| config.restore.path.clone());
let layout = BackupLayout::new(backup_path, config.backup.retention.clone());
let layout = BackupLayout::new(backup_path);

let title_finder = TitleFinder::new(config, manifest, layout.restorable_game_set());

Expand Down
29 changes: 14 additions & 15 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl App {

let games = match games {
Some(games) => {
let layout = BackupLayout::new(local.clone(), self.config.backup.retention.clone());
let layout = BackupLayout::new(local.clone());
let games: Vec<_> = games.iter().filter_map(|x| layout.game_folder(x).leaf()).collect();
games
}
Expand Down Expand Up @@ -369,7 +369,6 @@ impl App {
let mut manifest = self.manifest.primary.clone();
let config = self.config.clone();
let previewed_games = self.backup_screen.previewed_games.clone();
let should_force_new_full_backups = self.operation.should_force_new_full_backups();

Task::perform(
async move {
Expand All @@ -387,11 +386,8 @@ impl App {
manifest.processable_titles().cloned().collect()
};

let mut retention = config.backup.retention.clone();
retention.force_new_full = should_force_new_full_backups;

let roots = config.expanded_roots();
let layout = BackupLayout::new(config.backup.path.clone(), retention);
let layout = BackupLayout::new(config.backup.path.clone());
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());
let steam = SteamShortcuts::scan();
let launchers = Launchers::scan(&roots, &manifest, &subjects, &title_finder, None);
Expand Down Expand Up @@ -454,6 +450,10 @@ impl App {
let filter = std::sync::Arc::new(self.config.backup.filter.clone());
let steam_shortcuts = std::sync::Arc::new(steam);
let games_specified = self.operation.games_specified();
let retention = config
.backup
.retention
.with_force_new_full(self.operation.should_force_new_full_backups());

for key in subjects {
let game = manifest.0[&key].clone();
Expand Down Expand Up @@ -506,9 +506,12 @@ impl App {
}

let backup_info = if !preview {
layout
.game_layout(&key)
.back_up(&scan_info, &chrono::Utc::now(), &config.backup.format)
layout.game_layout(&key).back_up(
&scan_info,
&chrono::Utc::now(),
&config.backup.format,
retention,
)
} else {
None
};
Expand Down Expand Up @@ -770,13 +773,11 @@ impl App {
RestorePhase::Load => {
let restore_path = self.config.restore.path.clone();

let config = std::sync::Arc::new(self.config.clone());

self.progress.start();

Task::perform(
async move {
let layout = BackupLayout::new(restore_path, config.backup.retention.clone());
let layout = BackupLayout::new(restore_path);
let restorables = layout.restorable_games();
(layout, restorables)
},
Expand Down Expand Up @@ -1016,13 +1017,11 @@ impl App {
ValidatePhase::Load => {
let restore_path = self.config.restore.path.clone();

let config = std::sync::Arc::new(self.config.clone());

self.progress.start();

Task::perform(
async move {
let layout = BackupLayout::new(restore_path, config.backup.retention.clone());
let layout = BackupLayout::new(restore_path);
let subjects = layout.restorable_games();
(layout, subjects)
},
Expand Down
28 changes: 27 additions & 1 deletion src/resource/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ pub struct Sort {
pub reversed: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[derive(Clone, Debug, Copy, Eq, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(default, rename_all = "camelCase")]
pub struct Retention {
/// Full backups to keep. Range: 1-255.
Expand All @@ -777,6 +777,32 @@ pub struct Retention {
pub force_new_full: bool,
}

impl Retention {
#[cfg(test)]
pub fn new(full: u8, differential: u8) -> Self {
Self {
full,
differential,
..Default::default()
}
}

pub fn with_limits(self, full: Option<u8>, differential: Option<u8>) -> Self {
Self {
full: full.unwrap_or(self.full),
differential: differential.unwrap_or(self.differential),
..self
}
}

pub fn with_force_new_full(self, force: bool) -> Self {
Self {
force_new_full: force,
..self
}
}
}

impl Default for Retention {
fn default() -> Self {
Self {
Expand Down
Loading

0 comments on commit a8c3fcb

Please sign in to comment.