add pause, finish and status subcommands

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 13:06:48 +03:00
parent 92e4ae4dc2
commit e16011950d
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215

View file

@ -13,6 +13,8 @@ fn main() {
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
let finished_tasks_file_path = xdg_dirs.place_data_file("finished_data.json").unwrap();
let tasks_file_path = xdg_dirs.place_data_file("data.json").unwrap();
let mut tasks: Vec<Task> = std::fs::File::open(&tasks_file_path)
.map(|file| serde_json::from_reader(file).unwrap())
@ -41,7 +43,7 @@ fn main() {
no_link,
} => {
if current_task_info.is_some() {
panic!("You can change priority only when you don't have an active task, yet");
panic!("You can edit task only when you don't have an active task, yet");
}
if idx == 0 || idx > tasks.len() {
@ -66,16 +68,20 @@ fn main() {
}
cli::SubCommand::Remove { idx } => {
if current_task_info.is_some() {
panic!("You can change priority only when you don't have an active task, yet");
panic!("You can remove task only when you don't have an active task, yet");
}
if idx == 0 || idx > tasks.len() {
panic!("invalid index");
}
let task = &tasks[idx - 1];
println!("You are deleting task:");
println!(" {}", tasks[idx - 1].name);
println!();
println!(" {}", task.name);
if let Some(ref link) = task.link {
println!(" link: {}", link);
}
println!("In most cases you need to `finish` command");
loop {
print!("Do you still want to delete the task? (y/N): ");
@ -189,30 +195,54 @@ fn main() {
*/
}
cli::SubCommand::Pause => {
println!("pausing...");
if current_task_info.take().is_none() {
panic!("You can use the pause subcommand only when you have an active task");
}
let mut file = std::fs::File::create(&current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&current_task_info).unwrap())
.unwrap();
println!("paused");
}
cli::SubCommand::Finish => {
println!("finishing...");
println!("-- on finish hook found");
println!("-- running...");
println!("> curl ...");
println!("-- status was changed to \"Deploy\" successfuly");
println!("finished");
}
cli::SubCommand::Status => {
println!("Information about your current task:");
println!("[work]");
println!("https://redmine.example.com/issues/4051");
println!(
"#4051 Plannig/Assignment: Assign week and day general delvirebles after regular"
);
println!("-------------------------------------------------------------------------");
println!("lorem impsum dolor dolor impsum lorem lorem impsum");
println!("lorem dolor impsum lorem lorem impsum");
println!("lorem dolor impsum lorem lorem impsum lorem impsum dolor dolor impsum lorem");
println!("lorem impsum");
}
cli::SubCommand::Finish => match current_task_info.take() {
None => {
panic!("You can use the finish subcommand only when you have an active task")
}
Some(info) => {
let mut finished_tasks: Vec<Task> = std::fs::File::open(&finished_tasks_file_path)
.map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default();
let task = tasks.remove(info.task_idx - 1);
finished_tasks.push(task);
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
let mut file = std::fs::File::create(&finished_tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&finished_tasks).unwrap())
.unwrap();
let mut file = std::fs::File::create(&current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&current_task_info).unwrap())
.unwrap();
println!("finished");
}
},
cli::SubCommand::Status => match current_task_info {
None => {
panic!("You don't have an active task.");
}
Some(info) => {
println!("Information about your current task:");
println!(" {}", info.task.name);
if let Some(link) = info.task.link {
println!(" link: {}", link);
}
}
},
}
}