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/>. //! along with tas. If not, see <https://www.gnu.org/licenses/>.
//! //!
pub mod add;
pub mod edit; pub mod edit;
pub mod finish; pub mod finish;
pub mod list; pub mod list;
@ -32,12 +33,7 @@ pub struct Args {
#[derive(clap::Subcommand)] #[derive(clap::Subcommand)]
pub enum SubCommand { pub enum SubCommand {
Add { Add(self::add::Args),
#[clap(short, long)]
link: Option<String>,
name: String,
},
Edit(self::edit::Args), Edit(self::edit::Args),
Remove(self::remove::Args), Remove(self::remove::Args),
Priority(self::priority::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(); .unwrap_or_default();
match args.command { match args.command {
cli::SubCommand::Add { link, name } => { cli::SubCommand::Add(args) => {
tasks.push(Task { name, link }); cli::add::execute(cli::add::Request {
args,
let mut file = std::fs::File::create(&tasks_file_path).unwrap(); tasks,
file.write_all(&serde_json::to_vec(&tasks).unwrap()) tasks_file_path,
.unwrap(); });
println!("added");
} }
cli::SubCommand::Edit(args) => { cli::SubCommand::Edit(args) => {
cli::edit::execute(cli::edit::Request { cli::edit::execute(cli::edit::Request {