Skip to content

Commit

Permalink
List all the crates in the registry on the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed May 20, 2024
1 parent cdef6a0 commit 4639fde
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
34 changes: 34 additions & 0 deletions integration-tests/spec/features/cli_crate_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'scratch_space'

RSpec.describe 'The CLI crate list' do
let(:scratch) { ScratchSpace.new }
let(:registry) { scratch.registry }

before { registry.start }

after do
registry.stop
scratch.cleanup
end

it 'shows the crate and version' do
data = [['alpha', '1.0.0'], ['alpha', '1.1.1'], ['beta', '2.2.2']]

data.each do |name, version|
scratch
.crate(name:, version:)
.lib_rs(%(pub const ID: &str = "#{name}--#{version}";))
.publish_to(registry)
end

output = registry.list

aggregate_failures do
data.each do |name, version|
expect(output).to match(/#{Regexp.quote(name)}.*#{Regexp.quote(version)}/)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'scratch_space'

RSpec.describe 'The crate list', type: :feature do
RSpec.describe 'The HTML crate list', type: :feature do
let(:scratch) { ScratchSpace.new }
let(:registry) { scratch.registry }

Expand Down
11 changes: 11 additions & 0 deletions integration-tests/spec/support/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def yank(name:, version:)
)
end

def list
cmd = [
MARGO_BINARY,
'list',
'--registry',
@root.to_s,
]

IO.popen(cmd, exception: true, &:read)
end

def url
@server.url
end
Expand Down
71 changes: 68 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use snafu::prelude::*;
use std::{
collections::{BTreeMap, BTreeSet},
fmt,
fs::{self, File},
io::{self, BufRead, BufReader, BufWriter, Read, Write},
path::{Component, Path, PathBuf},
Expand All @@ -27,8 +28,9 @@ enum Subcommand {
Init(InitArgs),
Add(AddArgs),
Remove(RemoveArgs),
GenerateHtml(GenerateHtmlArgs),
Yank(YankArgs),
List(ListArgs),
GenerateHtml(GenerateHtmlArgs),
}

/// Initialize a new registry
Expand Down Expand Up @@ -123,6 +125,16 @@ struct YankArgs {
name: CrateName,
}

/// List all crates and their versions in the registry
#[derive(Debug, argh::FromArgs)]
#[argh(subcommand)]
#[argh(name = "list")]
struct ListArgs {
/// path to the registry to list
#[argh(option)]
registry: PathBuf,
}

#[snafu::report]
fn main() -> Result<(), Error> {
let args: Args = argh::from_env();
Expand All @@ -134,8 +146,9 @@ fn main() -> Result<(), Error> {
Subcommand::Init(init) => do_init(global, init)?,
Subcommand::Add(add) => do_add(global, add)?,
Subcommand::Remove(rm) => do_remove(global, rm)?,
Subcommand::GenerateHtml(html) => do_generate_html(global, html)?,
Subcommand::Yank(yank) => do_yank(global, yank)?,
Subcommand::List(list) => do_list(global, list)?,
Subcommand::GenerateHtml(html) => do_generate_html(global, html)?,
}

Ok(())
Expand Down Expand Up @@ -325,6 +338,52 @@ fn do_yank(_global: &Global, yank: YankArgs) -> Result<(), Error> {
Ok(())
}

fn do_list(_global: &Global, list: ListArgs) -> Result<(), Error> {
let r = Registry::open(list.registry)?;

let crates = r.list_all().unwrap();

#[derive(Default)]
struct Max(usize, String);

impl Max {
fn push(&mut self, v: impl fmt::Display) {
use std::fmt::Write;

let Self(m, s) = self;

s.clear();
_ = write!(s, "{v}");
*m = usize::max(*m, s.len());
}

fn max(&self) -> usize {
self.0
}
}

let mut max_c = Max::default();
let mut max_v = Max::default();

for (crate_, versions) in &crates {
max_c.push(crate_);
for version in versions.keys() {
max_v.push(version);
}
}

let max_c = max_c.max();
let max_v = max_v.max();

for (crate_, versions) in crates {
for version in versions.keys() {
println!("{crate_:<max_c$} {version:<max_v$}");
}
}

Ok(())
}

#[derive(Debug)]
struct Registry {
path: PathBuf,
Expand Down Expand Up @@ -1301,7 +1360,7 @@ mod common {
use snafu::prelude::*;
use std::{
borrow::Cow,
ops,
fmt, ops,
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -1341,6 +1400,12 @@ mod common {
}
}

impl fmt::Display for CrateName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl FromStr for CrateName {
type Err = CrateNameError;

Expand Down

0 comments on commit 4639fde

Please sign in to comment.