pmgr/commands/
add.rs

1use crate::{data, fg_color};
2use crate::{utils, Cli};
3use clap::builder::styling;
4use clap::error::ErrorKind;
5use clap::{Args, CommandFactory, Parser, Subcommand};
6
7#[derive(Subcommand)]
8pub enum Commands {
9    Add(Add),
10}
11
12#[derive(Parser)]
13pub struct Add {
14    #[structopt(subcommand)]
15    pub add_commands: AddCommands,
16}
17
18#[derive(Subcommand)]
19pub enum AddCommands {
20    /// Add a note to a group
21    Note(AddNoteArgs),
22    /// Add a task to a group
23    Task(AddTaskArgs),
24}
25
26#[derive(Args)]
27pub struct AddNoteArgs {
28    /// The group that you will add a note to
29    pub group_name: String,
30    /// The text that will show in the note
31    pub text: Vec<String>,
32}
33
34#[derive(Args)]
35pub struct AddTaskArgs {
36    /// The group that you will add a task to
37    pub group_name: String,
38    /// The text that will show in the task
39    pub text: Vec<String>,
40}
41
42impl super::Command for AddNoteArgs {
43    fn run(self, file_name: &str) {
44        let Some(mut data) = utils::get_data(file_name) else {
45            return;
46        };
47
48        if !data.groups.contains_key(&self.group_name) {
49            let _ = Cli::command()
50                .error(
51                    ErrorKind::InvalidValue,
52                    format!("Specified group `{}` does not exist", self.group_name),
53                )
54                .print();
55            return;
56        } else if self.text.is_empty() {
57            let _ = Cli::command()
58                .error(ErrorKind::MissingRequiredArgument, "No text was specified")
59                .print();
60            return;
61        }
62
63        let mut group = data.get_group(&self.group_name).clone();
64        group.notes.push(data::Note::new(&self.text.join(" ")));
65
66        data.groups.insert(self.group_name.clone(), group);
67        utils::write_data(file_name, &data);
68
69        println!("Added note to group `{}` {}", self.group_name, fg_color!("successfully", Green));
70    }
71}
72
73impl super::Command for AddTaskArgs {
74    fn run(self, file_name: &str) {
75        let Some(mut data) = utils::get_data(file_name) else {
76            return;
77        };
78
79        if !data.groups.contains_key(&self.group_name) {
80            let _ = Cli::command()
81                .error(
82                    ErrorKind::InvalidValue,
83                    format!("Specified group `{}` does not exist", self.group_name),
84                )
85                .print();
86            return;
87        } else if self.text.is_empty() {
88            let _ = Cli::command()
89                .error(ErrorKind::MissingRequiredArgument, "No text was specified")
90                .print();
91            return;
92        }
93
94        let mut group = data.get_group(&self.group_name).clone();
95        group.tasks.push(data::Task::new(&self.text.join(" ")));
96
97        data.groups.insert(self.group_name.clone(), group);
98        utils::write_data(file_name, &data);
99
100        println!(
101            "Added task to group `{}` {}",
102            self.group_name,
103            fg_color!("successfully", Green)
104        );
105    }
106}