Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dmtrctl"
version = "2.0.2"
version = "2.1.0"
edition = "2021"
repository = "https://github.com/demeter-run/cli"
license = "Apache-2.0"
Expand Down
138 changes: 110 additions & 28 deletions src/ports/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ use comfy_table::presets::UTF8_FULL;
use comfy_table::{ContentArrangement, Table};
use dmtri::demeter::ops::v1alpha::Resource;
use miette::IntoDiagnostic;
use serde::Serialize;

#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum OutputFormat {
#[default]
Table,
Json,
}

impl OutputFormat {
pub fn pretty_print(&self, resources: Vec<Resource>) {
match self {
OutputFormat::Table => pretty_print_resource_table(resources),
OutputFormat::Json => pretty_print_resource_json(resources),
}
}

pub fn pretty_print_single(&self, resource: &Resource) {
match self {
OutputFormat::Table => pretty_print_resource_detail_table(resource),
OutputFormat::Json => pretty_print_resource_detail_json(resource),
}
}
}

pub fn pretty_print_resource_table(resources: Vec<Resource>) {
let mut table = Table::new();
Expand All @@ -26,17 +51,78 @@ pub fn pretty_print_resource_table(resources: Vec<Resource>) {
println!("{table}");
}

pub fn pretty_print_resouce_detail_table(resources: Vec<Resource>) -> miette::Result<()> {
let mut table = Table::new();
pub fn pretty_print_resource_json(resources: Vec<Resource>) {
let mut collector = vec![];
for resource in resources {
collector.push(serde_json::Map::from_iter(vec![
("id".to_string(), serde_json::Value::from(resource.id)),
("name".to_string(), serde_json::Value::from(resource.name)),
("kind".to_string(), serde_json::Value::from(resource.kind)),
(
"created_at".to_string(),
serde_json::Value::from(resource.created_at),
),
(
"spec".to_string(),
serde_json::from_str::<serde_json::Value>(&resource.spec).unwrap(),
),
(
"annotations".to_string(),
serde_json::from_str::<serde_json::Value>(
&resource.annotations.unwrap_or_default(),
)
.unwrap(),
),
]))
}
println!("{}", serde_json::to_string_pretty(&collector).unwrap());
}

pub fn pretty_print_resource_detail_json(resource: &Resource) {
println!(
"{}",
serde_json::to_string_pretty(
&(serde_json::Map::from_iter(vec![
(
"id".to_string(),
serde_json::Value::from(resource.id.clone())
),
(
"name".to_string(),
serde_json::Value::from(resource.name.clone())
),
(
"kind".to_string(),
serde_json::Value::from(resource.kind.clone())
),
(
"created_at".to_string(),
serde_json::Value::from(resource.created_at.clone()),
),
(
"spec".to_string(),
serde_json::from_str::<serde_json::Value>(&resource.spec).unwrap(),
),
(
"annotations".to_string(),
serde_json::from_str::<serde_json::Value>(
&resource.annotations.clone().unwrap_or_default(),
)
.unwrap(),
),
]))
)
.unwrap()
);
}

let Some(first_resource) = resources.first() else {
return Ok(());
};
pub fn pretty_print_resource_detail_table(resource: &Resource) {
let mut table = Table::new();

let annotations = serde_json::from_str::<serde_json::Value>(
&first_resource.annotations.clone().unwrap_or_default(),
&resource.annotations.clone().unwrap_or_default(),
)
.into_diagnostic()?;
.unwrap();
let mut annotations_headers: Vec<String> = annotations
.as_array()
.unwrap()
Expand All @@ -53,29 +139,25 @@ pub fn pretty_print_resouce_detail_table(resources: Vec<Resource>) -> miette::Re
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(headers);

for resource in resources {
let mut values: Vec<String> = vec![resource.name];

if let Some(annotations) = resource.annotations {
let annotations: serde_json::Value =
serde_json::from_str(&annotations).into_diagnostic()?;

for value in annotations.as_array().unwrap().iter() {
values.push(
value
.get("value")
.unwrap()
.as_str()
.unwrap_or_default()
.into(),
);
}
}
let mut values: Vec<String> = vec![resource.name.clone()];

if let Some(annotations) = &resource.annotations {
let annotations: serde_json::Value =
serde_json::from_str(annotations).into_diagnostic().unwrap();

table.add_row(values);
for value in annotations.as_array().unwrap().iter() {
values.push(
value
.get("value")
.unwrap()
.as_str()
.unwrap_or_default()
.into(),
);
}
}

println!("{table}");
table.add_row(values);

Ok(())
println!("{table}");
}
14 changes: 8 additions & 6 deletions src/ports/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ use clap::Parser;

use crate::{
context::extract_context_data,
rpc::{self},
rpc,
};

use super::format::pretty_print_resource_table;
use super::format::OutputFormat;

#[derive(Parser)]
pub struct Args {}
pub struct Args {
#[clap(short, long, default_value_t, value_enum)]
pub output: OutputFormat,
}

pub async fn run(cli: &crate::Cli) -> miette::Result<()> {
pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> {
let _ctx = cli
.context
.as_ref()
Expand All @@ -24,7 +27,6 @@ pub async fn run(cli: &crate::Cli) -> miette::Result<()> {
return Ok(());
}

pretty_print_resource_table(response);

args.output.pretty_print(response);
Ok(())
}
2 changes: 1 addition & 1 deletion src/ports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum Commands {

pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> {
match args.command {
Commands::List(_) => list::run(cli).await,
Commands::List(x) => list::run(x, cli).await,
Commands::Show(x) => show::run(x, cli).await,
Commands::Create(x) => create::run(x, cli).await,
Commands::Delete(x) => delete::run(x, cli).await,
Expand Down
8 changes: 6 additions & 2 deletions src/ports/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use clap::Parser;

use crate::{context::extract_context_data, rpc};

use super::format::pretty_print_resouce_detail_table;
use super::format::OutputFormat;

#[derive(Parser)]
pub struct Args {
/// the resource uuid
id: String,

#[clap(short, long, default_value_t, value_enum)]
pub output: OutputFormat,
}

pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> {
Expand All @@ -24,6 +27,7 @@ pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> {
return Ok(());
}

pretty_print_resouce_detail_table(resouces)?;
let response = resouces.first().unwrap();
args.output.pretty_print_single(response);
Ok(())
}