Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose OpenAPI parsing and ability to not overwrite Cargo.toml #10

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading