All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
90 lines
3.3 KiB
Rust
90 lines
3.3 KiB
Rust
use clap::{Arg, ArgMatches, Command};
|
|
|
|
use crate::cli::CuddleCli;
|
|
|
|
pub fn build_command(root_cmd: Command, cli: CuddleCli) -> Command {
|
|
if cli.scripts.len() > 0 {
|
|
let execute_cmd_about = "x is your entry into your domains scripts, scripts inherited from parents will also be present here";
|
|
let mut execute_cmd = Command::new("x")
|
|
.about(execute_cmd_about)
|
|
.subcommand_required(true);
|
|
|
|
execute_cmd = execute_cmd.subcommands(&build_scripts(cli));
|
|
|
|
root_cmd.subcommand(execute_cmd)
|
|
} else {
|
|
root_cmd
|
|
}
|
|
}
|
|
|
|
pub fn build_scripts(cli: CuddleCli) -> Vec<Command> {
|
|
let mut cmds = Vec::new();
|
|
for script in cli.scripts.iter() {
|
|
let mut cmd = Command::new(&script.name);
|
|
|
|
if let Some(desc) = &script.description {
|
|
cmd = cmd.about(desc)
|
|
}
|
|
|
|
match &script.script {
|
|
crate::model::CuddleScript::Shell(shell_script) => {
|
|
if let Some(args) = &shell_script.args {
|
|
for (arg_name, arg) in args {
|
|
cmd = match arg {
|
|
crate::model::CuddleShellScriptArg::Env(arg_env) => cmd.arg(
|
|
Arg::new(arg_name.clone())
|
|
.env(arg_name.to_uppercase().replace(".", "_"))
|
|
.required(true),
|
|
),
|
|
crate::model::CuddleShellScriptArg::Flag(arg_flag) => {
|
|
let mut arg_val = Arg::new(arg_name.clone())
|
|
.env(arg_name.to_uppercase().replace(".", "_"))
|
|
.long(arg_name);
|
|
|
|
if let Some(true) = arg_flag.required {
|
|
arg_val = arg_val.required(true);
|
|
}
|
|
|
|
if let Some(def) = &arg_flag.default_value {
|
|
arg_val = arg_val.default_value(def);
|
|
}
|
|
|
|
if let Some(desc) = &arg_flag.description {
|
|
arg_val = arg_val.help(&*desc.clone().leak())
|
|
}
|
|
|
|
cmd.arg(arg_val)
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|
|
crate::model::CuddleScript::Dagger(_) => todo!(),
|
|
crate::model::CuddleScript::Lua(l) => {}
|
|
crate::model::CuddleScript::Rust(_) => todo!(),
|
|
}
|
|
|
|
cmds.push(cmd)
|
|
}
|
|
|
|
cmds
|
|
}
|
|
|
|
pub fn execute_x(exe_submatch: &ArgMatches, cli: CuddleCli) -> anyhow::Result<()> {
|
|
match exe_submatch.subcommand() {
|
|
Some((name, action_matches)) => {
|
|
log::trace!(action=name; "running action; name={}", name);
|
|
match cli.scripts.iter().find(|ele| ele.name == name) {
|
|
Some(script) => {
|
|
script
|
|
.clone()
|
|
.execute(action_matches, cli.variables.clone())?;
|
|
Ok(())
|
|
}
|
|
_ => Err(anyhow::anyhow!("could not find a match")),
|
|
}
|
|
}
|
|
_ => Err(anyhow::anyhow!("could not find a match")),
|
|
}
|
|
}
|