Skip to main content

kubemgrwasm/interfaces/
cli.rs

1use crate::utils::errors::KubeMergeError;
2use crate::utils::merger::KubeconfigContent;
3use clap::Parser;
4use std::fs;
5use std::path::PathBuf;
6
7#[derive(Debug)]
8pub struct CliOptions {
9    pub files: Vec<String>,
10    pub output_path: Option<String>,
11}
12
13#[derive(Parser, Debug)]
14#[command(name = "kubemgr")]
15#[command(about = "The fastest way to merge Kubernetes configuration files 🏎.")]
16pub struct Cli {
17    #[arg(
18        required = true,
19        value_name = "FILES",
20        help = "Kubernetes configuration files to merge"
21    )]
22    pub files: Vec<String>,
23
24    #[arg(
25        short,
26        long,
27        value_name = "FILE",
28        help = "Specify output file path (prints to stdout if not specified)"
29    )]
30    pub output: Option<String>,
31}
32
33/// Validates the input files and options
34pub fn validate_input(files: &[String]) -> Result<(), KubeMergeError> {
35    if files.len() < 2 {
36        return Err(KubeMergeError::InsufficientFiles(
37            "At least two kubeconfig files must be provided".to_string(),
38        ));
39    }
40
41    for file in files {
42        if !PathBuf::from(file).exists() {
43            return Err(KubeMergeError::FileNotFound(format!(
44                "File '{}' not found",
45                file
46            )));
47        }
48    }
49
50    Ok(())
51}
52
53/// Load kubeconfig files and convert them to KubeconfigContent
54pub fn load_kubeconfig_files(paths: &[String]) -> Result<Vec<KubeconfigContent>, KubeMergeError> {
55    let mut contents = Vec::new();
56
57    for path in paths {
58        let content = fs::read_to_string(path).map_err(|error| {
59            KubeMergeError::FileReadError(format!(
60                "Failed to read file '{}', {}",
61                path.clone(),
62                error
63            ))
64        })?;
65
66        contents.push(KubeconfigContent { content });
67    }
68
69    Ok(contents)
70}