Files
cuddle/cuddle/src/cli/mod.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

414 lines
14 KiB
Rust

mod subcommands;
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use clap::Command;
use crate::{
actions::CuddleAction,
config::{CuddleConfig, CuddleFetchPolicy},
context::{CuddleContext, CuddleTreeType},
model::*,
util::git::GitCommit,
};
use self::subcommands::{
render::RenderCommand, render_kustomize::RenderKustomizeCommand,
render_template::RenderTemplateCommand,
};
#[derive(Debug, Clone)]
pub struct CuddleCli {
scripts: Vec<CuddleAction>,
variables: Vec<CuddleVariable>,
context: Option<Arc<Mutex<Vec<CuddleContext>>>>,
command: Option<Command>,
tmp_dir: Option<PathBuf>,
config: CuddleConfig,
}
impl CuddleCli {
pub fn new(
context: Option<Arc<Mutex<Vec<CuddleContext>>>>,
config: CuddleConfig,
) -> anyhow::Result<CuddleCli> {
let mut cli = CuddleCli {
scripts: vec![],
variables: vec![],
context: context.clone(),
command: None,
tmp_dir: None,
config,
};
if let Ok(provider) = std::env::var("CUDDLE_SECRETS_PROVIDER") {
let provider = provider
.split(",")
.map(|p| p.to_string())
.collect::<Vec<_>>();
tracing::trace!("secrets-provider enabled, handling for each entry");
handle_providers(provider)?;
std::thread::sleep(std::time::Duration::from_millis(100));
}
match context {
Some(_) => {
tracing::debug!("build full cli");
cli = cli
.process_variables()
.process_scripts()
.process_templates()?
.build_cli();
}
None => {
tracing::debug!("build bare cli");
cli = cli.build_bare_cli();
}
}
Ok(cli)
}
fn process_variables(mut self) -> Self {
if let Ok(context_iter) = self.context.clone().unwrap().lock() {
for ctx in context_iter.iter() {
if let Some(variables) = ctx.plan.vars.clone() {
let mut variables: Vec<CuddleVariable> = variables.into();
self.variables.append(&mut variables);
}
if let CuddleTreeType::Root = ctx.node_type {
let mut temp_path = ctx.path.clone();
temp_path.push(".cuddle/tmp");
self.variables.push(CuddleVariable::new(
"tmp",
temp_path.clone().to_string_lossy().to_string(),
));
self.tmp_dir = Some(temp_path);
}
}
}
match GitCommit::new() {
Ok(commit) => self
.variables
.push(CuddleVariable::new("commit_sha", commit.commit_sha.clone())),
Err(e) => {
log::debug!("{}", e);
}
}
self
}
fn process_scripts(mut self) -> Self {
if let Ok(context_iter) = self.context.clone().unwrap().lock() {
for ctx in context_iter.iter() {
if let Some(scripts) = ctx.plan.scripts.clone() {
for (name, script) in scripts {
match &script {
CuddleScript::Shell(shell_script) => {
self.scripts.push(CuddleAction::new(
script.clone(),
ctx.path.clone(),
name,
shell_script.description.clone(),
))
}
CuddleScript::Dagger(_) => todo!(),
CuddleScript::Lua(l) => self.scripts.push(CuddleAction::new(
script.clone(),
ctx.path.clone(),
name,
l.description.clone(),
)),
CuddleScript::Rust(_) => todo!(),
}
}
}
}
}
self
}
fn process_templates(self) -> anyhow::Result<Self> {
if self.tmp_dir.is_none() {
log::debug!("cannot process template as bare bones cli");
return Ok(self);
}
// Make sure tmp_dir exists and clean it up first
let tmp_dir = self
.tmp_dir
.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())?;
}
_ => {}
}
std::fs::create_dir_all(tmp_dir.clone())?;
// Handle all templating with variables and such.
// TODO: use actual templating engine, for new we just copy templates to the final folder
if let Ok(context_iter) = self.context.clone().unwrap().lock() {
for ctx in context_iter.iter() {
let mut template_path = ctx.path.clone();
template_path.push("templates");
log::trace!("template path: {}", template_path.clone().to_string_lossy());
if !template_path.exists() {
continue;
}
for file in std::fs::read_dir(&template_path)? {
let f = file?;
let mut dest_file = tmp_dir.clone();
dest_file.push(f.path().strip_prefix(&template_path)?.parent().unwrap());
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()
},
)?;
}
}
}
Ok(self)
}
fn build_cli(mut self) -> Self {
let mut root_cmd = Command::new("cuddle")
.version("1.0")
.author("kjuulh <contact@kasperhermansen.com>")
.about("cuddle is your domain specific organization tool. It enabled widespread sharing through repositories, as well as collaborating while maintaining speed and integrity")
.subcommand_required(true)
.arg_required_else_help(true)
.propagate_version(true)
.arg(clap::Arg::new("secrets-provider").long("secrets-provider").env("CUDDLE_SECRETS_PROVIDER"));
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);
self
}
fn build_bare_cli(mut self) -> Self {
let mut root_cmd = Command::new("cuddle")
.version("1.0")
.author("kjuulh <contact@kasperhermansen.com>")
.about("cuddle is your domain specific organization tool. It enabled widespread sharing through repositories, as well as collaborating while maintaining speed and integrity")
.subcommand_required(true)
.arg_required_else_help(true)
.propagate_version(true);
root_cmd = subcommands::init::build_command(root_cmd, self.clone());
self.command = Some(root_cmd);
self
}
pub fn execute(self) -> anyhow::Result<Self> {
if let Some(cli) = self.command.clone() {
let matches = cli.clone().get_matches();
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())
}
_ => Err(anyhow::anyhow!("could not find a match")),
};
match res {
Ok(()) => {}
Err(e) => {
return Err(e);
}
}
}
Ok(self)
}
}
pub enum SecretProvider {
OnePassword {
inject: Vec<String>,
dotenv: Option<String>,
},
}
impl TryFrom<String> for SecretProvider {
type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"1password" => {
let one_password_inject = std::env::var("CUDDLE_ONE_PASSWORD_INJECT")
.ok()
.filter(|f| f.as_str() != "");
let one_password_dot_env = std::env::var("CUDDLE_ONE_PASSWORD_DOT_ENV").ok();
let injectables = one_password_inject
.unwrap_or(String::new())
.split(",")
.filter(|s| s.contains('='))
.map(|i| i.to_string())
.collect::<Vec<_>>();
// for i in &injectables {
// if !std::path::PathBuf::from(i).exists() {
// anyhow::bail!("1pass injectable path doesn't exist: {}", i);
// }
// }
if let Some(one_password_dot_env) = &one_password_dot_env {
if let Ok(dir) = std::env::current_dir() {
tracing::trace!(
current_dir = dir.display().to_string(),
dotenv = &one_password_dot_env,
exists = PathBuf::from(&one_password_dot_env).exists(),
"1password dotenv inject"
);
}
}
Ok(Self::OnePassword {
inject: injectables,
dotenv: if let Some(one_password_dot_env) = one_password_dot_env {
if PathBuf::from(&one_password_dot_env).exists() {
Some(one_password_dot_env)
} else {
None
}
} else {
None
},
})
}
value => {
tracing::debug!(
"provided secrets manager doesn't match any allowed values {}",
value
);
Err(anyhow::anyhow!("value is not one of supported values"))
}
}
}
}
fn handle_providers(provider: Vec<String>) -> anyhow::Result<()> {
fn execute_1password(lookup: &str) -> anyhow::Result<String> {
let out = std::process::Command::new("op")
.arg("read")
.arg(lookup)
.output()?;
let secret = std::str::from_utf8(&out.stdout)?;
Ok(secret.to_string())
}
fn execute_1password_inject(file: &str) -> anyhow::Result<Vec<(String, String)>> {
let out = std::process::Command::new("op")
.arg("inject")
.arg("--in-file")
.arg(file)
.output()?;
let secrets = std::str::from_utf8(&out.stdout)?.split('\n');
let secrets_pair = secrets
.map(|secrets_pair| secrets_pair.split_once("="))
.flatten()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect::<Vec<(String, String)>>();
Ok(secrets_pair)
}
let res = provider
.into_iter()
.map(|p| SecretProvider::try_from(p))
.collect::<anyhow::Result<Vec<_>>>();
let res = res?;
let res = res
.into_iter()
.map(|p| match p {
SecretProvider::OnePassword { inject, dotenv } => {
tracing::trace!(
inject = inject.join(","),
dotenv = dotenv,
"handling 1password"
);
if let Some(dotenv) = dotenv {
let pairs = execute_1password_inject(&dotenv).unwrap();
for (key, value) in pairs {
tracing::debug!(env_name = &key, value=&value, "set var from 1password");
std::env::set_var(key, value);
}
}
for i in inject {
let (env_var_name, op_lookup) = i.split_once("=").ok_or(anyhow::anyhow!(
"CUDDLE_ONE_PASSWORD_INJECT is not a key value pair ie. key:value,key2=value2"
))?;
let secret = execute_1password(&op_lookup)?;
std::env::set_var(&env_var_name, secret);
tracing::debug!(
env_name = &env_var_name,
lookup = &op_lookup,
"set var from 1password"
);
}
Ok(())
}
})
.collect::<anyhow::Result<Vec<()>>>();
let _ = res?;
Ok(())
}