diff --git a/gdn-cli/src/commands/repo.rs b/gdn-cli/src/commands/repo.rs index f3689ad..c39042a 100644 --- a/gdn-cli/src/commands/repo.rs +++ b/gdn-cli/src/commands/repo.rs @@ -2,6 +2,7 @@ mod add; mod info; mod list; mod remove; +mod rename; mod select; use clap::Parser; @@ -25,6 +26,9 @@ pub enum Command { #[command(visible_alias = "a")] Add(add::Command), + #[command(visible_alias = "rn")] + Rename(rename::Command), + #[command(visible_alias = "r")] Remove(remove::Command), } @@ -36,6 +40,7 @@ impl Command { Self::Info(command) => command.run(env), Self::Select(command) => command.run(env), Self::Add(command) => command.run(env), + Self::Rename(command) => command.run(env), Self::Remove(command) => command.run(env), } } diff --git a/gdn-cli/src/commands/repo/rename.rs b/gdn-cli/src/commands/repo/rename.rs new file mode 100644 index 0000000..66dbfbc --- /dev/null +++ b/gdn-cli/src/commands/repo/rename.rs @@ -0,0 +1,24 @@ +use clap::Parser; + +use crate::Environment; + +/// Rename an existing repository. +#[derive(Debug, Parser)] +pub struct Command { + repo: String, + name: 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(id) = state.resolve_repo_identifier(&self.repo) else { + println!("No repo found for identifier {}.", self.repo); + return Ok(()); + }; + gdn::data::rename_repo(&data, id, self.name.clone())?; + println!("Renamed repo {} ({id}) to {}.", self.repo, self.name); + Ok(()) + } +} diff --git a/gdn/src/data.rs b/gdn/src/data.rs index 1e8feed..6a1149f 100644 --- a/gdn/src/data.rs +++ b/gdn/src/data.rs @@ -13,8 +13,8 @@ pub use crate::repo::VERSION as REPO_VERSION; pub use self::{ datadir::{LockedDataDir, UnlockedDataDir}, v1::{ - State, VERSION, add_repo, load_repo, load_repo_version, load_state, remove_repo, save_repo, - select_repo, tidy, + State, VERSION, add_repo, load_repo, load_repo_version, load_state, remove_repo, + rename_repo, save_repo, select_repo, tidy, }, }; diff --git a/gdn/src/data/v1.rs b/gdn/src/data/v1.rs index 190df5f..ca4c873 100644 --- a/gdn/src/data/v1.rs +++ b/gdn/src/data/v1.rs @@ -111,7 +111,7 @@ pub fn remove_repo(dir: &LockedDataDir, id: RepoId) -> anyhow::Result<()> { Ok(()) } -pub fn set_repo_name(dir: &LockedDataDir, id: RepoId, name: String) -> anyhow::Result<()> { +pub fn rename_repo(dir: &LockedDataDir, id: RepoId, name: String) -> anyhow::Result<()> { let mut state = load_state(dir)?; *state .repos