♻️ refactor(rules): remove credentials config
- remove unused credentials configuration - remove credentials file setting/getting logic - rename config file to rules.toml
This commit is contained in:
committed by
Jeremiah Russell
parent
f3f3e22458
commit
2fa7cd21ab
@@ -84,6 +84,15 @@ fn get_logging(level: log::LevelFilter) -> env_logger::Builder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_config() -> Result<Rules> {
|
fn get_config() -> Result<Rules> {
|
||||||
|
// let settings = Config::builder()
|
||||||
|
// // Add in `./Settings.toml`
|
||||||
|
// .add_source(config::File::with_name("examples/simple/Settings"))
|
||||||
|
// // Add in settings from the environment (with a prefix of APP)
|
||||||
|
// // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
|
||||||
|
// .add_source(config::Environment::with_prefix("APP"))
|
||||||
|
// .build()
|
||||||
|
// .unwrap();
|
||||||
|
|
||||||
match Rules::load() {
|
match Rules::load() {
|
||||||
Ok(c) => Ok(c),
|
Ok(c) => Ok(c),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|||||||
29
src/rules.rs
29
src/rules.rs
@@ -16,7 +16,6 @@ use crate::{EolAction, Error, MessageAge, Result, Retention};
|
|||||||
/// Configuration file for the program
|
/// Configuration file for the program
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Rules {
|
pub struct Rules {
|
||||||
credentials: Option<String>,
|
|
||||||
rules: BTreeMap<String, EolRule>,
|
rules: BTreeMap<String, EolRule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,10 +23,7 @@ impl Default for Rules {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let rules = BTreeMap::new();
|
let rules = BTreeMap::new();
|
||||||
|
|
||||||
let mut cfg = Self {
|
let mut cfg = Self { rules };
|
||||||
credentials: Some("credential.json".to_string()),
|
|
||||||
rules,
|
|
||||||
};
|
|
||||||
|
|
||||||
cfg.add_rule(Retention::new(MessageAge::Years(1), true), None, false)
|
cfg.add_rule(Retention::new(MessageAge::Years(1), true), None, false)
|
||||||
.add_rule(Retention::new(MessageAge::Weeks(1), true), None, false)
|
.add_rule(Retention::new(MessageAge::Weeks(1), true), None, false)
|
||||||
@@ -44,12 +40,6 @@ impl Rules {
|
|||||||
Rules::default()
|
Rules::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a name for the credentials file
|
|
||||||
pub fn set_credentials(&mut self, file_name: &str) -> &mut Self {
|
|
||||||
self.credentials = Some(file_name.to_string());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the contents of an existing rule
|
/// Get the contents of an existing rule
|
||||||
pub fn get_rule(&self, id: usize) -> Option<EolRule> {
|
pub fn get_rule(&self, id: usize) -> Option<EolRule> {
|
||||||
self.rules.get(&id.to_string()).cloned()
|
self.rules.get(&id.to_string()).cloned()
|
||||||
@@ -189,9 +179,7 @@ impl Rules {
|
|||||||
/// Save the current configuration to the file
|
/// Save the current configuration to the file
|
||||||
pub fn save(&self) -> Result<()> {
|
pub fn save(&self) -> Result<()> {
|
||||||
let home_dir = env::home_dir().unwrap();
|
let home_dir = env::home_dir().unwrap();
|
||||||
let path = PathBuf::new()
|
let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml");
|
||||||
.join(home_dir)
|
|
||||||
.join(".cull-gmail/cull-gmail.toml");
|
|
||||||
|
|
||||||
let res = toml::to_string(self);
|
let res = toml::to_string(self);
|
||||||
log::trace!("toml conversion result: {res:#?}");
|
log::trace!("toml conversion result: {res:#?}");
|
||||||
@@ -207,9 +195,7 @@ impl Rules {
|
|||||||
/// Load the current configuration
|
/// Load the current configuration
|
||||||
pub fn load() -> Result<Rules> {
|
pub fn load() -> Result<Rules> {
|
||||||
let home_dir = env::home_dir().unwrap();
|
let home_dir = env::home_dir().unwrap();
|
||||||
let path = PathBuf::new()
|
let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml");
|
||||||
.join(home_dir)
|
|
||||||
.join(".cull-gmail/cull-gmail.toml");
|
|
||||||
log::trace!("Loading config from {}", path.display());
|
log::trace!("Loading config from {}", path.display());
|
||||||
|
|
||||||
let input = read_to_string(path)?;
|
let input = read_to_string(path)?;
|
||||||
@@ -217,15 +203,6 @@ impl Rules {
|
|||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the credential file name
|
|
||||||
pub fn credential_file(&self) -> &str {
|
|
||||||
if let Some(file) = &self.credentials {
|
|
||||||
file
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// List the end of life rules set in the configuration
|
/// List the end of life rules set in the configuration
|
||||||
pub fn list_rules(&self) -> Result<()> {
|
pub fn list_rules(&self) -> Result<()> {
|
||||||
for rule in self.rules.values() {
|
for rule in self.rules.values() {
|
||||||
|
|||||||
Reference in New Issue
Block a user