Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rpassword = "7.4.0"
reqwest = { version = "0.12.20", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
semver = "1.0.26"
tokio = { version = "1.45.1", features = ["macros", "rt-multi-thread"] }
toml = "0.8.23"
url = "2.5.4"
Expand Down
19 changes: 19 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ pub struct UserWithToken {
pub struct SystemInfo {
pub version: VersionInfo,
pub min_app_version: VersionInfo,
#[serde(default)]
pub min_cli_version: Option<VersionInfo>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -302,6 +304,11 @@ impl VersionInfo {
_ => format!("{}.{}.{}", self.major, self.minor, self.patch),
}
}

pub fn semver(&self) -> Result<semver::Version> {
semver::Version::parse(&self.display())
.with_context(|| format!("invalid version returned by server: {}", self.display()))
}
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -435,4 +442,16 @@ mod tests {
"http://localhost:3000/"
);
}

#[test]
fn parses_prerelease_versions() {
let version = VersionInfo {
major: 1,
minor: 2,
patch: 3,
pre: Some("rc.1".to_owned()),
};

assert_eq!(version.semver().unwrap().to_string(), "1.2.3-rc.1");
}
}
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod player;

use anyhow::{Context, Result, bail};
use clap::{Args, Parser, Subcommand};
use semver::Version;
use std::io::{self, Write};

use crate::{
Expand Down Expand Up @@ -215,6 +216,7 @@ async fn login(args: LoginArgs) -> Result<()> {
};

let client = ApiClient::new(&server, None)?;
ensure_supported_cli_version(&client).await?;
let response = client.login(&email, &password).await?;

let config = Config {
Expand All @@ -237,6 +239,25 @@ async fn login(args: LoginArgs) -> Result<()> {
Ok(())
}

async fn ensure_supported_cli_version(client: &ApiClient) -> Result<()> {
let Some(minimum) = client.system().await?.min_cli_version else {
return Ok(());
};
let current = Version::parse(env!("CARGO_PKG_VERSION"))
.expect("CARGO_PKG_VERSION must be a valid semantic version");
let minimum_version = minimum.semver()?;

if current < minimum_version {
bail!(
"this server requires Black Candy CLI {} or later; you are running {}. Please upgrade and try again.",
minimum.display(),
current
);
}

Ok(())
}

fn prompt_line(prompt: &str) -> Result<String> {
print!("{prompt}");
io::stdout().flush()?;
Expand Down Expand Up @@ -274,6 +295,9 @@ async fn run_authenticated(command: Command, client: ApiClient) -> Result<()> {
} else {
println!("Server: {}", system.version.display());
println!("Minimum app version: {}", system.min_app_version.display());
if let Some(minimum) = system.min_cli_version {
println!("Minimum CLI version: {}", minimum.display());
}
}
}
Command::Search { query, json } => {
Expand Down