@@ -1,13 +0,0 @@
|
||||
[package]
|
||||
name = "cuddle-actions"
|
||||
edition = "2021"
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
clap = { workspace = true, features = ["string"] }
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.0"
|
@@ -1,125 +0,0 @@
|
||||
// Cuddle actions is a two part action, it is called from cuddle itself, second the cli uses a provided sdk to expose functionality
|
||||
|
||||
use std::{collections::BTreeMap, ffi::OsString, io::Write};
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
// Fix design make it so that it works like axum!
|
||||
|
||||
type ActionFn = dyn Fn() -> anyhow::Result<()> + 'static;
|
||||
|
||||
struct Action {
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
f: Box<ActionFn>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
struct ActionSchema<'a> {
|
||||
name: &'a str,
|
||||
description: Option<&'a str>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
struct ActionsSchema<'a> {
|
||||
actions: Vec<ActionSchema<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct CuddleActions {
|
||||
actions: BTreeMap<String, Action>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct AddActionOptions {
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
impl CuddleActions {
|
||||
pub fn add_action<F>(
|
||||
&mut self,
|
||||
name: &str,
|
||||
action_fn: F,
|
||||
options: &AddActionOptions,
|
||||
) -> &mut Self
|
||||
where
|
||||
F: Fn() -> anyhow::Result<()> + 'static,
|
||||
{
|
||||
self.actions.insert(
|
||||
name.into(),
|
||||
Action {
|
||||
name: name.into(),
|
||||
description: options.description.clone(),
|
||||
f: Box::new(action_fn),
|
||||
},
|
||||
);
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn execute(&mut self) -> anyhow::Result<()> {
|
||||
self.execute_from(std::env::args())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn execute_from<I, T>(&mut self, items: I) -> anyhow::Result<()>
|
||||
where
|
||||
I: IntoIterator<Item = T>,
|
||||
T: Into<OsString> + Clone,
|
||||
{
|
||||
let mut do_cmd = clap::Command::new("do").subcommand_required(true);
|
||||
|
||||
for action in self.actions.values() {
|
||||
let mut do_action_cmd = clap::Command::new(action.name.clone());
|
||||
if let Some(description) = &action.description {
|
||||
do_action_cmd = do_action_cmd.about(description.clone());
|
||||
}
|
||||
|
||||
do_cmd = do_cmd.subcommand(do_action_cmd);
|
||||
}
|
||||
|
||||
let root = clap::Command::new("cuddle-action")
|
||||
.subcommand_required(true)
|
||||
.subcommand(clap::Command::new("schema"))
|
||||
.subcommand(do_cmd);
|
||||
|
||||
let matches = root.try_get_matches_from(items)?;
|
||||
match matches.subcommand().expect("subcommand to be required") {
|
||||
("schema", _args) => {
|
||||
let output = self.get_pretty_actions()?;
|
||||
|
||||
// Write all stdout to buffer
|
||||
std::io::stdout().write_all(output.as_bytes())?;
|
||||
std::io::stdout().write_all("\n".as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
("do", args) => {
|
||||
let (command_name, _args) = args.subcommand().unwrap();
|
||||
match self.actions.get_mut(command_name) {
|
||||
Some(action) => (*action.f)(),
|
||||
None => {
|
||||
anyhow::bail!("command not found: {}", command_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => anyhow::bail!("no command found"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pretty_actions(&self) -> anyhow::Result<String> {
|
||||
let schema = self
|
||||
.actions
|
||||
.values()
|
||||
.map(|a| ActionSchema {
|
||||
name: &a.name,
|
||||
description: a.description.as_deref(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let output = serde_json::to_string_pretty(&ActionsSchema { actions: schema })?;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
use cuddle_actions::AddActionOptions;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_can_schema_no_actions() -> anyhow::Result<()> {
|
||||
let output = cuddle_actions::CuddleActions::default().get_pretty_actions()?;
|
||||
|
||||
assert_eq!("[]", &output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_schema_simple_action() -> anyhow::Result<()> {
|
||||
let output = cuddle_actions::CuddleActions::default()
|
||||
.add_action("something", || Ok(()), &AddActionOptions::default())
|
||||
.get_pretty_actions()?;
|
||||
|
||||
assert_eq!(
|
||||
r#"[
|
||||
{
|
||||
"name": "something",
|
||||
"description": null
|
||||
}
|
||||
]"#,
|
||||
&output
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_call_simple_action() -> anyhow::Result<()> {
|
||||
cuddle_actions::CuddleActions::default()
|
||||
.add_action("something", || Ok(()), &AddActionOptions::default())
|
||||
.execute_from(vec!["cuddle-actions", "do", "something"])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_fail_on_unknown_command() -> anyhow::Result<()> {
|
||||
let res = cuddle_actions::CuddleActions::default().execute_from(vec![
|
||||
"cuddle-actions",
|
||||
"do",
|
||||
"something",
|
||||
]);
|
||||
|
||||
assert!(res.is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_cmd_can_fail() -> anyhow::Result<()> {
|
||||
let res = cuddle_actions::CuddleActions::default()
|
||||
.add_action(
|
||||
"something",
|
||||
|| anyhow::bail!("failed to run cmd"),
|
||||
&AddActionOptions::default(),
|
||||
)
|
||||
.execute_from(vec!["cuddle-actions", "do", "something"]);
|
||||
|
||||
assert!(res.is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user