Add repo select command

This commit is contained in:
Joscha 2025-05-03 01:43:47 +02:00
parent c9a7b6fa91
commit 11d03aac57
3 changed files with 41 additions and 1 deletions

View file

@ -2,6 +2,7 @@ mod add;
mod info;
mod list;
mod remove;
mod select;
use clap::Parser;
@ -18,6 +19,9 @@ pub enum Command {
#[command(visible_alias = "i")]
Info(info::Command),
#[command(visible_alias = "s")]
Select(select::Command),
#[command(visible_alias = "a")]
Add(add::Command),
@ -30,6 +34,7 @@ impl Command {
match self {
Self::List(command) => command.run(env),
Self::Info(command) => command.run(env),
Self::Select(command) => command.run(env),
Self::Add(command) => command.run(env),
Self::Remove(command) => command.run(env),
}

View file

@ -0,0 +1,32 @@
use clap::Parser;
use crate::Environment;
/// Select a repository.
#[derive(Debug, Parser)]
pub struct Command {
repo: 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)?;
if self.repo.is_empty() {
println!("Deselecting repo");
gdn::data::select_repo(&data, None)?;
return Ok(());
}
let Some(id) = state.resolve_repo_identifier(&self.repo) else {
println!("No repo found for identifier {}.", self.repo);
return Ok(());
};
println!("Selecting repo {id}");
gdn::data::select_repo(&data, Some(id))?;
Ok(())
}
}

View file

@ -12,7 +12,10 @@ 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, tidy},
v1::{
State, VERSION, add_repo, load_repo, load_repo_version, load_state, remove_repo,
select_repo, tidy,
},
};
fn migrate(dir: &LockedDataDir) -> anyhow::Result<()> {