diff --git a/gdn-cli/src/commands/repo.rs b/gdn-cli/src/commands/repo.rs index b8a3660..f3689ad 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 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), } diff --git a/gdn-cli/src/commands/repo/select.rs b/gdn-cli/src/commands/repo/select.rs new file mode 100644 index 0000000..05de702 --- /dev/null +++ b/gdn-cli/src/commands/repo/select.rs @@ -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(()) + } +} diff --git a/gdn/src/data.rs b/gdn/src/data.rs index b9d3771..39ede80 100644 --- a/gdn/src/data.rs +++ b/gdn/src/data.rs @@ -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<()> {