diff --git a/rust/lon/src/cli.rs b/rust/lon/src/cli.rs index b1efe1d..8cd9228 100644 --- a/rust/lon/src/cli.rs +++ b/rust/lon/src/cli.rs @@ -61,6 +61,8 @@ enum Commands { Freeze(SourceArgs), /// Unfreeze an existing source Unfreeze(SourceArgs), + /// Show the list of sources or a specific source + Show { name: Option }, /// Bot that opens PRs for updates Bot { @@ -243,6 +245,7 @@ impl Commands { Self::Remove(args) => remove(directory, &args), Self::Freeze(args) => freeze(directory, &args), Self::Unfreeze(args) => unfreeze(directory, &args), + Self::Show { name } => show(directory, name), Self::Bot { commands } => match commands { BotCommands::GitLab => bot(directory, &GitLab::from_env()?), @@ -484,6 +487,27 @@ fn unfreeze(directory: impl AsRef, args: &SourceArgs) -> Result<()> { Ok(()) } +fn show(directory: impl AsRef, name: Option) -> Result<()> { + let mut sources = Sources::read(&directory)?; + + match name { + Some(name) => { + let Some(source) = sources.get_mut(&name) else { + bail!("Source {name} doesn't exist") + }; + + println!("{name}:\n{source}"); + } + None => { + for (name, source) in sources.iter() { + println!("{name}:\n{source}"); + } + } + } + + Ok(()) +} + fn bot(directory: impl AsRef, forge: &impl Forge) -> Result<()> { let base_ref = git::current_rev(&directory)?; diff --git a/rust/lon/src/sources.rs b/rust/lon/src/sources.rs index 6ec01ad..c96cba5 100644 --- a/rust/lon/src/sources.rs +++ b/rust/lon/src/sources.rs @@ -1,4 +1,8 @@ -use std::{collections::BTreeMap, path::Path}; +use std::{ + collections::{BTreeMap, btree_map::Iter}, + fmt, + path::Path, +}; use anyhow::{Context, Result, bail}; use nix_compat::nixhash::NixHash; @@ -128,6 +132,11 @@ impl Sources { pub fn names(&self) -> Vec<&String> { self.map.keys().collect() } + + /// Return an iterator over the sources + pub fn iter(&self) -> Iter<'_, String, Source> { + self.map.iter() + } } #[derive(Clone)] @@ -207,6 +216,22 @@ impl Source { } } +impl fmt::Display for Source { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Source::Git(s) => { + write!(f, " Type: Git\n{s}") + } + Source::GitHub(s) => { + write!(f, " Type: GitHub\n{s}") + } + Source::Tarball(s) => { + write!(f, " Type: Tarball\n{s}") + } + } + } +} + #[derive(Clone)] pub struct GitSource { url: String, @@ -221,6 +246,19 @@ pub struct GitSource { frozen: bool, } +impl fmt::Display for GitSource { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + [ + ("Url", &self.url), + ("Branch", &self.branch), + ("Revision", &self.revision.as_str().into()), + ("Frozen", &self.frozen.to_string()), + ] + .iter() + .try_for_each(|(k, v)| writeln!(f, " {k}: {v}")) + } +} + impl GitSource { pub fn new( url: &str, @@ -349,6 +387,19 @@ pub struct GitHubSource { frozen: bool, } +impl fmt::Display for GitHubSource { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + [ + ("Repository", &format!("{}/{}", &self.owner, &self.repo)), + ("Branch", &self.branch), + ("Revision", &self.revision.as_str().into()), + ("Frozen", &self.frozen.to_string()), + ] + .iter() + .try_for_each(|(k, v)| writeln!(f, " {k}: {v}")) + } +} + impl GitHubSource { pub fn new( owner: &str, @@ -485,6 +536,17 @@ pub struct TarballSource { frozen: bool, } +impl fmt::Display for TarballSource { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(origin) = &self.origin { + writeln!(f, " Origin: {origin}")?; + } + [("Locked", &self.url), ("Frozen", &self.frozen.to_string())] + .iter() + .try_for_each(|(k, v)| writeln!(f, " {k}: {v}")) + } +} + #[derive(Clone)] struct TarballFlakeRef { url: String,