Skip to content

Commit

Permalink
Add clean_cache to backend mod
Browse files Browse the repository at this point in the history
Signed-off-by: innocentzero <[email protected]>
  • Loading branch information
InnocentZero committed Dec 4, 2024
1 parent 8049755 commit f2b28be
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/backends/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl Backend for Apt {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(["apt-get", "autoclean"], Perms::Sudo)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["apt", "--version"], Perms::Same, false)
}
Expand Down
9 changes: 9 additions & 0 deletions src/backends/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ impl Backend for Arch {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(
[config.arch_package_manager.as_command(), "-Sc"],
Perms::Same,
)
})
}

fn version(config: &Config) -> Result<String> {
run_command_for_stdout(
[config.arch_package_manager.as_command(), "--version"],
Expand Down
6 changes: 6 additions & 0 deletions src/backends/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ impl Backend for Brew {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(["brew", "cleanup", "--prune-prefix"], Perms::Same)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["brew", "--version"], Perms::Same, false)
}
Expand Down
6 changes: 6 additions & 0 deletions src/backends/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ impl Backend for Cargo {
Ok(())
}

fn clean_cache(_: &Config) -> Result<()> {
run_command_for_stdout(["cargo-cache", "-V"], Perms::Same, false).map_or(Ok(()), |_| {
run_command(["cargo", "cache", "-a"], Perms::Same)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["cargo", "--version"], Perms::Same, false)
}
Expand Down
6 changes: 6 additions & 0 deletions src/backends/dnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ impl Backend for Dnf {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(["dnf", "clean", "all"], Perms::Same)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["dnf", "--version"], Perms::Same, false)
}
Expand Down
6 changes: 6 additions & 0 deletions src/backends/flatpak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ impl Backend for Flatpak {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(["flatpak", "remove", "--unused"], Perms::Same)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["flatpak", "--version"], Perms::Same, false)
}
Expand Down
2 changes: 2 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ pub trait Backend {
config: &Config,
) -> Result<()>;

fn clean_cache(config: &Config) -> Result<()>;

fn version(config: &Config) -> Result<String>;
}
4 changes: 4 additions & 0 deletions src/backends/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl Backend for Pipx {
Ok(())
}

fn clean_cache(_: &Config) -> Result<()> {
Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["pipx", "--version"], Perms::Same, false)
}
Expand Down
4 changes: 4 additions & 0 deletions src/backends/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ impl Backend for Rustup {
Ok(())
}

fn clean_cache(_: &Config) -> Result<()> {
Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["rustup", "--version"], Perms::Same, true)
}
Expand Down
6 changes: 6 additions & 0 deletions src/backends/snap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl Backend for Snap {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| {
run_command(["rm", "-rf", "/var/lib/snapd/cache/*"], Perms::Sudo)
})
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["snap", "--version"], Perms::Same, false).map(|output| {
output
Expand Down
6 changes: 6 additions & 0 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl Backend for WinGet {
Ok(())
}

// currently there is no way to do it for winget, see
// https://github.com/microsoft/winget-cli/issues/343
fn clean_cache(_: &Config) -> Result<()> {
Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["winget", "--version"], Perms::Same, false)
}
Expand Down
4 changes: 4 additions & 0 deletions src/backends/xbps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl Backend for Xbps {
Ok(())
}

fn clean_cache(config: &Config) -> Result<()> {
Self::version(config).map_or(Ok(()), |_| run_command(["xbps-remove", "-Oo"], Perms::Sudo))
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["xbps-query", "--version"], Perms::Same, false)
}
Expand Down

0 comments on commit f2b28be

Please sign in to comment.