Skip to content

Commit

Permalink
Merge pull request #42 from DeterminateSystems/list-label
Browse files Browse the repository at this point in the history
Enable listing flakes by label
  • Loading branch information
lucperkins authored Sep 28, 2023
2 parents 60c965c + 4a5f6d3 commit 392d43c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/cli/cmd/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub(super) struct Release {
enum Subcommands {
/// Lists all currently public flakes on FlakeHub.
Flakes,
/// Lists all public flakes with the provided label.
Label { label: String },
/// Lists all currently public organizations on FlakeHub.
Orgs,
/// List all releases for a specific flake on FlakeHub.
Expand Down Expand Up @@ -148,6 +150,42 @@ impl CommandExecute for ListSubcommand {
Err(e) => return Err(e.into()),
}
}
Label { label } => {
if string_has_whitespace(&label) {
return Err(FhError::LabelParse(String::from("whitespace not allowed")).into());
}

let label = label.to_lowercase();

match client.flakes_by_label(&label).await {
Ok(flakes) => {
if flakes.is_empty() {
eprintln!("No results");
} else if self.json {
print_json(&flakes)?;
} else {
let mut table = Table::new();
table.set_format(*TABLE_FORMAT);

table.set_titles(row!["Flake", "FlakeHub URL"]);

for flake in flakes {
table.add_row(Row::new(vec![
Cell::new(&flake.name()).with_style(Attr::Bold),
Cell::new(&flake.url()).with_style(Attr::Dim),
]));
}

if std::io::stdout().is_terminal() {
table.printstd();
} else {
table.to_csv(std::io::stdout())?;
}
}
}
Err(e) => return Err(e.into()),
}
}
Orgs => {
let pb = ProgressBar::new_spinner();
pb.set_style(ProgressStyle::default_spinner());
Expand Down Expand Up @@ -286,3 +324,7 @@ impl CommandExecute for ListSubcommand {
Ok(ExitCode::SUCCESS)
}
}

fn string_has_whitespace(s: &str) -> bool {
s.chars().any(char::is_whitespace)
}
27 changes: 25 additions & 2 deletions src/cli/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use reqwest::Client as HttpClient;
use serde::Serialize;

use self::{
list::Flake,
list::{Org, Release, Version},
list::{Flake, Org, Release, Version},
search::SearchResult,
};

Expand Down Expand Up @@ -62,6 +61,9 @@ pub(super) enum FhError {
#[error("json parsing error: {0}")]
Json(#[from] serde_json::Error),

#[error("label parsing error: {0}")]
LabelParse(String),

#[error("the flake has no inputs")]
NoInputs,

Expand Down Expand Up @@ -128,6 +130,27 @@ impl FlakeHubClient {
Ok(flakes)
}

async fn flakes_by_label(&self, label: &str) -> Result<Vec<Flake>, FhError> {
let mut url = self.api_addr.clone();
{
let mut segs = url
.path_segments_mut()
.expect("flakehub url cannot be base (this should never happen)");

segs.push("label").push(label);
}

let flakes = self
.client
.get(&url.to_string())
.send()
.await?
.json::<Vec<Flake>>()
.await?;

Ok(flakes)
}

async fn releases(&self, org: &str, project: &str) -> Result<Vec<Release>, FhError> {
let mut url = self.api_addr.clone();
{
Expand Down

0 comments on commit 392d43c

Please sign in to comment.