cli: extract add subcommand

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 22:05:22 +03:00
parent ec880dad6e
commit f9d2761700
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
3 changed files with 35 additions and 14 deletions

View file

@ -14,6 +14,7 @@
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
//!
pub mod add;
pub mod edit;
pub mod finish;
pub mod list;
@ -32,12 +33,7 @@ pub struct Args {
#[derive(clap::Subcommand)]
pub enum SubCommand {
Add {
#[clap(short, long)]
link: Option<String>,
name: String,
},
Add(self::add::Args),
Edit(self::edit::Args),
Remove(self::remove::Args),
Priority(self::priority::Args),

View file

@ -0,0 +1,27 @@
use std::path::PathBuf;
use crate::Task;
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
link: Option<String>,
name: String,
}
pub struct Request {
pub args: Args,
pub tasks: Vec<Task>,
pub tasks_file_path: PathBuf,
}
pub fn execute(mut req: Request) {
tasks.push(Task { name, link });
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
println!("added");
}

View file

@ -52,14 +52,12 @@ fn main() {
.unwrap_or_default();
match args.command {
cli::SubCommand::Add { link, name } => {
tasks.push(Task { name, link });
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
println!("added");
cli::SubCommand::Add(args) => {
cli::add::execute(cli::add::Request {
args,
tasks,
tasks_file_path,
});
}
cli::SubCommand::Edit(args) => {
cli::edit::execute(cli::edit::Request {