Add repo rename command

This commit is contained in:
Joscha 2025-05-03 16:29:50 +02:00
parent 357de970ee
commit 3390526522
4 changed files with 32 additions and 3 deletions

View file

@ -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),
}
}

View file

@ -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(())
}
}

View file

@ -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,
},
};

View file

@ -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