From f73a7dfa290dadd1e9ec109518da511f39b551c1 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 17 Jul 2023 01:24:52 +0100 Subject: [PATCH] feat: add proper env vars for home and the csv --- .gitignore | 7 ++----- flake.nix | 4 +++- src/bin/update_groups.rs | 9 +++------ src/lib.rs | 12 +++++++++++- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 504e98f..7ce39aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,11 @@ /target - /.idea - .env - *.db - # flakes result /result -tmp.* \ No newline at end of file +tmp.* +*.csv \ No newline at end of file diff --git a/flake.nix b/flake.nix index 684d53e..1de2a4d 100644 --- a/flake.nix +++ b/flake.nix @@ -51,7 +51,9 @@ LDAP_ADMIN = cfg.ldap.admin; # basic dserver stuff - DATABASE = "${cfg.home}/database.db"; + HOME = cfg.home; + DATABASE = "database.db"; + CSV = "wolves.csv"; HOST_PORT = cfg.host_port; # special categories of users diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 22006c9..c74f023 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -35,7 +35,6 @@ async fn update_users(config: &Config) -> tide::Result<()> { for every valid user in wolves match to ldap add to users */ - println!("{:#?}", users_tmp); // pull from wolves csv for user in from_csv(config).await.unwrap_or_default() { users_tmp.insert(user); @@ -48,8 +47,6 @@ async fn update_users(config: &Config) -> tide::Result<()> { } } - println!("{:#?}", users_tmp); - // easier to work with Strings above but easier to work with &str below let users: Vec<&str> = users_tmp.iter().map(|s| &**s).collect(); @@ -184,7 +181,7 @@ async fn from_csv(config: &Config) -> Result, Box> { let mut uids = HashSet::new(); let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?; - let records = read_csv()?; + let records = read_csv(config)?; let now = Utc::now(); let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day()); @@ -218,10 +215,10 @@ struct Record { expiry: String, } -fn read_csv() -> Result, Box> { +fn read_csv(config: &Config) -> Result, Box> { let mut records: Vec = vec![]; - if let Ok(mut rdr) = csv::Reader::from_path("tmp.csv") { + if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.csv)) { for result in rdr.deserialize() { // Notice that we need to provide a type hint for automatic // deserialization. diff --git a/src/lib.rs b/src/lib.rs index 587dce1..1dab83c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ pub struct Accounts { } pub async fn db_init(config: &Config) -> Result, Error> { - let database = &config.database; + let database = format!("{}/{}", &config.home, &config.database); let pool = SqlitePoolOptions::new() .max_connections(5) .connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true)) @@ -91,7 +91,9 @@ pub struct Config { pub ldap_host: String, pub ldap_admin: String, pub ldap_admin_pw: String, + pub home: String, pub database: String, + pub csv: String, pub host_port: String, } @@ -103,7 +105,9 @@ pub fn get_config() -> Config { ldap_host: "".to_string(), ldap_admin: "".to_string(), ldap_admin_pw: "".to_string(), + home: ".".to_string(), database: "database.db".to_string(), + csv: "wolves.csv".to_string(), host_port: "127.0.0.1:8087".to_string(), }; @@ -116,9 +120,15 @@ pub fn get_config() -> Config { if let Ok(x) = env::var("LDAP_ADMIN_PW") { config.ldap_admin_pw = x.trim().to_string(); } + if let Ok(x) = env::var("HOME") { + config.home = x.trim().to_string(); + } if let Ok(x) = env::var("DATABASE") { config.database = x.trim().to_string(); } + if let Ok(x) = env::var("CSV") { + config.csv = x.trim().to_string(); + } if let Ok(x) = env::var("HOST_PORT") { config.host_port = x.trim().to_string(); }