Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Dec 20, 2023
1 parent c10b247 commit 19d56c6
Show file tree
Hide file tree
Showing 2 changed files with 384 additions and 299 deletions.
188 changes: 18 additions & 170 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lua::*;
use crate::nginx;
use crate::nginx::*;
use crate::run::run;
use crate::types::*;
Expand All @@ -10,7 +11,6 @@ use std::fmt::Display;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::process;
use std::process::Command;
use thiserror::Error as ThisError;

Expand Down Expand Up @@ -159,162 +159,6 @@ fn main_conf(user: &mut UserArgs) -> Vec<String> {
conf
}

pub struct NginxExec {
prefix: String,
runner: Runner,
bin: String,
label: Option<String>,
}

impl From<NginxExec> for Command {
fn from(ngx: NginxExec) -> Self {
let root = ngx.prefix;

// resty CLI always adds a trailing slash
let prefix = format!("{}/", root.trim_end_matches('/'));

let nginx = ngx.bin;

let mut nginx_args = vec![
String::from("-p"),
prefix,
String::from("-c"),
String::from("conf/nginx.conf"),
];

if let Some(label) = ngx.label {
nginx_args.insert(0, String::from("-g"));
nginx_args.insert(1, label);
}

let bin: String;
let mut args: Vec<String> = vec![];

match ngx.runner {
Runner::Default => {
bin = nginx;
args.append(&mut nginx_args);
}
Runner::RR => {
bin = String::from("rr");
args.push(String::from("record"));
args.push(nginx);
args.append(&mut nginx_args);
}
Runner::Stap(opts) => {
bin = String::from("stap");
args = vec![];
if let Some(opts) = opts {
args.append(&mut split_shell_args(&opts));
}
args.push("-c".to_owned());
nginx_args.insert(0, nginx);
args.push(join_shell_args(&nginx_args));
}
Runner::Valgrind(opts) => {
bin = "valgrind".to_owned();
args = vec![];
if let Some(opts) = opts {
args.append(&mut split_shell_args(&opts));
}
args.push(nginx);
args.append(&mut nginx_args);
}
Runner::Gdb(opts) => {
bin = String::from("gdb");
if let Some(opts) = opts {
args.append(&mut split_shell_args(&opts));
}
args.push("--args".to_owned());
args.push(nginx);
args.append(&mut nginx_args);
}
Runner::User(runner) => {
let mut user_args = split_shell_args(&runner);
bin = user_args.remove(0);
args.append(&mut user_args);
args.push(nginx);
args.append(&mut nginx_args);
}
};

let mut c = process::Command::new(bin);

c.args(args);
c
}
}

#[derive(Default, Debug)]
pub(crate) enum Runner {
#[default]
Default,
RR,
Stap(Option<String>),
Valgrind(Option<String>),
Gdb(Option<String>),
User(String),
}

impl Runner {
fn arg_name(&self) -> String {
match self {
Self::RR => "--rr",
Self::Stap(_) => "--stap",
Self::Gdb(_) => "--gdb",
Self::Valgrind(_) => "--valgrind",
Self::User(_) => "--user-runner",
_ => unreachable!(),
}
.to_owned()
}

fn opt_name(&self) -> String {
self.arg_name() + "-opts"
}

fn same(&self, other: &Runner) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}

fn takes_opts(&self) -> bool {
match self {
Self::Stap(_) => true,
Self::Gdb(_) => true,
Self::Valgrind(_) => true,
Self::User(_) => false,
Self::RR => false,
Self::Default => false,
}
}

fn has_opts(&self) -> bool {
match self {
Self::Stap(o) | Self::Gdb(o) | Self::Valgrind(o) => o.is_some(),
Self::User(_) => true,
Self::RR => false,
Self::Default => false,
}
}

fn update(&mut self, new: Runner) -> Result<(), ArgError> {
if let Runner::Default = self {
*self = new;
Ok(())
} else if self.same(&new) {
// e.g. we already saw --gdb and are now adding opts with --gdb-opts
if self.takes_opts() && !self.has_opts() && new.has_opts() {
*self = new;
Ok(())
} else {
Err(ArgError::Duplicate(new.opt_name()))
}
} else {
Err(ArgError::Conflict(self.arg_name(), new.arg_name()))
}
}
}

#[derive(ThisError, Debug)]
pub enum ArgError {
#[error("ERROR: could not find {0} include file '{1}'")]
Expand Down Expand Up @@ -490,8 +334,10 @@ impl Action {
user.inline_lua.insert(0, jit.to_lua());
}

let resty_compat_version = get_resty_compat_version();
let mut label = None;
if get_resty_compat_version() >= 30 {

if resty_compat_version >= 30 {
let mut s = String::from("# ");
if !user.inline_lua.is_empty() {
s.push_str("-e '");
Expand Down Expand Up @@ -527,16 +373,6 @@ impl Action {
}
};

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),
events_conf: vec![format!("worker_connections {};", user.worker_connections)],
lua_loader,
resty_compat_version: crate::nginx::get_resty_compat_version(),
};

let conf_path = prefix.conf.join("nginx.conf");
let mut file = match fs::File::create(conf_path) {
Ok(file) => file,
Expand All @@ -546,14 +382,26 @@ impl Action {
}
};

if let Err(e) = render_config(&mut file, vars).and_then(|_| file.flush()) {
let events_conf = vec![format!("worker_connections {};", user.worker_connections)];

let res = nginx::ConfBuilder::new()
.main(main_conf(&mut user))
.events(events_conf)
.stream(stream_conf(&mut user), !user.no_stream)
.http(http_conf(&mut user))
.lua(lua_loader)
.resty_compat_version(resty_compat_version)
.render(&mut file)
.and_then(|_| file.flush());

if let Err(e) = res {
eprintln!("failed writing nginx.conf file: {}", e);
return 2;
}

drop(file);

let ngx = NginxExec {
let ngx = nginx::Exec {
bin: find_nginx_bin(user.nginx_bin).to_str().unwrap().to_string(),
prefix: prefix.root.to_str().unwrap().to_string(),
runner: user.runner,
Expand Down
Loading

0 comments on commit 19d56c6

Please sign in to comment.