Skip to content

Commit

Permalink
feat(cli): Add "app volumes" commands
Browse files Browse the repository at this point in the history
  • Loading branch information
theduke committed Aug 13, 2024
1 parent 875cfcc commit 5d775cb
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/cli/src/commands/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod purge_cache;
pub mod regions;
pub mod secrets;
pub mod version;
pub mod volumes;

mod util;

Expand All @@ -33,6 +34,8 @@ pub enum CmdApp {
Secret(secrets::CmdAppSecrets),
#[clap(subcommand, alias = "regions")]
Region(regions::CmdAppRegions),
#[clap(subcommand, alias = "volumes")]
Volume(volumes::CmdAppVolumes),
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -61,6 +64,7 @@ impl AsyncCliCommand for CmdApp {
Self::PurgeCache(cmd) => cmd.run_async().await,
Self::Secret(cmd) => cmd.run_async().await,
Self::Region(cmd) => cmd.run_async().await,
Self::Volume(cmd) => cmd.run_async().await,
}
}
}
38 changes: 38 additions & 0 deletions lib/cli/src/commands/app/volumes/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! List Edge apps.
use super::super::util::AppIdentOpts;
use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ListFormatOpts};

/// List the volumes of an app.
#[derive(clap::Parser, Debug)]
pub struct CmdAppVolumesList {
#[clap(flatten)]
fmt: ListFormatOpts,

#[clap(flatten)]
env: WasmerEnv,

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

#[async_trait::async_trait]
impl AsyncCliCommand for CmdAppVolumesList {
type Output = ();

async fn run_async(self) -> Result<(), anyhow::Error> {
let client = self.env.client()?;

let (_ident, app) = self.ident.load_app(&client).await?;
let volumes =
wasmer_api::query::get_app_volumes(&client, &app.owner.global_name, &app.name).await?;

if volumes.is_empty() {
eprintln!("App {} has no volumes!", app.name);
} else {
println!("{}", self.fmt.format.render(volumes.as_slice()));
}

Ok(())
}
}
29 changes: 29 additions & 0 deletions lib/cli/src/commands/app/volumes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::commands::AsyncCliCommand;

pub mod list;
pub mod s3_credentials;

/// App volume management.
#[derive(Debug, clap::Parser)]
pub enum CmdAppVolumes {
S3Credentials(s3_credentials::CmdAppS3Credentials),
List(list::CmdAppVolumesList),
}

#[async_trait::async_trait]
impl AsyncCliCommand for CmdAppVolumes {
type Output = ();

async fn run_async(self) -> Result<Self::Output, anyhow::Error> {
match self {
Self::S3Credentials(c) => {
c.run_async().await?;
Ok(())
}
Self::List(c) => {
c.run_async().await?;
Ok(())
}
}
}
}
42 changes: 42 additions & 0 deletions lib/cli/src/commands/app/volumes/s3_credentials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Get information about an edge app.
use wasmer_api::types::DeployApp;

use super::super::util::AppIdentOpts;

use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts};

/// Retrieve S3 access credentials for the volumes of an app.
#[derive(clap::Parser, Debug)]
pub struct CmdAppS3Credentials {
#[clap(flatten)]
pub env: WasmerEnv,

#[clap(flatten)]
pub fmt: ItemFormatOpts,

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

#[async_trait::async_trait]
impl AsyncCliCommand for CmdAppS3Credentials {
type Output = DeployApp;

async fn run_async(self) -> Result<DeployApp, anyhow::Error> {
let client = self.env.client()?;
let (_ident, app) = self.ident.load_app(&client).await?;

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.");

Ok(app)
}
}
33 changes: 33 additions & 0 deletions lib/cli/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,36 @@ impl CliRender for DeployAppVersion {
table.to_string()
}
}

impl CliRender for wasmer_api::types::AppVersionVolume {
fn render_item_table(&self) -> String {
let mut table = Table::new();
table.add_rows([
vec!["Name".to_string(), self.name.clone()],
vec![
"Used size".to_string(),
format_disk_size_opt(self.used_size),
],
]);
table.to_string()
}

fn render_list_table(items: &[Self]) -> String {
let mut table = Table::new();
table.set_header(vec!["Name".to_string(), "Used size".to_string()]);
table.add_rows(
items
.iter()
.map(|vol| vec![vol.name.clone(), format_disk_size_opt(vol.used_size)]),
);
table.to_string()
}
}

fn format_disk_size_opt(value: Option<i32>) -> String {
if let Some(v) = value {
format!("{}Mb", v)
} else {
"n/a".to_string()
}
}

0 comments on commit 5d775cb

Please sign in to comment.