Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       1201use std::{2    error::Error,3    fs,4    path::{Path, PathBuf},5    process::Command,6    time::Duration,7};89use reqwest::header::CONTENT_TYPE;10use serde::Deserialize;1112use crate::{13    commands::progress::{self, Spinner},14    error::ContextError,15};1617/// Twig's binary registry accepts at most 512 MiB per asset.18const MAX_BINARY_BYTES: u64 = 512 * 1024 * 1024;1920/// Read from `[package.metadata.ino.binaries]` in `Cargo.toml`.21#[derive(Debug, Clone, Deserialize)]22pub struct BinaryConfig {23    /// Registry origin, for example `https://twig.example`.24    pub registry: String,25    pub namespace: String,26    pub repository: String,27    /// Rust target triples to build, for example `x86_64-unknown-linux-gnu`.28    pub targets: Vec<String>,29}3031#[derive(Debug, Deserialize)]32struct CargoMetadata {33    packages: Vec<CargoPackage>,34    target_directory: PathBuf,35}3637#[derive(Debug, Deserialize)]38struct CargoPackage {39    name: String,40    version: String,41    metadata: Option<PackageMetadata>,42}4344#[derive(Debug, Deserialize)]45struct PackageMetadata {46    ino: Option<InoMetadata>,47}4849#[derive(Debug, Deserialize)]50struct InoMetadata {51    binaries: Option<BinaryConfig>,52}5354/// The binary registry settings read from `Cargo.toml` metadata.55#[derive(Debug)]56struct Settings {57    config: BinaryConfig,58    package: String,59    version: String,60    target_directory: PathBuf,61}6263/// A configured upload, ready to build and send to the registry.64#[derive(Debug)]65pub struct Upload {66    settings: Settings,67    token: String,68}6970/// Returns the upload for the crate in the current directory, or `None` when71/// binaries are not configured or no Twig token is set. Without a token the72/// build and upload are skipped with a notice rather than failing the release.73pub fn load() -> Result<Option<Upload>, Box<dyn Error>> {74    let Some(settings) = read_settings()? else {75        return Ok(None);76    };77    let Some(token) = twig_token() else {78        progress::notice("skipping binary build and upload: TWIG_TOKEN is not set");79        return Ok(None);80    };81    Ok(Some(Upload { settings, token }))82}8384/// `ino binaries`: build and upload the configured binaries for the current85/// version in `Cargo.toml`. Skips with a notice when no Twig token is set.86pub fn run() -> Result<(), Box<dyn Error>> {87    let settings = read_settings()?.ok_or(88        "no [package.metadata.ino.binaries] section in Cargo.toml; add a registry, namespace, repository, and targets",89    )?;90    let Some(token) = twig_token() else {91        progress::notice("skipping binary build and upload: TWIG_TOKEN is not set");92        return Ok(());93    };94    let version = settings.version.clone();95    Upload { settings, token }.publish(&version)?;96    Ok(())97}9899fn twig_token() -> Option<String> {100    std::env::var("TWIG_TOKEN")101        .or_else(|_| std::env::var("INO_TWIG_TOKEN"))102        .ok()103        .filter(|token| !token.is_empty())104}105106fn read_settings() -> Result<Option<Settings>, Box<dyn Error>> {107    if !Path::new("Cargo.toml").is_file() {108        return Ok(None);109    }110    let metadata = cargo_metadata()?;111    let package = metadata112        .packages113        .into_iter()114        .next()115        .ok_or("cargo metadata returned no package")?;116    let Some(config) = package117        .metadata118        .and_then(|metadata| metadata.ino)119        .and_then(|ino| ino.binaries)120    else {121        return Ok(None);122    };123    validate_config(&config)?;124    Ok(Some(Settings {125        config,126        package: package.name,127        version: package.version,128        target_directory: metadata.target_directory,129    }))130}131132impl Upload {133    /// Builds every configured target and uploads it as `version`. Returns the134    /// number of assets uploaded.135    pub fn publish(&self, version: &str) -> Result<usize, Box<dyn Error>> {136        let client = reqwest::Client::builder()137            .timeout(Duration::from_mins(10))138            .build()139            .map_err(|error| ContextError::new("configuring binary upload client", error))?;140        let runtime = tokio::runtime::Builder::new_current_thread()141            .enable_all()142            .build()143            .map_err(|error| ContextError::new("starting binary upload runtime", error))?;144145        let mut published = 0;146        for target in &self.settings.config.targets {147            let artifact = self.build(target)?;148            let bytes = read_artifact(&artifact)?;149            let filename =150                asset_filename(&self.settings.package, target, target.contains("windows"));151            runtime.block_on(self.upload(&client, version, &filename, bytes))?;152            published += 1;153        }154        Ok(published)155    }156157    fn build(&self, target: &str) -> Result<PathBuf, Box<dyn Error>> {158        progress::notice(&format!("building {} for {target}", self.settings.package));159        let status = progress::run_command(Command::new("cargo").args([160            "build",161            "--release",162            "--locked",163            "--target",164            target,165        ]))166        .map_err(|error| {167            ContextError::new(format!("starting `cargo build` for {target}"), error)168        })?;169        if !status.success() {170            return Err(format!("`cargo build` for {target} failed ({status})").into());171        }172        let executable = if target.contains("windows") {173            ".exe"174        } else {175            ""176        };177        let artifact = self178            .settings179            .target_directory180            .join(target)181            .join("release")182            .join(format!("{}{executable}", self.settings.package));183        if !artifact.is_file() {184            return Err(format!("cargo build did not produce {}", artifact.display()).into());185        }186        Ok(artifact)187    }188189    async fn upload(190        &self,191        client: &reqwest::Client,192        version: &str,193        filename: &str,194        bytes: Vec<u8>,195    ) -> Result<(), Box<dyn Error>> {196        let size = bytes.len();197        let url = binary_url(&self.settings.config, version, filename)?;198        let spinner = Spinner::start(format!("Uploading {filename} ({size} bytes) to Twig"));199        let response = client200            .put(url)201            .bearer_auth(&self.token)202            .header(CONTENT_TYPE, "application/octet-stream")203            .body(bytes)204            .send()205            .await206            .map_err(|error| ContextError::new(format!("uploading `{filename}` to Twig"), error));207        drop(spinner);208        let response = response?;209        let status = response.status();210        if status.is_success() {211            progress::notice(&format!(212                "uploaded {filename} ({size} bytes) to {}/{} v{version}",213                self.settings.config.namespace, self.settings.config.repository214            ));215            return Ok(());216        }217        let detail = response.text().await.unwrap_or_default();218        let detail = detail.trim();219        let message = match status.as_u16() {220            400 => format!("Twig rejected `{filename}` as an invalid binary (HTTP 400): {detail}"),221            401 | 403 => format!(222                "Twig rejected the binary upload for `{filename}` (HTTP {}); TWIG_TOKEN needs push permission for {}/{}",223                status.as_u16(),224                self.settings.config.namespace,225                self.settings.config.repository226            ),227            409 => format!(228                "Twig refused `{filename}` because version {version} is older than its three retained releases"229            ),230            413 => format!("`{filename}` exceeds Twig's binary upload limit"),231            code => {232                format!("Twig rejected the binary upload for `{filename}` (HTTP {code}): {detail}")233            }234        };235        Err(message.into())236    }237}238239fn cargo_metadata() -> Result<CargoMetadata, Box<dyn Error>> {240    let output = Command::new("cargo")241        .args(["metadata", "--no-deps", "--format-version", "1"])242        .output()243        .map_err(|error| ContextError::new("starting `cargo metadata`", error))?;244    if !output.status.success() {245        return Err(format!(246            "`cargo metadata` failed: {}",247            String::from_utf8_lossy(&output.stderr).trim()248        )249        .into());250    }251    Ok(serde_json::from_slice(&output.stdout)252        .map_err(|error| ContextError::new("parsing `cargo metadata` output", error))?)253}254255fn validate_config(config: &BinaryConfig) -> Result<(), Box<dyn Error>> {256    let url = reqwest::Url::parse(&config.registry).map_err(|error| {257        format!(258            "binary registry `{}` is not a URL: {error}",259            config.registry260        )261    })?;262    if !matches!(url.scheme(), "https" | "http") {263        return Err(format!("binary registry `{}` must use https", config.registry).into());264    }265    for (field, value) in [266        ("namespace", &config.namespace),267        ("repository", &config.repository),268    ] {269        if value.is_empty()270            || !value.chars().all(|character| {271                character.is_ascii_alphanumeric() || matches!(character, '-' | '_' | '.')272            })273        {274            return Err(format!(275                "binaries {field} `{value}` must contain only letters, digits, '-', '_' or '.'"276            )277            .into());278        }279    }280    if config.targets.is_empty() {281        return Err("[package.metadata.ino.binaries] needs at least one target".into());282    }283    Ok(())284}285286fn read_artifact(path: &Path) -> Result<Vec<u8>, Box<dyn Error>> {287    let size = fs::metadata(path)288        .map_err(|error| ContextError::new(format!("reading `{}`", path.display()), error))?289        .len();290    if size > MAX_BINARY_BYTES {291        return Err(format!(292            "`{}` is {size} bytes, over Twig's 512 MiB binary limit",293            path.display()294        )295        .into());296    }297    fs::read(path)298        .map_err(|error| ContextError::new(format!("reading `{}`", path.display()), error).into())299}300301fn binary_url(302    config: &BinaryConfig,303    version: &str,304    filename: &str,305) -> Result<reqwest::Url, Box<dyn Error>> {306    let base = config.registry.trim_end_matches('/');307    let path = format!(308        "{base}/api/v1/namespaces/{}/repositories/{}/binaries/{version}/{filename}",309        config.namespace, config.repository310    );311    Ok(reqwest::Url::parse(&path)?)312}313314fn asset_filename(package: &str, target: &str, windows: bool) -> String {315    format!("{package}-{target}{}", if windows { ".exe" } else { "" })316}317318#[cfg(test)]319mod tests {320    use super::*;321322    fn config(registry: &str) -> BinaryConfig {323        BinaryConfig {324            registry: registry.to_owned(),325            namespace: "twig".to_owned(),326            repository: "ino".to_owned(),327            targets: vec!["x86_64-unknown-linux-gnu".to_owned()],328        }329    }330331    #[test]332    fn derives_binary_asset_filenames() {333        assert_eq!(334            asset_filename("ino", "x86_64-unknown-linux-gnu", false),335            "ino-x86_64-unknown-linux-gnu"336        );337        assert_eq!(338            asset_filename("ino", "x86_64-pc-windows-gnu", true),339            "ino-x86_64-pc-windows-gnu.exe"340        );341    }342343    #[test]344    fn builds_the_registry_upload_url() {345        let url = binary_url(&config("https://twig.example/"), "0.5.22", "ino-x86_64").unwrap();346        assert_eq!(347            url.as_str(),348            "https://twig.example/api/v1/namespaces/twig/repositories/ino/binaries/0.5.22/ino-x86_64"349        );350    }351352    #[test]353    fn rejects_unusable_registry_settings() {354        assert!(validate_config(&config("ftp://twig.example")).is_err());355        assert!(validate_config(&config("not a url")).is_err());356        let mut bad_namespace = config("https://twig.example");357        bad_namespace.namespace = "twig/../x".to_owned();358        assert!(validate_config(&bad_namespace).is_err());359        let mut no_targets = config("https://twig.example");360        no_targets.targets.clear();361        assert!(validate_config(&no_targets).is_err());362        assert!(validate_config(&config("https://twig.example")).is_ok());363    }364}