Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
173 lines
5.8 KiB
Rust
173 lines
5.8 KiB
Rust
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<String>,
|
|
list_format: Vec<ColumnConfig>,
|
|
human_readable: bool,
|
|
output_format: Option<String>,
|
|
quiet: bool,
|
|
force: bool,
|
|
server: Option<ServerConfig>,
|
|
compression_plugin: Option<CompressionPluginConfig>,
|
|
meta_plugins: Option<Vec<MetaPluginConfig>>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct ColumnConfig {
|
|
name: String,
|
|
label: Option<String>,
|
|
#[serde(default)]
|
|
align: ColumnAlignment,
|
|
#[serde(default)]
|
|
max_len: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
|
#[serde(rename_all = "lowercase")]
|
|
enum ColumnAlignment {
|
|
#[default]
|
|
Left,
|
|
Right,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct ServerConfig {
|
|
address: Option<String>,
|
|
port: Option<u16>,
|
|
password_file: Option<String>,
|
|
password: Option<String>,
|
|
password_hash: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct CompressionPluginConfig {
|
|
name: String,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct MetaPluginConfig {
|
|
name: String,
|
|
#[serde(default)]
|
|
options: std::collections::HashMap<String, serde_yaml::Value>,
|
|
#[serde(default)]
|
|
outputs: std::collections::HashMap<String, String>,
|
|
}
|
|
|
|
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<String, serde_yaml::Value>,
|
|
) -> std::collections::HashMap<String, String> {
|
|
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
|
|
}
|