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

Trim trailing /s from flakerefs #67

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Changes from all 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
26 changes: 16 additions & 10 deletions src/cli/cmd/add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async fn infer_flake_input_name_url(
flake_ref: String,
input_name: Option<String>,
) -> color_eyre::Result<(String, url::Url)> {
let flake_ref = flake_ref.trim_end_matches('/');
Hoverbear marked this conversation as resolved.
Show resolved Hide resolved
let url_result = flake_ref.parse::<url::Url>();

match url_result {
Expand All @@ -143,15 +144,19 @@ async fn infer_flake_input_name_url(
[org, project, version] => {
let version = version.strip_suffix(".tar.gz").unwrap_or(version);
let version = version.strip_prefix('v').unwrap_or(version);
semver::VersionReq::parse(version).map_err(|_| {
color_eyre::eyre::eyre!(
"version '{version}' was not a valid SemVer version requirement"
)
})?;

(org, project, Some(version))
}
// `nixos/nixpkgs`
[org, project] => {
(org, project, None)
}
[org, project] => (org, project, None),
_ => Err(color_eyre::eyre::eyre!(
"flakehub input did not match the expected format of `org/project` or `org/project/version`"
"flakehub input did not match the expected format of \
`org/project` or `org/project/version`"
))?,
};

Expand Down Expand Up @@ -225,11 +230,12 @@ pub(crate) async fn get_flakehub_project_and_url(

let res = client.get(&flakehub_json_url.to_string()).send().await?;

if res.status().is_success() {
let res = res.json::<ProjectCanonicalNames>().await?;
if let Err(e) = res.error_for_status_ref() {
let err_text = res.text().await?;
return Err(e).wrap_err(err_text)?;
};

let res = res.json::<ProjectCanonicalNames>().await?;

Ok((res.project, res.pretty_download_url))
} else {
Err(color_eyre::eyre::eyre!(res.text().await?))
}
Ok((res.project, res.pretty_download_url))
}