1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use clap::{Parser, Subcommand};
use crate::{conn, container, error::Error, generate_live, generate_managed, CodegenSettings};
/// Command line interface to interact with Cornucopia SQL.
#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
/// Use `podman` instead of `docker`
#[clap(short, long)]
podman: bool,
/// Folder containing the queries
#[clap(short, long, default_value = "queries/")]
queries_path: String,
/// Destination folder for generated modules
#[clap(short, long, default_value = "src/cornucopia.rs")]
destination: String,
#[clap(subcommand)]
action: Action,
/// Generate synchronous rust code. Async otherwise.
#[clap(long)]
sync: bool,
/// Derive serde's `Serialize` trait for generated types.
#[clap(long)]
serialize: bool,
}
#[derive(Debug, Subcommand)]
enum Action {
/// Generate your modules against your own db
Live {
/// Postgres url to the database
url: String,
},
/// Generate your modules against schema files
Schema {
/// SQL files containing the database schema
schema_files: Vec<String>,
},
}
// Main entrypoint of the CLI. Parses the args and calls the appropriate routines.
pub fn run() -> Result<(), Error> {
let Args {
podman,
queries_path,
destination,
action,
sync,
serialize,
} = Args::parse();
match action {
Action::Live { url } => {
let mut client = conn::from_url(&url)?;
generate_live(
&mut client,
&queries_path,
Some(&destination),
CodegenSettings {
is_async: !sync,
derive_ser: serialize,
},
)?;
}
Action::Schema { schema_files } => {
// Run the generate command. If the command is unsuccessful, cleanup Cornucopia's container
if let Err(e) = generate_managed(
&queries_path,
schema_files,
Some(&destination),
podman,
CodegenSettings {
is_async: !sync,
derive_ser: serialize,
},
) {
container::cleanup(podman).ok();
return Err(e);
}
}
};
Ok(())
}