add path to task

Closes #3
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-16 22:24:21 +03:00
parent 8d21df094c
commit 238e4b0b03
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
9 changed files with 69 additions and 27 deletions

View file

@ -57,4 +57,8 @@ pub fn print_task_detail(task: &domain::Task) {
if let Some(link) = task.link.as_ref() {
println!(" link: {}", link);
}
if let Some(path) = task.path.as_ref() {
println!(" path: {}", path.to_string_lossy());
}
}

View file

@ -5,11 +5,14 @@ use crate::{
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
group: Option<String>,
#[clap(short, long)]
link: Option<String>,
#[clap(short, long)]
group: Option<String>,
path: Option<String>,
name: String,
}
@ -19,6 +22,7 @@ pub fn execute(repo: impl Repository, args: Args) {
name: args.name,
link: args.link,
group: args.group,
path: args.path.map(std::fs::canonicalize).transpose().unwrap(),
index: None,
});

View file

@ -1,13 +1,10 @@
use std::path::PathBuf;
use crate::cli::print_task_detail;
use crate::repo::{self, Repository};
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
link: Option<String>,
#[clap(long)]
no_link: bool,
#[clap(short, long)]
name: Option<String>,
@ -16,6 +13,16 @@ pub struct Args {
#[clap(long)]
no_group: bool,
#[clap(short, long)]
link: Option<String>,
#[clap(long)]
no_link: bool,
#[clap(short, long)]
path: Option<PathBuf>,
#[clap(long)]
no_path: bool,
idx: usize,
}
@ -34,8 +41,12 @@ pub fn execute(repo: impl Repository, args: Args) {
args.idx,
repo::UpdateTaskData {
name: args.name,
link: args.no_link.then(|| None).or_else(|| args.link.map(Some)),
group: args.no_group.then(|| None).or_else(|| args.group.map(Some)),
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()),
},
);
match res {

View file

@ -12,13 +12,6 @@ pub fn execute(repo: impl Repository) {
Ok(task) => task,
};
match repo.stop_task() {
Ok(_) => {
println!("The task was paused successfully");
print_task_detail(&task);
}
Err(err) => {
eprintln!("Cannot pause the task: {}", err);
}
}
println!("The task was paused successfully");
print_task_detail(&task);
}

View file

@ -59,8 +59,9 @@ pub fn execute(repo: impl Repository, args: Args) {
let res = repo.insert_task(repo::InsertTaskData {
index: Some(new_idx),
name: target.name,
link: target.link,
group: target.group,
link: target.link,
path: target.path,
});
match res {
Ok(task) => {

View file

@ -8,6 +8,9 @@ pub struct Args {
#[clap(short, long)]
open: bool,
#[clap(long)]
print_path: bool,
idx: Option<usize>,
}
@ -18,8 +21,17 @@ pub fn execute(repo: impl Repository, args: Args) {
Err(err) => return eprintln!("Cannot start task: {}", err),
};
println!("The task was started successfully");
print_task_detail(&task);
if args.print_path {
println!(
"{}",
task.path
.unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir"))
.to_string_lossy()
)
} else {
println!("The task was started successfully");
print_task_detail(&task);
}
if let (Some(link), true) = (task.link.as_ref(), args.open) {
log::debug!("opening link...");

View file

@ -1,9 +1,12 @@
use std::path::PathBuf;
pub type TaskId = usize;
pub struct Task {
pub name: String,
pub link: Option<String>,
pub group: Option<String>,
pub link: Option<String>,
pub path: Option<PathBuf>,
// created_at
}

View file

@ -1,5 +1,7 @@
pub mod fs;
use std::path::PathBuf;
use crate::domain;
#[derive(Debug)]
@ -23,15 +25,17 @@ impl std::error::Error for Error {}
pub struct InsertTaskData {
pub name: String,
pub link: Option<String>,
pub group: Option<String>,
pub link: Option<String>,
pub path: Option<PathBuf>,
pub index: Option<usize>,
}
pub struct UpdateTaskData {
pub name: Option<String>,
pub link: Option<Option<String>>,
pub group: Option<Option<String>>,
pub link: Option<Option<String>>,
pub path: Option<Option<PathBuf>>,
}
pub trait Repository {

View file

@ -1,5 +1,6 @@
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use crate::domain;
use crate::repo::{Error, InsertTaskData, Repository, UpdateTaskData};
@ -10,8 +11,9 @@ use xdg::BaseDirectories;
#[derive(Deserialize, Serialize, Clone)]
pub struct Task {
name: String,
link: Option<String>,
group: Option<String>,
link: Option<String>,
path: Option<String>,
// created_at
}
@ -19,8 +21,9 @@ impl From<Task> for domain::Task {
fn from(repo: Task) -> Self {
domain::Task {
name: repo.name,
link: repo.link,
group: repo.group,
link: repo.link,
path: repo.path.map(PathBuf::from),
}
}
}
@ -102,12 +105,16 @@ impl Repository for FsRepo {
task.name = name;
}
if let Some(group) = update_data.group {
task.group = group;
}
if let Some(link) = update_data.link {
task.link = link;
}
if let Some(group) = update_data.group {
task.group = group;
if let Some(path) = update_data.path {
task.path = path.and_then(|p| p.into_os_string().into_string().ok());
}
let new_task = task.clone();
@ -120,8 +127,11 @@ impl Repository for FsRepo {
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error> {
let new_task = Task {
name: insert_data.name,
link: insert_data.link,
group: insert_data.group,
link: insert_data.link,
path: insert_data
.path
.and_then(|p| p.into_os_string().into_string().ok()),
};
let mut tasks = self.get_tasks_impl()?;