tas/src/cli/edit.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

2022-08-16 22:24:21 +03:00
use std::path::PathBuf;
use crate::cli::print_task_detail;
2022-08-05 23:40:26 +03:00
use crate::repo::{self, Repository};
2022-08-05 21:57:34 +03:00
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
name: Option<String>,
#[clap(short, long)]
group: Option<String>,
#[clap(long)]
no_group: bool,
2022-08-16 22:24:21 +03:00
#[clap(short, long)]
link: Option<String>,
#[clap(long)]
no_link: bool,
#[clap(short, long)]
path: Option<PathBuf>,
#[clap(long)]
no_path: bool,
2022-08-05 21:57:34 +03:00
idx: usize,
}
2022-08-16 15:43:44 +03:00
pub fn execute(repo: impl Repository, args: Args) {
match repo.get_current_task_opt() {
Ok(Some(_)) => {
return eprintln!("You can edit task only when you don't have an active task, yet")
}
Err(err) => {
return eprintln!("Cannot read current task: {}", err);
}
_ => {}
2022-08-05 21:57:34 +03:00
}
2022-08-05 23:40:26 +03:00
let res = repo.update_task(
args.idx,
repo::UpdateTaskData {
name: args.name,
group: args.no_group.then(|| None).or_else(|| args.group.map(Some)),
2022-08-16 22:24:21 +03:00
link: args.no_link.then(|| None).or_else(|| args.link.map(Some)),
path: args
.no_path
.then(|| None)
.or_else(|| args.path.map(std::fs::canonicalize).transpose().ok()),
2022-08-05 23:40:26 +03:00
},
);
match res {
Ok(task) => {
println!("The task was changed successfully");
print_task_detail(&task);
2022-08-05 23:40:26 +03:00
}
Err(err) => {
eprintln!("Cannot update the task: {}", err);
}
2022-08-05 21:57:34 +03:00
}
}