use anyhow::Result; use clap::Command; use serde::{Deserialize, Serialize}; use serde_yaml; use crate::meta_plugin::MetaPlugin; #[derive(Debug, Serialize, Deserialize)] struct DefaultConfig { dir: Option, list_format: Vec, human_readable: bool, output_format: Option, quiet: bool, force: bool, server: Option, compression_plugin: Option, meta_plugins: Option>, } #[derive(Debug, Serialize, Deserialize)] struct ColumnConfig { name: String, label: Option, #[serde(default)] align: ColumnAlignment, #[serde(default)] max_len: Option, } #[derive(Debug, Serialize, Deserialize, Default)] #[serde(rename_all = "lowercase")] enum ColumnAlignment { #[default] Left, Right, } #[derive(Debug, Serialize, Deserialize)] struct ServerConfig { address: Option, port: Option, password_file: Option, password: Option, password_hash: Option, } #[derive(Debug, Serialize, Deserialize)] struct CompressionPluginConfig { name: String, } #[derive(Debug, Serialize, Deserialize)] struct MetaPluginConfig { name: String, #[serde(default)] options: std::collections::HashMap, #[serde(default)] outputs: std::collections::HashMap, } pub fn mode_generate_config(_cmd: &mut Command, _settings: &crate::config::Settings) -> Result<()> { // Create instances of each meta plugin to get their default options and outputs let text_plugin = crate::meta_plugin::text::TextMetaPlugin::new(None, None); let cwd_plugin = crate::meta_plugin::cwd::CwdMetaPlugin::new(None, None); let digest_plugin = crate::meta_plugin::digest::DigestMetaPlugin::new(None, None); let hostname_plugin = crate::meta_plugin::hostname::HostnameMetaPlugin::new(None, None); let magic_file_plugin = crate::meta_plugin::magic::MagicFileMetaPlugin::new(None, None); // Create a default configuration let default_config = DefaultConfig { dir: Some("~/.local/share/keep".to_string()), list_format: vec![ ColumnConfig { name: "id".to_string(), label: Some("Item".to_string()), align: ColumnAlignment::Right, max_len: None, }, ColumnConfig { name: "time".to_string(), label: Some("Time".to_string()), align: ColumnAlignment::Right, max_len: None, }, ColumnConfig { name: "size".to_string(), label: Some("Size".to_string()), align: ColumnAlignment::Right, max_len: None, }, ColumnConfig { name: "meta:text_line_count".to_string(), label: Some("Lines".to_string()), align: ColumnAlignment::Right, max_len: None, }, ColumnConfig { name: "tags".to_string(), label: Some("Tags".to_string()), align: ColumnAlignment::Left, max_len: Some("40".to_string()), }, ColumnConfig { name: "meta:hostname_full".to_string(), label: Some("Hostname".to_string()), align: ColumnAlignment::Left, max_len: Some("28".to_string()), }, ], human_readable: false, output_format: Some("table".to_string()), quiet: false, force: false, server: Some(ServerConfig { address: Some("127.0.0.1".to_string()), port: Some(8080), password_file: None, password: None, password_hash: None, }), compression_plugin: None, meta_plugins: Some(vec![ MetaPluginConfig { name: "text".to_string(), options: text_plugin.options().clone(), outputs: convert_outputs_to_string_map(text_plugin.outputs()), }, MetaPluginConfig { name: "cwd".to_string(), options: cwd_plugin.options().clone(), outputs: convert_outputs_to_string_map(cwd_plugin.outputs()), }, MetaPluginConfig { name: "digest".to_string(), options: digest_plugin.options().clone(), outputs: convert_outputs_to_string_map(digest_plugin.outputs()), }, MetaPluginConfig { name: "hostname".to_string(), options: hostname_plugin.options().clone(), outputs: convert_outputs_to_string_map(hostname_plugin.outputs()), }, MetaPluginConfig { name: "magic_file".to_string(), options: magic_file_plugin.options().clone(), outputs: convert_outputs_to_string_map(magic_file_plugin.outputs()), }, ]), }; // Serialize to YAML and print to stdout let yaml = serde_yaml::to_string(&default_config)?; println!("{}", yaml); Ok(()) } // Helper function to convert outputs from serde_yaml::Value to String fn convert_outputs_to_string_map( outputs: &std::collections::HashMap, ) -> std::collections::HashMap { let mut result = std::collections::HashMap::new(); for (key, value) in outputs { if let Some(str_value) = value.as_str() { result.insert(key.clone(), str_value.to_string()); } else { // Convert non-string values to their YAML string representation result.insert(key.clone(), serde_yaml::to_string(value).unwrap_or_default()); } } result }