Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable listing flakes by label #42

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.clone()) {
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: String) -> bool {
lucperkins marked this conversation as resolved.
Show resolved Hide resolved
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