feat: make sure dir is there as well

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-01-28 16:41:50 +01:00
parent cc7aaf14eb
commit 85cc1d46db
15 changed files with 1153 additions and 447 deletions

View File

@@ -15,7 +15,10 @@ use crate::{
util::git::GitCommit,
};
use self::subcommands::render_template::RenderTemplateCommand;
use self::subcommands::{
render::RenderCommand, render_kustomize::RenderKustomizeCommand,
render_template::RenderTemplateCommand,
};
#[derive(Debug, Clone)]
pub struct CuddleCli {
@@ -73,9 +76,8 @@ impl CuddleCli {
if let Ok(context_iter) = self.context.clone().unwrap().lock() {
for ctx in context_iter.iter() {
if let Some(variables) = ctx.plan.vars.clone() {
for (name, var) in variables {
self.variables.push(CuddleVariable::new(name, var))
}
let mut variables: Vec<CuddleVariable> = variables.into();
self.variables.append(&mut variables);
}
if let CuddleTreeType::Root = ctx.node_type {
@@ -83,7 +85,7 @@ impl CuddleCli {
temp_path.push(".cuddle/tmp");
self.variables.push(CuddleVariable::new(
"tmp".into(),
"tmp",
temp_path.clone().to_string_lossy().to_string(),
));
@@ -93,10 +95,9 @@ impl CuddleCli {
}
match GitCommit::new() {
Ok(commit) => self.variables.push(CuddleVariable::new(
"commit_sha".into(),
commit.commit_sha.clone(),
)),
Ok(commit) => self
.variables
.push(CuddleVariable::new("commit_sha", commit.commit_sha.clone())),
Err(e) => {
log::debug!("{}", e);
}
@@ -126,6 +127,7 @@ impl CuddleCli {
name,
l.description.clone(),
)),
CuddleScript::Rust(_) => todo!(),
}
}
}
@@ -136,7 +138,7 @@ impl CuddleCli {
}
fn process_templates(self) -> anyhow::Result<Self> {
if let None = self.tmp_dir {
if self.tmp_dir.is_none() {
log::debug!("cannot process template as bare bones cli");
return Ok(self);
}
@@ -147,10 +149,8 @@ impl CuddleCli {
.clone()
.ok_or(anyhow::anyhow!("tmp_dir does not exist aborting"))?;
match self.config.get_fetch_policy()? {
CuddleFetchPolicy::Always => {
if tmp_dir.exists() && tmp_dir.ends_with("tmp") {
std::fs::remove_dir_all(tmp_dir.clone())?;
}
CuddleFetchPolicy::Always if tmp_dir.exists() && tmp_dir.ends_with("tmp") => {
std::fs::remove_dir_all(tmp_dir.clone())?;
}
_ => {}
}
@@ -169,12 +169,30 @@ impl CuddleCli {
continue;
}
for file in std::fs::read_dir(template_path)?.into_iter() {
for file in std::fs::read_dir(&template_path)? {
let f = file?;
let mut dest_file = tmp_dir.clone();
dest_file.push(f.file_name());
dest_file.push(f.path().strip_prefix(&template_path)?.parent().unwrap());
std::fs::copy(f.path(), dest_file)?;
tracing::trace!(
"moving from: {} to {}",
f.path().display(),
dest_file.display()
);
if f.path().is_dir() {
std::fs::create_dir_all(&dest_file)?;
}
fs_extra::copy_items(
&[f.path()],
&dest_file,
&fs_extra::dir::CopyOptions {
overwrite: true,
skip_exist: false,
..Default::default()
},
)?;
}
}
}
@@ -194,6 +212,8 @@ impl CuddleCli {
root_cmd = subcommands::x::build_command(root_cmd, self.clone());
root_cmd = subcommands::render_template::build_command(root_cmd);
root_cmd = subcommands::render_kustomize::build_command(root_cmd);
root_cmd = subcommands::render::build_command(root_cmd);
root_cmd = subcommands::init::build_command(root_cmd, self.clone());
self.command = Some(root_cmd);
@@ -223,11 +243,20 @@ impl CuddleCli {
let res = match matches.subcommand() {
Some(("x", exe_submatch)) => subcommands::x::execute_x(exe_submatch, self.clone()),
Some(("render", sub_matches)) => {
RenderCommand::execute(sub_matches, self.clone())?;
Ok(())
}
Some(("render_template", sub_matches)) => {
RenderTemplateCommand::from_matches(sub_matches, self.clone())
.and_then(|cmd| cmd.execute())?;
Ok(())
}
Some(("render-kustomize", sub_matches)) => {
RenderKustomizeCommand::from_matches(sub_matches, self.clone())
.and_then(|cmd| cmd.execute())?;
Ok(())
}
Some(("init", sub_matches)) => {
subcommands::init::execute_init(sub_matches, self.clone())
}

View File

@@ -0,0 +1,168 @@
use std::{collections::HashMap, path::PathBuf};
use anyhow::Context;
use clap::{Arg, ArgAction, ArgMatches, Command};
use serde_json::from_value;
use tera::Function;
use crate::{cli::CuddleCli, model::CuddleVariable};
pub fn build_command(root_cmd: Command) -> Command {
root_cmd.subcommand(
Command::new("folder")
.about("renders a template folder")
.args(&[
Arg::new("source")
.long("source")
.required(true)
.value_parser(clap::value_parser!(PathBuf)),
Arg::new("destination")
.long("destination")
.required(true)
.value_parser(clap::value_parser!(PathBuf)),
Arg::new("extra-var")
.long("extra-var")
.action(ArgAction::Append)
.required(false),
]),
)
}
pub struct FolderCommand {
variables: Vec<CuddleVariable>,
source: PathBuf,
destination: PathBuf,
}
impl FolderCommand {
pub fn from_matches(matches: &ArgMatches, cli: CuddleCli) -> anyhow::Result<Self> {
let source = matches
.get_one::<PathBuf>("source")
.expect("source")
.clone();
let destination = matches
.get_one::<PathBuf>("destination")
.expect("destination")
.clone();
let mut extra_vars: Vec<CuddleVariable> =
if let Some(extra_vars) = matches.get_many::<String>("extra-var") {
let mut vars = Vec::with_capacity(extra_vars.len());
for var in extra_vars.into_iter() {
let parts: Vec<&str> = var.split('=').collect();
if parts.len() != 2 {
return Err(anyhow::anyhow!("extra-var: is not set correctly: {}", var));
}
vars.push(CuddleVariable::new(parts[0], parts[1]));
}
vars
} else {
vec![]
};
extra_vars.append(&mut cli.variables.clone());
Ok(Self {
variables: extra_vars,
source,
destination,
})
}
pub fn execute(self) -> anyhow::Result<()> {
std::fs::remove_dir_all(&self.destination)?;
std::fs::create_dir_all(&self.destination)?;
// Prepare context
let mut context = tera::Context::new();
for var in &self.variables {
context.insert(var.name.to_lowercase().replace([' ', '-'], "_"), &var.value)
}
let mut tera = tera::Tera::default();
tera.register_function("filter_by_prefix", filter_by_prefix(self.variables.clone()));
for entry in walkdir::WalkDir::new(&self.source) {
let entry = entry?;
let entry_path = entry.path();
let rel_path = self
.destination
.join(entry_path.strip_prefix(&self.source)?);
if entry_path.is_file() {
// Load source template
let source = std::fs::read_to_string(entry_path)?;
let output = tera.render_str(&source, &context)?;
if let Some(parent) = rel_path.parent() {
std::fs::create_dir_all(parent)?;
}
// Put template in final destination
std::fs::write(&rel_path, output).context(format!(
"failed to write to destination: {}",
&rel_path.display()
))?;
log::info!("finished writing template to: {}", &rel_path.display());
}
}
Ok(())
}
}
fn filter_by_prefix(variables: Vec<CuddleVariable>) -> impl Function {
Box::new(
move |args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
for var in &variables {
tracing::info!("variable: {} - {}", var.name, var.value);
}
let prefix = match args.get("prefix") {
Some(value) => match from_value::<Vec<String>>(value.clone()) {
Ok(prefix) => prefix,
Err(e) => {
tracing::error!("prefix was not a string: {}", e);
return Err("prefix was not a string".into());
}
},
None => return Err("prefix is required".into()),
};
let prefix = prefix.join("_");
let vars = variables
.iter()
.filter_map(|v| {
if v.name.starts_with(&prefix) {
Some(CuddleVariable::new(
v.name.trim_start_matches(&prefix).trim_start_matches('_'),
v.value.clone(),
))
} else {
None
}
})
.collect::<Vec<CuddleVariable>>();
tracing::info!("was here");
let mut structure: HashMap<String, String> = HashMap::new();
for var in vars {
tracing::info!("found: {} - {}", &var.name, &var.value);
structure.insert(var.name, var.value);
}
Ok(serde_json::to_value(structure).unwrap())
},
)
}
fn filter_by_name(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
Ok(tera::Value::Null)
}

View File

@@ -0,0 +1,85 @@
use std::{io::Write, path::PathBuf};
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");
std::fs::remove_dir_all(&self.destination)?;
std::fs::create_dir_all(&self.destination)?;
let cmd = cmd.arg("kustomize").arg(self.kustomize_folder);
let output = cmd.output()?;
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()?;
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(&output.stdout)?;
}
let output = child.wait_with_output()?;
if !output.status.success() {
anyhow::bail!(
"failed to run kustomize: {}",
output.status.code().expect("to find exit code")
)
}
Ok(())
}
}

View File

@@ -1,3 +1,7 @@
pub mod folder;
pub mod init;
pub mod kustomize;
pub mod render;
pub mod render_kustomize;
pub mod render_template;
pub mod x;

View File

@@ -0,0 +1,31 @@
use clap::{ArgMatches, Command};
use crate::cli::CuddleCli;
use super::{folder::FolderCommand, kustomize::KustomizeCommand};
pub fn build_command(root_cmd: Command) -> Command {
let cmd = Command::new("render").about("accesses different render commands");
let cmd = super::kustomize::build_command(cmd);
let cmd = super::folder::build_command(cmd);
root_cmd.subcommand(cmd)
}
pub struct RenderCommand {}
impl RenderCommand {
pub fn execute(matches: &ArgMatches, cli: CuddleCli) -> anyhow::Result<()> {
match matches.subcommand() {
Some(("kustomize", sub_matches)) => {
KustomizeCommand::from_matches(sub_matches, cli)?.execute()?;
}
Some(("folder", sub_matches)) => {
FolderCommand::from_matches(sub_matches, cli)?.execute()?;
}
_ => anyhow::bail!("failed to find match for render"),
}
Ok(())
}
}

View File

@@ -0,0 +1,70 @@
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(())
}
}

View File

@@ -1,11 +1,12 @@
use std::{path::PathBuf, str::FromStr};
use clap::{Arg, ArgGroup, ArgMatches, Command};
use anyhow::Context;
use clap::{Arg, ArgMatches, Command};
use crate::{cli::CuddleCli, model::CuddleVariable};
const DESTINATION: &'static str = "destination is the output path of the template once done, but default .tmpl is stripped and the normal file extension is used. this can be overwritten if a file path is entered instead. I.e. (/some/file/name.txt)";
const TEMPLATE_FILE: &'static str = "template-file is the input file path of the .tmpl file (or inferred) that you would like to render";
const DESTINATION: &str = "destination is the output path of the template once done, but default .tmpl is stripped and the normal file extension is used. this can be overwritten if a file path is entered instead. I.e. (/some/file/name.txt)";
const TEMPLATE_FILE: &str = "template-file is the input file path of the .tmpl file (or inferred) that you would like to render";
pub fn build_command(root_cmd: Command) -> Command {
root_cmd.subcommand(
@@ -51,18 +52,19 @@ impl RenderTemplateCommand {
.get_one::<String>("destination")
.ok_or(anyhow::anyhow!("destination was not found"))
.and_then(get_path_buf_and_check_dir_exists)
.and_then(RenderTemplateCommand::transform_extension)?;
.and_then(RenderTemplateCommand::transform_extension)
.context("failed to access dest directory")?;
let mut extra_vars: Vec<CuddleVariable> =
if let Some(extra_vars) = matches.get_many::<String>("extra-var") {
let mut vars = Vec::with_capacity(extra_vars.len());
for var in extra_vars.into_iter() {
let parts: Vec<&str> = var.split("=").collect();
let parts: Vec<&str> = var.split('=').collect();
if parts.len() != 2 {
return Err(anyhow::anyhow!("extra-var: is not set correctly: {}", var));
}
vars.push(CuddleVariable::new(parts[0].into(), parts[1].into()));
vars.push(CuddleVariable::new(parts[0], parts[1]));
}
vars
} else {
@@ -82,10 +84,7 @@ impl RenderTemplateCommand {
// Prepare context
let mut context = tera::Context::new();
for var in self.variables {
context.insert(
var.name.to_lowercase().replace(" ", "_").replace("-", "_"),
&var.value,
)
context.insert(var.name.to_lowercase().replace([' ', '-'], "_"), &var.value)
}
// Load source template
@@ -93,8 +92,15 @@ impl RenderTemplateCommand {
let output = tera::Tera::one_off(source.as_str(), &context, false)?;
if let Some(parent) = self.destination.parent() {
std::fs::create_dir_all(parent)?;
}
// Put template in final destination
std::fs::write(&self.destination, output)?;
std::fs::write(&self.destination, output).context(format!(
"failed to write to destination: {}",
&self.destination.display(),
))?;
log::info!(
"finished writing template to: {}",
@@ -123,8 +129,8 @@ impl RenderTemplateCommand {
}
}
fn get_path_buf_and_check_exists(raw_path: &String) -> anyhow::Result<PathBuf> {
match PathBuf::from_str(&raw_path) {
fn get_path_buf_and_check_exists(raw_path: impl Into<String>) -> anyhow::Result<PathBuf> {
match PathBuf::from_str(&raw_path.into()) {
Ok(pb) => {
if pb.exists() {
Ok(pb)
@@ -139,17 +145,9 @@ fn get_path_buf_and_check_exists(raw_path: &String) -> anyhow::Result<PathBuf> {
}
}
fn get_path_buf_and_check_dir_exists(raw_path: &String) -> anyhow::Result<PathBuf> {
match PathBuf::from_str(&raw_path) {
Ok(pb) => {
if pb.is_dir() && pb.exists() {
Ok(pb)
} else if pb.is_file() {
Ok(pb)
} else {
Ok(pb)
}
}
fn get_path_buf_and_check_dir_exists(raw_path: impl Into<String>) -> anyhow::Result<PathBuf> {
match PathBuf::from_str(&raw_path.into()) {
Ok(pb) => Ok(pb),
Err(e) => Err(anyhow::anyhow!(e)),
}
}

View File

@@ -61,6 +61,7 @@ pub fn build_scripts(cli: CuddleCli) -> Vec<Command> {
}
crate::model::CuddleScript::Dagger(_) => todo!(),
crate::model::CuddleScript::Lua(l) => {}
crate::model::CuddleScript::Rust(_) => todo!(),
}
cmds.push(cmd)