-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move config to subdir and break into multiple files
- Loading branch information
Showing
12 changed files
with
167 additions
and
124 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,51 @@ | ||
use crate::config::runtime::RuntimeConfig; | ||
use crate::sources::auto_prune::Prune; | ||
use crate::sources::bitwarden::BitWardenCore; | ||
use crate::sources::exporter::Exporter; | ||
use crate::sources::op::core::OnePasswordCore; | ||
use crate::sources::s3::S3Core; | ||
use lib::anyhow::Result; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub enum Backend { | ||
S3(S3Core), | ||
BitWarden(BitWardenCore), | ||
OnePassword(OnePasswordCore), | ||
} | ||
|
||
impl Display for Backend { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Backend::S3(s3) => write!( | ||
f, | ||
"S3 ({}:{})", | ||
&s3.base.backend.get("bucket").unwrap(), | ||
&s3.base.object.display() | ||
), | ||
Backend::BitWarden(bw) => write!(f, "BitWarden ({})", &bw.org_name), | ||
Backend::OnePassword(op) => write!(f, "1Password ({})", &op.account), | ||
} | ||
} | ||
} | ||
|
||
impl Backend { | ||
pub async fn run(&mut self, config: &RuntimeConfig) -> Result<()> { | ||
match self { | ||
Backend::S3(ref mut core) => { | ||
core.prune(&config)?; | ||
core.export(&config).await?; | ||
} | ||
Backend::BitWarden(ref mut core) => { | ||
core.prune(&config)?; | ||
core.export(&config).await?; | ||
} | ||
Backend::OnePassword(ref mut core) => { | ||
core.prune(&config)?; | ||
core.export(&config).await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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,7 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub rules: super::rules::Rules, | ||
pub exporters: Vec<super::backend::Backend>, | ||
} |
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,4 @@ | ||
pub mod runtime; | ||
pub mod config; | ||
pub mod backend; | ||
pub mod rules; |
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,45 @@ | ||
use lib::anyhow::Result; | ||
use std::path::PathBuf; | ||
use std::time::SystemTime; | ||
|
||
#[derive(Default, Debug, Clone, Serialize, Deserialize)] | ||
pub struct Rules { | ||
/// The AutoPrune configuration. | ||
pub auto_prune: AutoPrune, | ||
} | ||
|
||
#[derive(Parser, Debug, Clone, Serialize, Deserialize)] | ||
pub struct AutoPrune { | ||
/// Whether or not the auto prune feature is enabled. | ||
#[arg(long = "prune", action = clap::ArgAction::SetTrue)] | ||
pub enabled: bool, | ||
|
||
/// How long backups should be kept for in days. | ||
#[arg(long = "prune-keep-days", default_value = "28")] | ||
pub keep_for: usize, | ||
|
||
/// The minimum number of backups to keep ignoring the keep_for duration. | ||
#[arg(long = "prune-keep-count", default_value = "5")] | ||
pub keep_latest: usize, | ||
} | ||
|
||
impl AutoPrune { | ||
pub fn should_prune(&self, file: &PathBuf, remaining_files: usize) -> Result<bool> { | ||
let mtime = file.metadata()?.modified()?; | ||
let now = SystemTime::now(); | ||
let age = now.duration_since(mtime)?; | ||
let days = chrono::Duration::from_std(age)?.num_days(); | ||
|
||
Ok(days > self.keep_for.clone() as i64 && remaining_files > *&self.keep_latest) | ||
} | ||
} | ||
|
||
impl Default for AutoPrune { | ||
fn default() -> Self { | ||
Self { | ||
enabled: false, | ||
keep_for: 28, | ||
keep_latest: 5, | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.