Add note add command

This commit is contained in:
Joscha 2025-05-03 16:36:58 +02:00
parent 3390526522
commit e2436328b7
5 changed files with 41 additions and 3 deletions

View file

@ -1,3 +1,4 @@
mod add;
mod list;
use clap::Parser;
@ -9,12 +10,15 @@ use crate::Environment;
pub enum Command {
#[command(visible_alias = "l")]
List(list::Command),
#[command(visible_alias = "a")]
Add(add::Command),
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
match self {
Self::List(command) => command.run(env),
Self::Add(command) => command.run(env),
}
}
}

View file

@ -0,0 +1,34 @@
use clap::Parser;
use gdn::ids::NoteId;
use gdn::repo::Note;
use crate::Environment;
/// Add a note to the selected repository.
#[derive(Debug, Parser)]
pub struct Command {
text: String,
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
let data = gdn::data::open_and_migrate(env.data_dir.clone())?;
let state = gdn::data::load_state(&data)?;
let Some(selected) = state.selected_repo else {
println!("No repo selected");
return Ok(());
};
let mut repo = gdn::data::load_repo(&data, selected)?;
repo.notes.push(Note {
id: NoteId::new(),
text: self.text,
children: vec![],
});
let oid = gdn::data::save_repo(&data, selected, repo)?;
println!("Note added ({oid}).");
Ok(())
}
}

View file

@ -2,7 +2,7 @@ use clap::Parser;
use crate::Environment;
/// List all notes in the current repository.
/// List all notes in the selected repository.
#[derive(Debug, Parser)]
pub struct Command {}