feat: add --output-format option for json/yaml support in info/status/list modes
Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
@@ -8,8 +8,11 @@ use crate::compression_engine::COMPRESSION_PROGRAMS;
|
||||
use crate::compression_engine::CompressionType;
|
||||
use crate::compression_engine::program::CompressionEngineProgram;
|
||||
|
||||
use crate::modes::common::get_format_box_chars_no_border_line_separator;
|
||||
use crate::modes::common::{get_format_box_chars_no_border_line_separator, get_output_format, OutputFormat};
|
||||
use prettytable::color;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use serde_yaml;
|
||||
use prettytable::row;
|
||||
use prettytable::{Attr, Cell, Row, Table};
|
||||
use prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR;
|
||||
@@ -178,6 +181,138 @@ fn build_meta_plugin_table(enabled_meta_plugins: &Vec<MetaPluginType>) -> Table
|
||||
meta_plugin_table
|
||||
}
|
||||
|
||||
fn show_status_structured(
|
||||
data_path: PathBuf,
|
||||
db_path: PathBuf,
|
||||
enabled_meta_plugins: &Vec<MetaPluginType>,
|
||||
output_format: OutputFormat,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let path_info = PathInfo {
|
||||
data: data_path.into_os_string().into_string().expect("Unable to convert data path to string"),
|
||||
database: db_path.into_os_string().into_string().expect("Unable to convert DB path to string"),
|
||||
};
|
||||
|
||||
let default_type = compression_engine::default_compression_type();
|
||||
let mut compression_info = Vec::new();
|
||||
|
||||
for compression_type in CompressionType::iter() {
|
||||
let compression_program: CompressionEngineProgram =
|
||||
match &COMPRESSION_PROGRAMS[compression_type.clone()] {
|
||||
Some(compression_program) => compression_program.clone(),
|
||||
None => CompressionEngineProgram {
|
||||
program: "".to_string(),
|
||||
compress: Vec::new(),
|
||||
decompress: Vec::new(),
|
||||
supported: true,
|
||||
},
|
||||
};
|
||||
|
||||
let is_default = compression_type == default_type;
|
||||
let binary = if compression_program.program.is_empty() {
|
||||
"<INTERNAL>".to_string()
|
||||
} else {
|
||||
compression_program.program
|
||||
};
|
||||
|
||||
compression_info.push(CompressionInfo {
|
||||
compression_type: compression_type.to_string(),
|
||||
found: compression_program.supported,
|
||||
default: is_default,
|
||||
binary,
|
||||
compress: compression_program.compress.join(" "),
|
||||
decompress: compression_program.decompress.join(" "),
|
||||
});
|
||||
}
|
||||
|
||||
let mut meta_plugin_info = Vec::new();
|
||||
|
||||
for meta_plugin_type in MetaPluginType::iter() {
|
||||
let mut meta_plugin = meta_plugin::get_meta_plugin(meta_plugin_type.clone());
|
||||
let is_supported = meta_plugin.is_supported();
|
||||
let is_enabled = enabled_meta_plugins.contains(&meta_plugin_type);
|
||||
|
||||
let (binary_display, args_display) = if !is_supported {
|
||||
("<NOT FOUND>".to_string(), "".to_string())
|
||||
} else {
|
||||
match meta_plugin_type {
|
||||
MetaPluginType::DigestSha256 | MetaPluginType::ReadTime | MetaPluginType::ReadRate |
|
||||
MetaPluginType::Cwd | MetaPluginType::Uid | MetaPluginType::User |
|
||||
MetaPluginType::Gid | MetaPluginType::Group | MetaPluginType::Shell |
|
||||
MetaPluginType::ShellPid | MetaPluginType::KeepPid | MetaPluginType::Hostname |
|
||||
MetaPluginType::FullHostname => {
|
||||
("<INTERNAL>".to_string(), "".to_string())
|
||||
},
|
||||
_ => {
|
||||
if let Some((program, args)) = meta_plugin.program_info() {
|
||||
(program.to_string(), args.join(" "))
|
||||
} else {
|
||||
("<NOT FOUND>".to_string(), "".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
meta_plugin_info.push(MetaPluginInfo {
|
||||
meta_name: meta_plugin.meta_name(),
|
||||
found: is_supported,
|
||||
enabled: is_enabled,
|
||||
binary: binary_display,
|
||||
args: args_display,
|
||||
});
|
||||
}
|
||||
|
||||
let status_info = StatusInfo {
|
||||
paths: path_info,
|
||||
compression: compression_info,
|
||||
meta_plugins: meta_plugin_info,
|
||||
};
|
||||
|
||||
match output_format {
|
||||
OutputFormat::Json => {
|
||||
println!("{}", serde_json::to_string_pretty(&status_info)?);
|
||||
}
|
||||
OutputFormat::Yaml => {
|
||||
println!("{}", serde_yaml::to_string(&status_info)?);
|
||||
}
|
||||
OutputFormat::Table => unreachable!(),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct StatusInfo {
|
||||
paths: PathInfo,
|
||||
compression: Vec<CompressionInfo>,
|
||||
meta_plugins: Vec<MetaPluginInfo>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PathInfo {
|
||||
data: String,
|
||||
database: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CompressionInfo {
|
||||
#[serde(rename = "type")]
|
||||
compression_type: String,
|
||||
found: bool,
|
||||
default: bool,
|
||||
binary: String,
|
||||
compress: String,
|
||||
decompress: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct MetaPluginInfo {
|
||||
meta_name: String,
|
||||
found: bool,
|
||||
enabled: bool,
|
||||
binary: String,
|
||||
args: String,
|
||||
}
|
||||
|
||||
pub fn mode_status(
|
||||
_cmd: &mut Command,
|
||||
args: &crate::Args,
|
||||
@@ -201,6 +336,12 @@ pub fn mode_status(
|
||||
}
|
||||
}
|
||||
|
||||
let output_format = get_output_format(args);
|
||||
|
||||
if output_format != OutputFormat::Table {
|
||||
return show_status_structured(data_path, db_path, &meta_plugin_types, output_format);
|
||||
}
|
||||
|
||||
println!("PATHS:");
|
||||
build_path_table(data_path, db_path).printstd();
|
||||
println!();
|
||||
|
||||
Reference in New Issue
Block a user