Skip to content

Commit

Permalink
feat(cli): Add --print-rclone-config to "app volumes s3-credentials"
Browse files Browse the repository at this point in the history
  • Loading branch information
theduke committed Aug 19, 2024
1 parent c67bdfb commit 3bf7165
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub async fn get_app_volumes(
/// Retrieved with [`get_app_s3_credentials`].
#[derive(Clone)]
pub struct AppS3Credentials {
pub domain: String,
pub url: String,
pub access_key: String,
pub secret_key: String,
}
Expand Down Expand Up @@ -262,7 +262,7 @@ pub async fn get_app_s3_credentials(
.with_context(|| format!("No value found for secret with name '{}'", SECRET_KEY_NAME))?;

Ok(AppS3Credentials {
domain: url.0,
url: url.0,
access_key,
secret_key,
})
Expand Down
47 changes: 41 additions & 6 deletions lib/cli/src/commands/app/volumes/s3_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use super::super::util::AppIdentOpts;
use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts};

/// Retrieve S3 access credentials for the volumes of an app.
///
/// The S3 credentials can be used to access volumes with any S3 client,
/// for example rclone.
///
/// Note that --rclone-print will output an rclone configuration snippet.
#[derive(clap::Parser, Debug)]
pub struct CmdAppS3Credentials {
#[clap(flatten)]
Expand All @@ -17,6 +22,13 @@ pub struct CmdAppS3Credentials {

#[clap(flatten)]
pub ident: AppIdentOpts,

/// Print rclone configuration.
///
/// The output can be added the rclone configuration file, which is
/// usually located at `~/.config/rclone/rclone.conf`.
#[clap(long)]
pub rclone_print: bool,
}

#[async_trait::async_trait]
Expand All @@ -30,12 +42,35 @@ impl AsyncCliCommand for CmdAppS3Credentials {
let creds =
wasmer_api::query::get_app_s3_credentials(&client, app.id.clone().into_inner()).await?;

println!("S3 credentials for app {}:\n", app.name);
println!(" S3 URL: https://{}", creds.domain);
println!(" Access key: {}", creds.access_key);
println!(" Secret key: {}", creds.secret_key);
println!();
println!("Consult the app volumes documentation for more information.");
if self.print_rclone_config {
let rclone_config = format!(
r#"
[edge-{app_name}]
type = s3
provider = Other
acl = private
access_key_id = {access_key}
secret_access_key = {secret_key}
endpoint = {endpoint}
"#,
app_name = app.name,
access_key = creds.access_key,
secret_key = creds.secret_key,
endpoint = creds.url,
);

println!("{}", rclone_config);
} else {
println!("S3 credentials for app {}:\n", app.name);
println!(" S3 URL: {}", creds.url);
println!(" Access key: {}", creds.access_key);
println!(" Secret key: {}", creds.secret_key);
println!();
println!("Hint: use --rclone-print to generate configuration for the rclone S3 client");
println!("Consult the app volumes documentation for more information.");
println!();
}

Ok(app)
}
Expand Down

0 comments on commit 3bf7165

Please sign in to comment.