diff --git a/Cargo.lock b/Cargo.lock index bdbd0ce8..c630b2f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2668,7 +2668,7 @@ dependencies = [ [[package]] name = "volo-build" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "dirs", @@ -2694,7 +2694,7 @@ dependencies = [ [[package]] name = "volo-cli" -version = "0.8.0" +version = "0.8.1" dependencies = [ "anyhow", "clap", diff --git a/volo-build/Cargo.toml b/volo-build/Cargo.toml index b9884559..f245c93e 100644 --- a/volo-build/Cargo.toml +++ b/volo-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "volo-build" -version = "0.8.2" +version = "0.8.3" edition.workspace = true homepage.workspace = true repository.workspace = true diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index eee3e306..0c00b39b 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -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} + }} +"# ) } diff --git a/volo-build/src/model.rs b/volo-build/src/model.rs index b2f9babd..f8ea9eae 100644 --- a/volo-build/src/model.rs +++ b/volo-build/src/model.rs @@ -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; @@ -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>(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 { diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index 8a4367f0..dc750c0a 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -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()) + }} +"# ) } diff --git a/volo-cli/Cargo.toml b/volo-cli/Cargo.toml index 3acfa44c..e856bc63 100644 --- a/volo-cli/Cargo.toml +++ b/volo-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "volo-cli" -version = "0.8.0" +version = "0.8.1" edition.workspace = true homepage.workspace = true repository.workspace = true diff --git a/volo-cli/src/bin/volo.rs b/volo-cli/src/bin/volo.rs index f29b2375..01c68d0c 100644 --- a/volo-cli/src/bin/volo.rs +++ b/volo-cli/src/bin/volo.rs @@ -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"); @@ -46,5 +45,7 @@ fn main() -> Result<()> { println!("\n{outdated_msg}\n{update_msg}"); } - res + if res.is_err() { + std::process::exit(1); + } } diff --git a/volo-cli/src/init.rs b/volo-cli/src/init.rs index fb3cf2cb..a7c08d62 100644 --- a/volo-cli/src/init.rs +++ b/volo-cli/src/init.rs @@ -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::{ @@ -198,6 +198,7 @@ impl CliCommand for Init { }); } idl.path = self.idl.clone(); + idl.ensure_readable()?; let mut entry = Entry { protocol: idl.protocol(), @@ -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(()) } } diff --git a/volo-cli/src/templates/grpc/src/bin/server_rs b/volo-cli/src/templates/grpc/src/bin/server_rs index 5d92fed9..33194067 100644 --- a/volo-cli/src/templates/grpc/src/bin/server_rs +++ b/volo-cli/src/templates/grpc/src/bin/server_rs @@ -1,5 +1,3 @@ - - use std::net::SocketAddr; use volo_grpc::server::{{Server, ServiceBuilder}}; diff --git a/volo-cli/src/templates/grpc/src/lib_rs b/volo-cli/src/templates/grpc/src/lib_rs index c5fe0d21..c4935fe1 100644 --- a/volo-cli/src/templates/grpc/src/lib_rs +++ b/volo-cli/src/templates/grpc/src/lib_rs @@ -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}}} diff --git a/volo-cli/src/templates/thrift/src/bin/server_rs b/volo-cli/src/templates/thrift/src/bin/server_rs index b9b255e3..9dddffee 100644 --- a/volo-cli/src/templates/thrift/src/bin/server_rs +++ b/volo-cli/src/templates/thrift/src/bin/server_rs @@ -1,8 +1,6 @@ - - use std::net::SocketAddr; -use {name}::{{S}}; +use {name}::S; #[volo::main] async fn main() {{ diff --git a/volo-cli/src/templates/thrift/src/lib_rs b/volo-cli/src/templates/thrift/src/lib_rs index c5fe0d21..c4935fe1 100644 --- a/volo-cli/src/templates/thrift/src/lib_rs +++ b/volo-cli/src/templates/thrift/src/lib_rs @@ -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}}} diff --git a/volo-cli/src/templates/thrift/volo-gen/src/lib_rs b/volo-cli/src/templates/thrift/volo-gen/src/lib_rs index fe51797c..e9d7fc00 100644 --- a/volo-cli/src/templates/thrift/volo-gen/src/lib_rs +++ b/volo-cli/src/templates/thrift/volo-gen/src/lib_rs @@ -1,5 +1,3 @@ - - mod gen {{ volo::include_service!("volo_gen.rs"); }}