Add repo add and repo show commands

This commit is contained in:
Joscha 2025-04-30 00:49:04 +02:00
parent b922af9283
commit 2c5ff584db
10 changed files with 219 additions and 4 deletions

View file

@ -2,6 +2,7 @@ use clap::Parser;
use crate::Environment;
mod repo;
mod status;
mod tidy;
@ -9,6 +10,10 @@ mod tidy;
pub enum Command {
Status(status::Command),
Tidy(tidy::Command),
Repo {
#[command(subcommand)]
command: repo::Command,
},
}
impl Command {
@ -16,6 +21,7 @@ impl Command {
match self {
Self::Status(command) => command.run(env),
Self::Tidy(command) => command.run(env),
Self::Repo { command } => command.run(env),
}
}
}

View file

@ -0,0 +1,22 @@
mod add;
mod show;
use clap::Parser;
use crate::Environment;
/// Perform repo operations.
#[derive(Debug, Parser)]
pub enum Command {
Show(show::Command),
Add(add::Command),
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
match self {
Self::Show(command) => command.run(env),
Self::Add(command) => command.run(env),
}
}
}

View file

@ -0,0 +1,18 @@
use clap::Parser;
use crate::Environment;
/// Add a new repository.
#[derive(Debug, Parser)]
pub struct Command {
name: String,
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
let data = gdn::data::open_and_migrate(env.data_dir.clone())?;
let id = gdn::data::add_repo(&data, self.name.clone())?;
println!("Added repo {} ({id}).", self.name);
Ok(())
}
}

View file

@ -0,0 +1,30 @@
use clap::Parser;
use gdn::data::REPO_VERSION;
use crate::Environment;
/// Show info about 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(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(());
};
let version = gdn::data::load_repo_version(&data, id)?;
let repo = gdn::data::load_repo(&data, id)?;
println!("Repo version: {version} (latest: {REPO_VERSION})",);
println!("Number of notes: {}", repo.notes.len());
Ok(())
}
}