difftreelog
fix(cli) yaml format should be used by default, when -y is passed
in: master
1 file changed
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5 JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{TomlFormat, YamlFormat};89#[derive(Clone, ValueEnum)]10pub enum ManifestFormatName {11 /// Expect string as output, and write them directly12 String,13 Json,14 Yaml,15 Toml,16}1718#[derive(Parser)]19#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]20pub struct ManifestOpts {21 /// Output format, wraps resulting value to corresponding std.manifest call.22 #[clap(long, short = 'f', default_value = "json")]23 format: ManifestFormatName,24 /// Expect plain string as output.25 /// Mutually exclusive with `--format`26 #[clap(long, short = 'S', conflicts_with = "format")]27 string: bool,28 /// Write output as YAML stream, can be used with --format json/yaml29 #[clap(long, short = 'y', conflicts_with = "string")]30 yaml_stream: bool,31 /// Number of spaces to pad output manifest with.32 /// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml/toml]33 #[clap(long)]34 line_padding: Option<usize>,35 /// Preserve order in object manifestification36 #[cfg(feature = "exp-preserve-order")]37 #[clap(long)]38 pub preserve_order: bool,39}40impl ManifestOpts {41 pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {42 let format: Box<dyn ManifestFormat> = if self.string {43 Box::new(StringFormat)44 } else {45 #[cfg(feature = "exp-preserve-order")]46 let preserve_order = self.preserve_order;47 match self.format {48 ManifestFormatName::String => Box::new(ToStringFormat),49 ManifestFormatName::Json => Box::new(JsonFormat::cli(50 self.line_padding.unwrap_or(3),51 #[cfg(feature = "exp-preserve-order")]52 preserve_order,53 )),54 ManifestFormatName::Yaml => Box::new(YamlFormat::cli(55 self.line_padding.unwrap_or(2),56 #[cfg(feature = "exp-preserve-order")]57 preserve_order,58 )),59 ManifestFormatName::Toml => Box::new(TomlFormat::cli(60 self.line_padding.unwrap_or(2),61 #[cfg(feature = "exp-preserve-order")]62 preserve_order,63 )),64 }65 };66 if self.yaml_stream {67 Box::new(YamlStreamFormat(format))68 } else {69 format70 }71 }72}7374#[derive(Parser)]75pub struct OutputOpts {76 /// Write to the output file rather than stdout77 #[clap(long, short = 'o')]78 pub output_file: Option<PathBuf>,79 /// Automatically creates all parent directories for files80 #[clap(long, short = 'c')]81 pub create_output_dirs: bool,82 /// Write multiple files to the directory, list files on stdout83 #[clap(long, short = 'm')]84 pub multi: Option<PathBuf>,85}1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5 JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{TomlFormat, YamlFormat};89#[derive(Clone, Copy, ValueEnum)]10pub enum ManifestFormatName {11 /// Expect string as output, and write them directly12 String,13 Json,14 Yaml,15 Toml,16}1718#[derive(Parser)]19#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]20pub struct ManifestOpts {21 /// Output format, wraps resulting value to corresponding std.manifest call22 ///23 /// [default: json, yaml when -y is used]24 #[clap(long, short = 'f')]25 format: Option<ManifestFormatName>,26 /// Expect plain string as output.27 /// Mutually exclusive with `--format`28 #[clap(long, short = 'S', conflicts_with = "format")]29 string: bool,30 /// Write output as YAML stream, can be used with --format json/yaml31 #[clap(long, short = 'y', conflicts_with = "string")]32 yaml_stream: bool,33 /// Number of spaces to pad output manifest with.34 /// `0` for hard tabs, `-1` for single line output35 ///36 /// [default: 3 for json, 2 for yaml/toml]37 #[clap(long)]38 line_padding: Option<usize>,39 /// Preserve order in object manifestification40 #[cfg(feature = "exp-preserve-order")]41 #[clap(long)]42 pub preserve_order: bool,43}44impl ManifestOpts {45 pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {46 let format: Box<dyn ManifestFormat> = if self.string {47 Box::new(StringFormat)48 } else {49 #[cfg(feature = "exp-preserve-order")]50 let preserve_order = self.preserve_order;51 let format = match self.format {52 Some(v) => v,53 None if self.yaml_stream => ManifestFormatName::Yaml,54 None => ManifestFormatName::Json,55 };56 match format {57 ManifestFormatName::String => Box::new(ToStringFormat),58 ManifestFormatName::Json => Box::new(JsonFormat::cli(59 self.line_padding.unwrap_or(3),60 #[cfg(feature = "exp-preserve-order")]61 preserve_order,62 )),63 ManifestFormatName::Yaml => Box::new(YamlFormat::cli(64 self.line_padding.unwrap_or(2),65 #[cfg(feature = "exp-preserve-order")]66 preserve_order,67 )),68 ManifestFormatName::Toml => Box::new(TomlFormat::cli(69 self.line_padding.unwrap_or(2),70 #[cfg(feature = "exp-preserve-order")]71 preserve_order,72 )),73 }74 };75 if self.yaml_stream {76 Box::new(YamlStreamFormat(format))77 } else {78 format79 }80 }81}8283#[derive(Parser)]84pub struct OutputOpts {85 /// Write to the output file rather than stdout86 #[clap(long, short = 'o')]87 pub output_file: Option<PathBuf>,88 /// Automatically creates all parent directories for files89 #[clap(long, short = 'c')]90 pub create_output_dirs: bool,91 /// Write multiple files to the directory, list files on stdout92 #[clap(long, short = 'm')]93 pub multi: Option<PathBuf>,94}