Skip to content

Commit

Permalink
feat: add pixi upload command to upload packages to prefix.dev (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Jul 14, 2023
1 parent a524fdb commit dd8870e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ minijinja = { version = "0.34.0", features = ["builtins"] }
once_cell = "1.17.1"
rattler = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main" }
rattler_conda_types = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main" }
rattler_digest = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main" }
rattler_networking = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main" }
rattler_repodata_gateway = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main", features = ["sparse"] }
rattler_shell = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main", features = ["sysinfo"] }
Expand All @@ -55,6 +56,7 @@ serde_with = { version = "3.0.0", features = ["indexmap"] }
shlex = "1.1.0"
tempfile = "3.5.0"
tokio = { version = "1.27.0", features = ["macros", "rt-multi-thread", "signal"] }
tokio-util = "0.7.8"
toml_edit = { version = "0.19.10", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
Expand Down
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod install;
pub mod run;
pub mod shell;
pub mod task;
pub mod upload;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -55,6 +56,7 @@ pub enum Command {
Install(install::Args),
Task(task::Args),
Info(info::Args),
Upload(upload::Args),
}

fn completion(args: CompletionCommand) -> miette::Result<()> {
Expand Down Expand Up @@ -115,5 +117,6 @@ pub async fn execute_command(command: Command) -> miette::Result<()> {
Command::Shell(cmd) => shell::execute(cmd).await,
Command::Task(cmd) => task::execute(cmd),
Command::Info(cmd) => info::execute(cmd).await,
Command::Upload(cmd) => upload::execute(cmd).await,
}
}
93 changes: 93 additions & 0 deletions src/cli/upload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::path::PathBuf;

use clap::Parser;
use futures::TryStreamExt;
use indicatif::HumanBytes;
use miette::IntoDiagnostic;
use rattler_networking::AuthenticatedClient;

use rattler_digest::{compute_file_digest, Sha256};
use tokio::fs::File;
use tokio_util::io::ReaderStream;

use crate::progress;

/// Upload a package to a prefix.dev channel
#[derive(Parser, Debug)]
pub struct Args {
/// The host + channel to upload to
host: String,

/// The file to upload
package_file: PathBuf,
}

/// Upload a package to a prefix.dev channel
pub async fn execute(args: Args) -> miette::Result<()> {
let filename = args
.package_file
.file_name()
.unwrap()
.to_string_lossy()
.to_string();

let filesize = args.package_file.metadata().into_diagnostic()?.len();

println!("Uploading package to: {}", args.host);
println!(
"Package file: {} ({})\n",
args.package_file.display(),
HumanBytes(filesize)
);

let client = AuthenticatedClient::default();

let sha256sum = format!(
"{:x}",
compute_file_digest::<Sha256>(&args.package_file).into_diagnostic()?
);

let file = File::open(&args.package_file).await.into_diagnostic()?;

let progress_bar = indicatif::ProgressBar::new(filesize)
.with_prefix("Uploading")
.with_style(progress::default_bytes_style());

let reader_stream = ReaderStream::new(file)
.inspect_ok(move |bytes| {
progress_bar.inc(bytes.len() as u64);
})
.inspect_err(|e| {
println!("Error while uploading: {}", e);
});

let body = reqwest::Body::wrap_stream(reader_stream);

let response = client
.post(args.host.clone())
.header("X-File-Sha256", sha256sum)
.header("X-File-Name", filename)
.header("Content-Length", filesize)
.header("Content-Type", "application/octet-stream")
.body(body)
.send()
.await
.into_diagnostic()?;

if response.status().is_success() {
println!("Upload successful!");
} else {
println!("Upload failed!");

if response.status() == 401 {
println!(
"Authentication failed! Did you run `pixi auth login {}`?",
args.host
);
}

std::process::exit(1);
}

Ok(())
}

0 comments on commit dd8870e

Please sign in to comment.