feat: with schema

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-07-02 00:47:14 +02:00
parent 6a82e0c10a
commit 12bd424f18
8 changed files with 164 additions and 23 deletions

View File

@@ -43,6 +43,7 @@ impl CuddleCli {
match context {
Some(_) => {
tracing::debug!("build full cli");
cli = cli
.process_variables()
.process_scripts()
@@ -50,6 +51,7 @@ impl CuddleCli {
.build_cli();
}
None => {
tracing::debug!("build bare cli");
cli = cli.build_bare_cli();
}
}
@@ -98,8 +100,17 @@ impl CuddleCli {
for ctx in context_iter.iter() {
if let Some(scripts) = ctx.plan.scripts.clone() {
for (name, script) in scripts {
self.scripts
.push(CuddleAction::new(script.clone(), ctx.path.clone(), name))
match &script {
CuddleScript::Shell(shell_script) => {
self.scripts.push(CuddleAction::new(
script.clone(),
ctx.path.clone(),
name,
shell_script.description.clone(),
))
}
CuddleScript::Dagger(_) => todo!(),
}
}
}
}

View File

@@ -1,4 +1,4 @@
use clap::{ArgMatches, Command};
use clap::{Arg, ArgMatches, Command};
use crate::cli::CuddleCli;
@@ -9,11 +9,7 @@ pub fn build_command(root_cmd: Command, cli: CuddleCli) -> Command {
.about(execute_cmd_about)
.subcommand_required(true);
for script in cli.scripts.iter() {
let action_cmd = Command::new(&script.name).about(&script.name);
execute_cmd = execute_cmd.subcommand(action_cmd);
}
execute_cmd = execute_cmd.subcommands(&build_scripts(cli));
root_cmd.subcommand(execute_cmd)
} else {
@@ -21,13 +17,62 @@ pub fn build_command(root_cmd: Command, cli: CuddleCli) -> Command {
}
}
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);
}
cmd.arg(arg_val)
}
};
}
}
}
crate::model::CuddleScript::Dagger(_) => todo!(),
}
cmds.push(cmd)
}
cmds
}
pub fn execute_x(exe_submatch: &ArgMatches, cli: CuddleCli) -> anyhow::Result<()> {
match exe_submatch.subcommand() {
Some((name, _action_matches)) => {
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(cli.variables.clone())?;
script
.clone()
.execute(action_matches, cli.variables.clone())?;
Ok(())
}
_ => Err(anyhow::anyhow!("could not find a match")),