1use anyhow::anyhow;
2use clap::{Arg, Command};
3
4use crate::lualib::init_std;
5
6pub async fn dekit_main() -> anyhow::Result<()> {
7 println!("* Welcome to dekit — playground for future features *\n");
8
9 let cmd = clap::command!()
10 .subcommands([
11 Command::new("up"),
12 Command::new("down"),
13 Command::new("server").subcommands([
14 Command::new("start"),
15 Command::new("stop"),
16 Command::new("status"),
17 ]),
18 ])
19 .arg(
20 Arg::new("files")
21 .action(clap::ArgAction::Append)
22 .trailing_var_arg(true),
23 );
24 let matches = cmd.get_matches();
25
26 match matches.subcommand() {
27 Some(("up", _sub_m)) => {
28 println!("Up.");
29 }
30 Some(("down", _sub_m)) => {
31 println!("Down.");
32 }
33 Some(("server", sub_m)) => {
34 match sub_m.subcommand() {
35 Some(("start", _sub_m)) => {
36 println!("Start server.");
37 }
38 Some(("stop", _sub_m)) => {
39 println!("Stop server.");
40 }
41 Some(("status", _sub_m)) => {
42 println!("Server status.");
43 }
44 _ => {
45 }
47 }
48 }
49 Some((arg, _sub_m)) => {
50 println!("Unknown: {}", arg);
51 }
52 None => {
53 println!("None.");
54 let paths = matches
55 .get_many::<String>("files")
56 .map(|p| p.collect::<Vec<_>>())
57 .unwrap_or_default();
58 println!("paths = {:?}", paths);
59
60 #[allow(clippy::collapsible_if)]
61 if let Some(first) = paths.first() {
62 if first.ends_with(".lua") {
63 println!("Running the script.");
64
65 let src = std::fs::read_to_string(first)?;
66
67 let lua = mlua::Lua::new();
68 let cancel = tokio_util::sync::CancellationToken::new();
69 lua.set_app_data(cancel.clone());
70 lua
71 .globals()
72 .set("std", init_std(&lua).map_err(|e| anyhow!("{}", e))?)
73 .map_err(|e| anyhow!("{}", e))?;
74 println!("After std init.");
75
76 let chunk = lua.load(src.clone());
77 let f: mlua::Function = chunk.eval().map_err(|e| anyhow!("{}", e))?;
78 let r = f
79 .call_async::<mlua::Value>(())
80 .await
81 .map_err(|e| anyhow!("{}", e))?;
82 println!("-> {:?}", r);
83 cancel.cancel();
84 }
85 }
86 }
87 }
88
89 Ok(())
90}