dagger-rs/src/cli_generate.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

2023-01-29 15:23:03 +01:00
use std::io::Write;
use std::sync::Arc;
2023-01-29 15:23:03 +01:00
2023-01-28 20:21:19 +01:00
use clap::{Arg, ArgMatches};
use dagger_codegen::generate;
use dagger_codegen::rust::RustGenerator;
2023-02-05 22:26:58 +01:00
use dagger_core::config::Config;
use dagger_core::engine::Engine;
use dagger_core::session::Session;
2023-01-28 20:21:19 +01:00
#[allow(dead_code)]
pub struct GenerateCommand;
#[allow(dead_code)]
impl GenerateCommand {
pub fn new_cmd() -> clap::Command {
clap::Command::new("generate").arg(Arg::new("output").long("output"))
}
pub fn exec(arg_matches: &ArgMatches) -> eyre::Result<()> {
let cfg = Config::default();
let (conn, _proc) = Engine::new().start(&cfg)?;
let session = Session::new();
let req = session.start(&cfg, &conn)?;
let schema = session.schema(req)?;
let code = generate(
schema.into_schema().schema.unwrap(),
Arc::new(RustGenerator {}),
)?;
2023-01-28 20:21:19 +01:00
if let Some(output) = arg_matches.get_one::<String>("output") {
2023-01-29 15:23:03 +01:00
let mut file = std::fs::File::create(output)?;
file.write(code.as_bytes())?;
2023-01-28 20:21:19 +01:00
} else {
2023-01-29 15:19:21 +01:00
println!("{}", code);
2023-01-28 20:21:19 +01:00
}
Ok(())
}
}