From e2436328b79ff9408a1ad540516046c4fdf0bb89 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 3 May 2025 16:36:58 +0200 Subject: [PATCH] Add note add command --- gdn-cli/src/commands/note.rs | 4 ++++ gdn-cli/src/commands/note/add.rs | 34 +++++++++++++++++++++++++++++++ gdn-cli/src/commands/note/list.rs | 2 +- gdn/src/lib.rs | 2 +- gdn/src/repo.rs | 2 +- 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 gdn-cli/src/commands/note/add.rs diff --git a/gdn-cli/src/commands/note.rs b/gdn-cli/src/commands/note.rs index 0bb7887..3b6a380 100644 --- a/gdn-cli/src/commands/note.rs +++ b/gdn-cli/src/commands/note.rs @@ -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), } } } diff --git a/gdn-cli/src/commands/note/add.rs b/gdn-cli/src/commands/note/add.rs new file mode 100644 index 0000000..431826f --- /dev/null +++ b/gdn-cli/src/commands/note/add.rs @@ -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(()) + } +} diff --git a/gdn-cli/src/commands/note/list.rs b/gdn-cli/src/commands/note/list.rs index 0a0d4f7..4ee094b 100644 --- a/gdn-cli/src/commands/note/list.rs +++ b/gdn-cli/src/commands/note/list.rs @@ -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 {} diff --git a/gdn/src/lib.rs b/gdn/src/lib.rs index 23ee8d5..fdab218 100644 --- a/gdn/src/lib.rs +++ b/gdn/src/lib.rs @@ -1,6 +1,6 @@ pub mod data; pub mod ids; -mod repo; +pub mod repo; pub const PROPER_NAME: &str = "GedächtNAS"; pub const TECHNICAL_NAME: &str = "gedaechtnas"; diff --git a/gdn/src/repo.rs b/gdn/src/repo.rs index 3595f33..733a1f3 100644 --- a/gdn/src/repo.rs +++ b/gdn/src/repo.rs @@ -7,7 +7,7 @@ use anyhow::{anyhow, bail}; use git2::{Commit, ErrorCode, Oid, Reference, Repository}; use jiff::Zoned; -pub use self::v1::{Repo, VERSION}; +pub use self::v1::{Note, Repo, VERSION}; const VERSION_FILE: &str = "VERSION";