Files
cuddle/cuddle/src/cli/subcommands/kustomize.rs
kjuulh 7b136b1331
All checks were successful
continuous-integration/drone/push Build is passing
feat: enable helm via. kustomize
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-02-06 21:35:27 +01:00

92 lines
2.6 KiB
Rust

use std::{io::Write, path::PathBuf};
use anyhow::Context;
use clap::{Arg, ArgMatches, Command};
use crate::cli::CuddleCli;
pub fn build_command(root_cmd: Command) -> Command {
root_cmd.subcommand(
Command::new("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 KustomizeCommand {
kustomize_folder: PathBuf,
destination: PathBuf,
}
impl KustomizeCommand {
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");
let _ = std::fs::remove_dir_all(&self.destination);
std::fs::create_dir_all(&self.destination)?;
let cmd = cmd
.arg("kustomize")
.arg("--enable-helm")
.arg(self.kustomize_folder);
let output = cmd.output().context("failed to run kubectl kustomize")?;
if !output.status.success() {
anyhow::bail!(
"failed to run kustomize: {}",
output.status.code().expect("to find exit code")
)
}
let mut cmd = std::process::Command::new("kubectl-slice");
let cmd = cmd
.arg("-o")
.arg(self.destination)
.stdin(std::process::Stdio::piped());
let mut child = cmd.spawn().context("failed to run kubectl-slice")?;
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(&output.stdout)?;
}
let output = child
.wait_with_output()
.context("failed to run kubectl-slice")?;
if !output.status.success() {
anyhow::bail!(
"failed to run kustomize: {}",
output.status.code().expect("to find exit code")
)
}
Ok(())
}
}