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

Format generated codes and make err outputs readable for volo-cli #253

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 9 additions & 3 deletions volo-build/src/grpc_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,15 @@ impl CodegenBackend for VoloGrpcBackend {
let name = self.cx().rust_name(method.def_id);

format!(
r#"async fn {name}(&self, {args}) -> ::std::result::Result<{ret_ty}>{{
{default_result}
}}"#
r#"
async fn {name}(
&self,
{args},
) -> ::std::result::Result<{ret_ty}>
{{
{default_result}
}}
"#
)
}

Expand Down
25 changes: 25 additions & 0 deletions volo-build/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::HashMap, path::PathBuf};

use anyhow::anyhow;
use serde::{Deserialize, Serialize};

use crate::util::get_repo_latest_commit_id;
Expand Down Expand Up @@ -48,12 +49,36 @@ impl Idl {
Source::Local => Ok(()),
}
}

pub fn ensure_readable(&self) -> anyhow::Result<()> {
// We should ensure that:
// 1. All the files exist (`ENOENT` may occur)
// 2. All the files can be accessed by the current user (`EPERM` may occur)
// 3. All the files can be read by the current user (`EPERM` may occur)
// The simplest method is opening it with read perm (`O_RDONLY`)

try_open_readonly(&self.path)
.map_err(|e| anyhow!("{}: {}", self.path.to_str().unwrap(), e))?;

if let Some(includes) = &self.includes {
for inc in includes.iter() {
try_open_readonly(inc).map_err(|e| anyhow!("{}: {}", inc.to_str().unwrap(), e))?;
}
}

Ok(())
}
}

fn default_keep_unknown_fields() -> bool {
false
}

fn try_open_readonly<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<()> {
let _ = std::fs::OpenOptions::new().read(true).open(path)?;
Ok(())
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "source")]
pub enum Source {
Expand Down
12 changes: 9 additions & 3 deletions volo-build/src/thrift_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,15 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
};

format!(
r#"async fn {name}(&self, {args}) -> ::core::result::Result<{ret_ty}, {exception}>{{
Ok(Default::default())
}}"#
r#"
async fn {name}(
&self,
{args},
) -> ::core::result::Result<{ret_ty}, {exception}>
{{
Ok(Default::default())
}}
"#
)
}

Expand Down
7 changes: 4 additions & 3 deletions volo-cli/src/bin/volo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use anyhow::Result;
use clap::Parser;
use colored::*;
use log::{debug, error};
use update_informer::{registry, Check};
use volo_cli::model;

fn main() -> Result<()> {
fn main() {
// set default log level if not set
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "WARN");
Expand Down Expand Up @@ -46,5 +45,7 @@ fn main() -> Result<()> {
println!("\n{outdated_msg}\n{update_msg}");
}

res
if res.is_err() {
std::process::exit(1);
}
}
5 changes: 4 additions & 1 deletion volo-cli/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::create_dir_all, path::PathBuf};
use std::{fs::create_dir_all, path::PathBuf, process::Command};

use clap::{value_parser, Parser};
use volo_build::{
Expand Down Expand Up @@ -198,6 +198,7 @@ impl CliCommand for Init {
});
}
idl.path = self.idl.clone();
idl.ensure_readable()?;

let mut entry = Entry {
protocol: idl.protocol(),
Expand Down Expand Up @@ -272,6 +273,8 @@ impl CliCommand for Init {
PathBuf::from("./volo-gen/").join(DEFAULT_CONFIG_FILE),
)?;

let _ = Command::new("cargo").arg("fmt").arg("--all").output()?;

Ok(())
}
}
2 changes: 0 additions & 2 deletions volo-cli/src/templates/grpc/src/bin/server_rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


use std::net::SocketAddr;

use volo_grpc::server::{{Server, ServiceBuilder}};
Expand Down
6 changes: 1 addition & 5 deletions volo-cli/src/templates/grpc/src/lib_rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@


pub struct S;

impl volo_gen::{service_global_name} for S {{
{methods}
}}
impl volo_gen::{service_global_name} for S {{{methods}}}
4 changes: 1 addition & 3 deletions volo-cli/src/templates/thrift/src/bin/server_rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@


use std::net::SocketAddr;

use {name}::{{S}};
use {name}::S;

#[volo::main]
async fn main() {{
Expand Down
6 changes: 1 addition & 5 deletions volo-cli/src/templates/thrift/src/lib_rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@


pub struct S;

impl volo_gen::{service_global_name} for S {{
{methods}
}}
impl volo_gen::{service_global_name} for S {{{methods}}}
2 changes: 0 additions & 2 deletions volo-cli/src/templates/thrift/volo-gen/src/lib_rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


mod gen {{
volo::include_service!("volo_gen.rs");
}}
Expand Down
Loading