Skip to content

Commit

Permalink
Merge pull request #10 from golemcloud/lib-improvements
Browse files Browse the repository at this point in the history
Expose OpenAPI parsing and ability to not overwrite Cargo.toml
  • Loading branch information
vigoo authored Mar 20, 2024
2 parents 9c5b914 + 9cc5985 commit c6d8d2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
41 changes: 33 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::result;

use openapiv3::OpenAPI;

pub use merger::merge_all_openapi_specs;

use crate::rust::lib_gen::{Module, ModuleDef, ModuleName};
use crate::rust::model_gen::RefCache;
use openapiv3::OpenAPI;
use std::path::Path;
use std::result;

pub(crate) mod merger;
pub(crate) mod printer;
mod rust;
mod toml;

pub use merger::merge_all_openapi_specs;

#[derive(Debug, Clone)]
pub enum Error {
Unexpected { message: String },
Expand Down Expand Up @@ -57,7 +61,13 @@ impl Error {

pub type Result<T> = result::Result<T, Error>;

pub fn gen(openapi_specs: Vec<OpenAPI>, target: &Path, name: &str, version: &str) -> Result<()> {
pub fn gen(
openapi_specs: Vec<OpenAPI>,
target: &Path,
name: &str,
version: &str,
overwrite_cargo: bool,
) -> Result<()> {
let open_api = merge_all_openapi_specs(openapi_specs)?;

let src = target.join("src");
Expand Down Expand Up @@ -138,8 +148,23 @@ pub fn gen(openapi_specs: Vec<OpenAPI>, target: &Path, name: &str, version: &str
let lib = rust::lib_gen::lib_gen("crate", &module_defs);
std::fs::write(src.join("lib.rs"), lib).unwrap();

let cargo = toml::cargo::gen(name, version);
std::fs::write(target.join("Cargo.toml"), cargo).unwrap();
if overwrite_cargo {
let cargo = toml::cargo::gen(name, version);
std::fs::write(target.join("Cargo.toml"), cargo).unwrap();
}

Ok(())
}

pub fn parse_openapi_specs(
spec: &[PathBuf],
) -> std::result::Result<Vec<OpenAPI>, Box<dyn std::error::Error>> {
spec.iter()
.map(|spec_path| {
let file = File::open(spec_path)?;
let reader = BufReader::new(file);
let openapi: OpenAPI = serde_yaml::from_reader(reader)?;
Ok(openapi)
})
.collect()
}
24 changes: 7 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use clap::{Args, Parser};
use golem_openapi_client_generator::gen;
use openapiv3::OpenAPI;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

use clap::{Args, Parser};

use golem_openapi_client_generator::{gen, parse_openapi_specs};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, rename_all = "kebab-case")]
enum Cli {
Expand Down Expand Up @@ -56,32 +56,22 @@ fn main() {

match command {
Cli::Generate(args) => {
let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap();
let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap();
gen(
openapi_specs,
&args.output_directory,
&args.name,
&args.client_version,
true,
)
.unwrap();
}
Cli::Merge(args) => {
let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap();
let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap();
let openapi =
golem_openapi_client_generator::merge_all_openapi_specs(openapi_specs).unwrap();
let file = File::create(&args.output_yaml).unwrap();
serde_yaml::to_writer(file, &openapi).unwrap();
}
}
}

fn parse_openapi_specs(spec: Vec<PathBuf>) -> Result<Vec<OpenAPI>, Box<dyn std::error::Error>> {
spec.iter()
.map(|spec_path| {
let file = File::open(spec_path)?;
let reader = BufReader::new(file);
let openapi: OpenAPI = serde_yaml::from_reader(reader)?;
Ok(openapi)
})
.collect()
}

0 comments on commit c6d8d2e

Please sign in to comment.