smart_locker/commands/
export.rs1use crate::commands::list;
2use crate::LockerResult;
3use crate::SmartLockerError;
4use std::env;
5use std::fs;
6use std::path::PathBuf;
7
8pub struct ExportFormat;
9
10impl ExportFormat {
11 pub fn export_env_with_placeholders(
12 secret_names: &Vec<String>,
13 output_path: &PathBuf,
14 ) -> LockerResult<()> {
15 let mut content = String::new();
16 for secret_name in secret_names {
17 content.push_str(&format!(
18 "{}=$(smart-locker decrypt -n {})\n",
19 secret_name, secret_name
20 ));
21 }
22 fs::write(output_path, content).map_err(|e| {
23 SmartLockerError::FileSystemError(format!("Error writing .env file: {}", e))
24 })?;
25 Ok(())
26 }
27}
28
29pub fn export(format: &str, output_file: Option<&str>) -> LockerResult<()> {
30 let current_dir: PathBuf = env::current_dir().map_err(|_| {
31 SmartLockerError::FileSystemError("Unable to get current directory".to_string())
32 })?;
33
34 let output_path = match output_file {
36 Some(file) => current_dir.join(file),
37 None => current_dir.join(".env"), };
39
40 let secret_list: Vec<String> = list::list_secrets_names()?;
42 if secret_list.is_empty() {
43 eprintln!("Aucun secret à exporter.");
44 return Ok(());
45 }
46
47 match format {
48 "env" => ExportFormat::export_env_with_placeholders(&secret_list, &output_path)?,
49 _ => eprintln!("Format non supporté : {}", format),
50 }
51
52 println!("Le fichier a été exporté à : {:?}", output_path);
53 Ok(())
54}