Skip to content

Commit

Permalink
fix: implement profile export toml and make it default option (#4172)
Browse files Browse the repository at this point in the history
* fix: implement toml profile export and make it default option

* fix: enable display feature for toml package
  • Loading branch information
PanGan21 authored Sep 9, 2024
1 parent 32e4068 commit babdebf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 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.

6 changes: 3 additions & 3 deletions crates/fluvio-cli/src/profile/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::error::CliError;
pub struct ExportOpt {
profile_name: Option<String>,
#[arg(
default_value_t = OutputType::json,
default_value_t = OutputType::toml,
short = 'O',
long = "output",
value_name = "type",
Expand All @@ -27,8 +27,8 @@ impl ExportOpt {
pub fn process<O: Terminal>(self, out: Arc<O>) -> Result<()> {
let output_format = match self.output_format {
OutputType::table => {
eprintln!("Table format is not supported, using JSON instead");
OutputType::json
eprintln!("Table format is not supported, using TOML instead");
OutputType::toml
}
_ => self.output_format,
};
Expand Down
1 change: 1 addition & 0 deletions crates/fluvio-extension-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_yaml = { workspace = true }
semver = { workspace = true, features = ["serde"] }
thiserror = { workspace = true }
tracing = { workspace = true }
toml = { workspace = true, features = ["display"] }

fluvio = { workspace = true, optional = true }
fluvio-package-index = { workspace = true }
7 changes: 7 additions & 0 deletions crates/fluvio-extension-common/src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod error {

use serde_json::Error as SerdeJsonError;
use serde_yaml::Error as SerdeYamlError;
use toml::ser::Error as SerdeTomlError;

#[derive(thiserror::Error, Debug)]
pub enum OutputError {
Expand All @@ -53,6 +54,11 @@ mod error {
#[from]
source: SerdeYamlError,
},
#[error("Fluvio client error")]
SerdeTomlError {
#[from]
source: SerdeTomlError,
},
}
}

Expand Down Expand Up @@ -94,6 +100,7 @@ mod output {
table,
yaml,
json,
toml,
}

/// OutputType defaults to table formatting
Expand Down
15 changes: 15 additions & 0 deletions crates/fluvio-extension-common/src/output/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use super::OutputError;
pub enum SerializeType {
yaml,
json,
toml,
}

impl From<OutputType> for SerializeType {
fn from(output: OutputType) -> Self {
match output {
OutputType::yaml => SerializeType::yaml,
OutputType::json => SerializeType::json,
OutputType::toml => SerializeType::toml,
_ => panic!("should never happen"),
}
}
Expand All @@ -42,6 +44,7 @@ where
match output_type {
SerializeType::yaml => self.to_yaml(value),
SerializeType::json => self.to_json(value),
SerializeType::toml => self.to_toml(value),
}
}

Expand All @@ -68,4 +71,16 @@ where

Ok(())
}

/// convert to toml format and print to terminal
fn to_toml<S>(&self, value: &S) -> Result<(), OutputError>
where
S: Serialize,
{
let serialized = toml::to_string(value)?;

self.0.println(&serialized);

Ok(())
}
}

0 comments on commit babdebf

Please sign in to comment.