Files
cuddle/cuddle/src/cli/subcommands/render_kustomize.rs
kjuulh 85cc1d46db
All checks were successful
continuous-integration/drone/push Build is passing
feat: make sure dir is there as well
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-01-28 16:42:34 +01:00

71 lines
1.8 KiB
Rust

use std::path::PathBuf;
use clap::{Arg, ArgMatches, Command};
use crate::cli::CuddleCli;
pub fn build_command(root_cmd: Command) -> Command {
root_cmd.subcommand(
Command::new("render-kustomize")
.about("renders a kustomize folder")
.args(&[
Arg::new("kustomize-folder")
.long("kustomize-folder")
.value_parser(clap::value_parser!(PathBuf))
.required(true),
Arg::new("destination")
.long("destination")
.required(true)
.value_parser(clap::value_parser!(PathBuf)),
]),
)
}
pub struct RenderKustomizeCommand {
kustomize_folder: PathBuf,
destination: PathBuf,
}
impl RenderKustomizeCommand {
pub fn from_matches(matches: &ArgMatches, _cli: CuddleCli) -> anyhow::Result<Self> {
let kustomize_folder = matches
.get_one::<PathBuf>("kustomize-folder")
.expect("kustomize-folder")
.clone();
let destination = matches
.get_one::<PathBuf>("destination")
.expect("destination")
.clone();
Ok(Self {
kustomize_folder,
destination,
})
}
pub fn execute(self) -> anyhow::Result<()> {
let mut cmd = std::process::Command::new("kubectl");
std::fs::create_dir_all(&self.destination)?;
let cmd = cmd
.arg("kustomize")
.arg(self.kustomize_folder)
.arg(format!("--output={}", self.destination.display()));
let mut process = cmd.spawn()?;
let exit = process.wait()?;
if !exit.success() {
anyhow::bail!(
"failed to run kustomize: {}",
exit.code().expect("to find exit code")
)
}
Ok(())
}
}