Skip to content

Commit

Permalink
fix(*): handle more IO errors when generating nginx.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Sep 15, 2023
1 parent 7c8a9db commit 211b25d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
33 changes: 20 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::env;
use std::fmt::Display;
use std::fs;
use std::fs::File;
use std::io::Write as IoWrite;
use std::process;
use std::process::Command;
use thiserror::Error as ThisError;
Expand Down Expand Up @@ -486,27 +485,35 @@ impl Action {
user.inline_lua.insert(0, jit.to_lua());
}

let lua_loader = match generate_lua_loader(
&prefix,
&user.lua_file,
&user.inline_lua,
&user.lua_args,
user.arg_0.clone(),
user.arg_c,
) {
Ok(ll) => ll,
Err(e) => {
eprintln!("failed to generate inline lua: {}", e);
return e.raw_os_error().unwrap_or(2);
}
};

let vars = Vars {
main_conf: main_conf(&mut user),
stream_enabled: !user.no_stream,
stream_conf: stream_conf(&mut user),
http_conf: http_conf(&mut user),
lua_loader: generate_lua_loader(
&prefix,
&user.lua_file,
&user.inline_lua,
&user.lua_args,
user.arg_0.clone(),
user.arg_c,
),
events_conf: vec![format!("worker_connections {};", user.worker_connections)],
lua_loader,
};

let conf_path = prefix.conf.join("nginx.conf");
let mut fh = std::fs::File::create(conf_path).unwrap();
fh.write_all(render_config(vars).as_bytes()).unwrap();
fh.flush().unwrap();
drop(fh);
if let Err(e) = fs::write(conf_path, render_config(vars)) {
eprintln!("failed writing nginx.conf file: {}", e);
return 2;
}

let ngx = NginxExec {
bin: find_nginx_bin(user.nginx_bin).to_str().unwrap().to_string(),
Expand Down
21 changes: 10 additions & 11 deletions src/lua.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::types::*;
use std::cmp::max;
use std::fs;
use std::io::Write as IoWrite;

// resty-cli has a fancier implementation that stores all observed "levels" in
// a hash. Then it iterates over 1..$max_level and checks for hash membership,
Expand Down Expand Up @@ -63,7 +62,7 @@ pub(crate) fn generate_lua_loader(
lua_args: &Vec<String>,
arg_0: String,
all_args_len: usize,
) -> Vec<String> {
) -> Result<Vec<String>, std::io::Error> {
let buf = Buf::new();
let inline_filename = prefix.conf.join("a.lua").to_str().unwrap().to_owned();

Expand Down Expand Up @@ -132,15 +131,15 @@ struct LuaGenerator<'a> {
}

impl<'a> LuaGenerator<'a> {
pub fn generate(mut self) -> Vec<String> {
pub fn generate(mut self) -> Result<Vec<String>, std::io::Error> {
self.buf.append("local gen");
self.buf.append("do");
self.buf.indent();

self.insert_lua_args();
self.buf.newline();

self.insert_inline_lua();
self.insert_inline_lua()?;
self.buf.newline();

self.insert_code_for_lua_file();
Expand All @@ -156,7 +155,7 @@ impl<'a> LuaGenerator<'a> {
self.buf.dedent();
self.buf.append("end");

self.buf.finalize()
Ok(self.buf.finalize())
}

fn insert_lua_args(&mut self) {
Expand Down Expand Up @@ -194,22 +193,22 @@ impl<'a> LuaGenerator<'a> {
self.buf.append(&format!("arg[{}] = {}", 0 - pos, prog));
}

fn insert_inline_lua(&mut self) {
fn insert_inline_lua(&mut self) -> Result<(), std::io::Error> {
self.buf.append("-- inline lua code");

if self.inline.is_empty() {
return;
return Ok(());
}

self.buf.append("local inline_gen");

let contents = self.inline.join("; ");
let fname = self.inline_filename.clone();
let mut fh = fs::File::create(&fname).unwrap();

fh.write_all(self.inline.join("; ").as_bytes()).unwrap();
fh.flush().unwrap();
fs::write(&fname, contents)?;

self.insert_lua_file_loader(&fname, true);

Ok(())
}

fn insert_code_for_lua_file(&mut self) {
Expand Down

0 comments on commit 211b25d

Please sign in to comment.