use std::sync::{Arc, RwLock};
use nu_plugin::{Plugin, PluginCommand};
pub mod commands;
pub mod config;
pub mod memory_optimizations;
pub mod performance_monitoring;
pub mod redaction;
mod secret_types;
pub mod startup_optimizations;
pub mod tera_functions;
use commands::*;
pub use config::ConfigManager;
pub use secret_types::{
SecretBinary, SecretBool, SecretDate, SecretFloat, SecretInt, SecretList, SecretRecord,
SecretString,
};
#[derive(Clone)]
pub struct SecretPlugin {
config_manager: Arc<RwLock<ConfigManager>>,
}
impl SecretPlugin {
pub fn new(config_manager: ConfigManager) -> Self {
Self {
config_manager: Arc::new(RwLock::new(config_manager)),
}
}
pub fn config_manager(&self) -> &Arc<RwLock<ConfigManager>> {
&self.config_manager
}
}
impl Default for SecretPlugin {
fn default() -> Self {
#[cfg(not(miri))]
let config_manager = ConfigManager::load().unwrap_or_else(|_| {
ConfigManager::new(config::PluginConfig::default())
});
#[cfg(miri)]
let config_manager = ConfigManager::new(config::PluginConfig::default());
Self {
config_manager: Arc::new(RwLock::new(config_manager)),
}
}
}
impl Plugin for SecretPlugin {
fn version(&self) -> String {
env!("CARGO_PKG_VERSION").into()
}
fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin = Self>>> {
startup_optimizations::command_optimizations::init_command_cache();
let _ = redaction::init_redaction_templating();
vec![
Box::new(SecretWrapCommand),
Box::new(SecretWrapWithCommand),
Box::new(SecretUnwrapCommand),
Box::new(SecretContainsCommand),
Box::new(SecretHashCommand),
Box::new(SecretIsEmptyCommand),
Box::new(SecretLengthCommand),
Box::new(SecretInfoCommand),
Box::new(SecretValidateCommand),
Box::new(SecretValidateFormatCommand),
Box::new(SecretTypeOfCommand),
Box::new(SecretConfigureCommand),
Box::new(SecretConfigShowCommand),
Box::new(SecretConfigResetCommand),
Box::new(SecretConfigValidateCommand),
Box::new(SecretConfigExportCommand),
Box::new(SecretConfigImportCommand),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plugin_version() {
let plugin = SecretPlugin::default();
assert!(!plugin.version().is_empty());
}
#[test]
fn test_plugin_commands() {
let plugin = SecretPlugin::default();
let commands = plugin.commands();
assert_eq!(commands.len(), 17);
let command_names: Vec<&str> = commands.iter().map(|cmd| cmd.name()).collect();
assert!(command_names.contains(&"secret wrap"));
assert!(command_names.contains(&"secret wrap-with"));
assert!(command_names.contains(&"secret unwrap"));
assert!(command_names.contains(&"secret contains"));
assert!(command_names.contains(&"secret hash"));
assert!(command_names.contains(&"secret is-empty"));
assert!(command_names.contains(&"secret length"));
assert!(command_names.contains(&"secret info"));
assert!(command_names.contains(&"secret validate"));
assert!(command_names.contains(&"secret validate-format"));
assert!(command_names.contains(&"secret type-of"));
assert!(command_names.contains(&"secret configure"));
assert!(command_names.contains(&"secret config show"));
assert!(command_names.contains(&"secret config reset"));
assert!(command_names.contains(&"secret config validate"));
assert!(command_names.contains(&"secret config export"));
assert!(command_names.contains(&"secret config import"));
}
}