Skip to content

Commit

Permalink
feat(kudoctl): implement "get resource" command
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Ponsard <[email protected]>
  • Loading branch information
nponsard authored and sameo committed Aug 25, 2022
1 parent 1392c57 commit ce1e1cb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
7 changes: 3 additions & 4 deletions kudoctl/src/subcommands/get/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod instance;
mod instances;
mod output;
mod resource;
mod resources;
use self::output::OutputFormat;
use crate::config;
use anyhow::{bail, Result};
use anyhow::Result;
use clap::{Args, ValueEnum};

#[derive(Debug, Args)]
Expand Down Expand Up @@ -48,10 +49,8 @@ pub async fn execute(args: GetSubcommand, conf: &config::Config) -> Result<Strin

match args.subject {
GetSubjects::Resources => resources::execute(conf, format, show_header).await,
GetSubjects::Resource => resource::execute(conf, format, args.id).await,
GetSubjects::Instances => instances::execute(conf, format, show_header).await,
GetSubjects::Instance => instance::execute(conf, format, args.id).await,
GetSubjects::Resource => {
bail!(format!("{:?} not implemented yet", args.subject))
}
}
}
63 changes: 63 additions & 0 deletions kudoctl/src/subcommands/get/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::output::{self, OutputFormat};
use crate::{
client::{self, request::Client},
config,
resource::workload::Workload,
};
use anyhow::{bail, Context, Result};
use std::fmt::Display;

/// get workload <id> subcommand execution
/// Does the request, then formats the output.
pub async fn execute(
conf: &config::Config,
format: OutputFormat,
search: Option<String>,
) -> Result<String> {
if search.is_none() {
bail!("You must provide an instance id");
}
let search = search.unwrap();

let client = Client::new(conf).context("Error creating client")?;
let result = client::workload::get(&client, search.as_str()).await?;

output::format_output(result, format)
}

impl Display for Workload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "name : {}\n", self.name)?;
writeln!(f, "uri : {}\n", self.uri)?;

if let Some(ports) = &self.ports {
// display ports
let ports_str = ports
.iter()
.fold(String::new(), |acc, port| acc + &format!("{} ", port))
.trim()
.replace(' ', ",")
.replace(':', "->");
writeln!(f, "ports : {} ", ports_str)?;
}

if let Some(env) = &self.env {
// display environment variables
let env_vars_str = env
.iter()
.fold(String::new(), |acc, env_var| acc + &format!("{} ", env_var))
.trim()
.replace(' ', ",");
writeln!(f, "env variables : {} ", env_vars_str)?;
}

// display resources

writeln!(
f,
"resources : {}milliCPU, {}mB memory, {}GB disk ",
self.resources.cpu, self.resources.memory, self.resources.disk
)?;
Ok(())
}
}

0 comments on commit ce1e1cb

Please sign in to comment.