Assert Taskwarrior data location is custom

This commit is contained in:
2021-12-24 10:13:39 +01:00
parent 5d9bac5cd1
commit dcdbbc785b
3 changed files with 57 additions and 28 deletions

View File

@@ -16,6 +16,7 @@ async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();
let port = env::var("PORT").unwrap_or("8000".to_string()); let port = env::var("PORT").unwrap_or("8000".to_string());
taskwarrior::load_config(None);
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).expect("Failed to bind port"); let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).expect("Failed to bind port");
run(listener)?.await run(listener)?.await

View File

@@ -1,7 +1,10 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use configparser::ini::Ini;
use serde::{de, Deserialize, Deserializer, Serialize}; use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json; use serde_json;
use std::env;
use std::io::Error; use std::io::Error;
use std::path::Path;
use std::process::Command; use std::process::Command;
use std::str; use std::str;
use uuid::Uuid; use uuid::Uuid;
@@ -128,6 +131,51 @@ pub mod opt_tw_date_format {
} }
} }
pub fn load_config(task_data_location: Option<&str>) -> String {
if let Ok(taskrc_location) = env::var("TASKRC") {
let mut taskrc = Ini::new();
taskrc
.load(&taskrc_location)
.expect(&format!("Cannot load taskrc file {}", taskrc_location));
return taskrc.get("default", "data.location").expect(&format!(
"'data.location' must be set in taskrc file {}",
taskrc_location
));
}
let data_location = task_data_location
.map(|s| s.to_string())
.unwrap_or_else(|| {
env::var("TASK_DATA_LOCATION")
.expect("Expecting TASKRC or TASK_DATA_LOCATION environment variable value")
});
let mut taskrc = Ini::new();
taskrc.setstr("default", "uda.contextswitch.type", Some("string"));
taskrc.setstr(
"default",
"uda.contextswitch.label",
Some("Context Switch metadata"),
);
taskrc.setstr("default", "uda.contextswitch.default", Some("{}"));
taskrc.setstr("default", "data.location", Some(&data_location));
taskrc.setstr("default", "uda.contextswitch.type", Some("string"));
taskrc.setstr(
"default",
"uda.contextswitch.label",
Some("Context Switch metadata"),
);
taskrc.setstr("default", "uda.contextswitch.default", Some("{}"));
let taskrc_path = Path::new(&data_location).join(".taskrc");
let taskrc_location = taskrc_path.to_str().unwrap();
taskrc.write(taskrc_location).unwrap();
env::set_var("TASKRC", taskrc_location);
return data_location;
}
pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> { pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> {
let mut args = vec!["export"]; let mut args = vec!["export"];
args.extend(filters); args.extend(filters);

View File

@@ -1,45 +1,25 @@
use configparser::ini::Ini; use contextswitch_api::taskwarrior;
use mktemp::Temp; use mktemp::Temp;
use std::env;
use std::fs; use std::fs;
use std::net::TcpListener; use std::net::TcpListener;
use std::path::PathBuf;
pub fn spawn_app() -> String { pub fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port(); let port = listener.local_addr().unwrap().port();
let server = contextswitch::run(listener).expect("Failed to bind address"); let server = contextswitch_api::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server); let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port) format!("http://127.0.0.1:{}", port)
} }
pub fn setup_tasks() -> PathBuf { pub fn setup_tasks() -> String {
let mut config = Ini::new();
config.setstr("default", "uda.contextswitch.type", Some("string"));
config.setstr(
"default",
"uda.contextswitch.label",
Some("Context Switch metadata"),
);
config.setstr("default", "uda.contextswitch.default", Some("{}"));
let tmp_dir = Temp::new_dir().unwrap(); let tmp_dir = Temp::new_dir().unwrap();
let task_data_path = tmp_dir.to_path_buf(); let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
config.setstr(
"default",
"data.location",
Some(task_data_path.to_str().unwrap()),
);
let taskrc_path = task_data_path.join(".taskrc");
config.write(taskrc_path.as_path()).unwrap();
env::set_var("TASKRC", taskrc_path.to_str().unwrap());
tmp_dir.release(); tmp_dir.release();
return task_data_path;
return task_data_location;
} }
pub fn clear_tasks(task_data_path: PathBuf) { pub fn clear_tasks(task_data_location: String) {
fs::remove_dir_all(task_data_path).unwrap(); fs::remove_dir_all(task_data_location).unwrap();
} }