Skip to content

Commit

Permalink
Merge pull request #34 from yukihirop/issue6/refactor_all
Browse files Browse the repository at this point in the history
Refactor All
  • Loading branch information
yukihirop authored Dec 25, 2020
2 parents 7206314 + 154b248 commit 037bbae
Show file tree
Hide file tree
Showing 17 changed files with 623 additions and 650 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Download from [release page](https://github.com/yukihirop/ultraman/releases), an

If you want to install the `man`,

Suppose you unzip the archive in the `./tmp` directory

```bash
install -Dm644 ./tmp/ultraman.1 /usr/local/share/man/man1/ultraman.1
```

or

```bash
git clone [email protected]:yukihirop/ultraman.git && cd ultraman
make install_man
Expand Down
56 changes: 35 additions & 21 deletions src/cmd/export/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ pub struct EnvParameter {
pub(crate) value: String,
}

pub struct Template {
pub(crate) template_path: PathBuf,
pub(crate) data: Map<String, Json>,
pub(crate) output_path: PathBuf,
}

pub trait Exportable {
fn export(&self) -> Result<(), Box<dyn std::error::Error>>;
//https://yajamon.hatenablog.com/entry/2018/01/30/202849
fn opts(&self) -> ExportOpts;
fn ref_opts(&self) -> &ExportOpts;

fn base_export(&self) -> Result<(), Box<dyn std::error::Error>> {
let opts = self.opts();
let opts = self.ref_opts();
let location = &opts.location;
let display = location.clone().into_os_string().into_string().unwrap();
create_dir_all(&location).expect(&format!("Could not create: {}", display));
Expand All @@ -33,28 +39,34 @@ pub trait Exportable {
}

fn app(&self) -> String {
self.opts().app.unwrap_or_else(|| "app".to_string())
self.ref_opts()
.app
.clone()
.unwrap_or_else(|| "app".to_string())
}

fn log_path(&self) -> PathBuf {
self.opts()
self.ref_opts()
.log_path
.clone()
.unwrap_or_else(|| PathBuf::from(format!("/var/log/{}", self.app())))
}

fn run_path(&self) -> PathBuf {
self.opts()
self.ref_opts()
.run_path
.clone()
.unwrap_or_else(|| PathBuf::from(format!("/var/run/{}", self.app())))
}

fn username(&self) -> String {
self.opts().user.unwrap_or_else(|| self.app())
self.ref_opts().user.clone().unwrap_or_else(|| self.app())
}

fn root_path(&self) -> PathBuf {
self.opts()
self.ref_opts()
.root_path
.clone()
.unwrap_or_else(|| env::current_dir().unwrap())
}

Expand Down Expand Up @@ -83,36 +95,38 @@ pub trait Exportable {
println!("[ultraman export] {}", msg)
}

fn write_template(
&self,
template_path: &PathBuf,
data: &mut Map<String, Json>,
output_path: &PathBuf,
) {
fn write_template(&self, tmpl: Template) {
let handlebars = Handlebars::new();
let display_template = template_path
let display_template = tmpl
.template_path
.clone()
.into_os_string()
.into_string()
.unwrap();
let display_output = tmpl
.output_path
.clone()
.into_os_string()
.into_string()
.unwrap();
let display_output = output_path.clone().into_os_string().into_string().unwrap();
let mut template_source =
File::open(template_path).expect(&format!("Could not open file: {}", display_template));
let mut output_file = File::create(output_path)
let mut template_source = File::open(tmpl.template_path)
.expect(&format!("Could not open file: {}", display_template));
let mut output_file = File::create(tmpl.output_path)
.expect(&format!("Could not create file: {}", &display_output));
self.say(&format!("writing: {}", &display_output));
let mut data = tmpl.data;
handlebars
.render_template_source_to_write(&mut template_source, data, &mut output_file)
.render_template_source_to_write(&mut template_source, &mut data, &mut output_file)
.expect(&format!("Coult not render file: {}", &display_output));
}

fn output_path(&self, filename: String) -> PathBuf {
let location = self.opts().location;
let location = self.ref_opts().location.clone();
location.join(filename)
}

fn env_without_port(&self) -> Vec<EnvParameter> {
let mut env = read_env(self.opts().env_path).expect("failed read .env");
let mut env = read_env(self.ref_opts().env_path.clone()).expect("failed read .env");
env.remove("PORT");
let mut env_without_port: Vec<EnvParameter> = vec![];
for (key, value) in env {
Expand Down
119 changes: 61 additions & 58 deletions src/cmd/export/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::base::{EnvParameter, Exportable};
use super::base::{EnvParameter, Exportable, Template};
use crate::cmd::export::ExportOpts;
use crate::env::read_env;
use crate::process::port_for;
Expand All @@ -12,20 +12,7 @@ use std::path::PathBuf;

pub struct Exporter {
pub procfile: Procfile,
// ExportOpts
pub format: String,
pub location: PathBuf,
pub app: Option<String>,
pub formation: String,
pub log_path: Option<PathBuf>,
pub run_path: Option<PathBuf>,
pub port: Option<String>,
pub template_path: Option<PathBuf>,
pub user: Option<String>,
pub env_path: PathBuf,
pub procfile_path: PathBuf,
pub root_path: Option<PathBuf>,
pub timeout: String,
pub opts: ExportOpts,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -58,19 +45,21 @@ impl Default for Exporter {
procfile: Procfile {
data: HashMap::new(),
},
format: String::from(""),
location: PathBuf::from("location"),
app: None,
formation: String::from("all=1"),
log_path: None,
run_path: None,
port: None,
template_path: None,
user: None,
env_path: PathBuf::from(".env"),
procfile_path: PathBuf::from("Procfile"),
root_path: Some(env::current_dir().unwrap()),
timeout: String::from("5"),
opts: ExportOpts {
format: String::from(""),
location: PathBuf::from("location"),
app: None,
formation: String::from("all=1"),
log_path: None,
run_path: None,
port: None,
template_path: None,
user: None,
env_path: PathBuf::from(".env"),
procfile_path: PathBuf::from("Procfile"),
root_path: Some(env::current_dir().unwrap()),
timeout: String::from("5"),
},
}
}
}
Expand Down Expand Up @@ -168,8 +157,13 @@ impl Exporter {
}

fn environment(&self, index: usize, con_index: usize) -> Vec<EnvParameter> {
let port = port_for(self.opts().env_path, self.opts().port, index, con_index + 1);
let mut env = read_env(self.opts().env_path).expect("failed read .env");
let port = port_for(
self.opts.env_path.clone(),
self.opts.port.clone(),
index,
con_index + 1,
);
let mut env = read_env(self.opts.env_path.clone()).expect("failed read .env");
env.insert("PORT".to_string(), port);

let mut result = vec![];
Expand All @@ -188,51 +182,60 @@ impl Exportable for Exporter {
fn export(&self) -> Result<(), Box<dyn std::error::Error>> {
self.base_export().expect("failed execute base_export");

let mut data = self.make_master_data();
let output_path = self.opts().location.join(format!("{}.conf", self.app()));
self.clean(&output_path);
self.write_template(&self.master_tmpl_path(), &mut data, &output_path);
let mut clean_paths: Vec<PathBuf> = vec![];
let mut tmpl_data: Vec<Template> = vec![];

let output_path = self.opts.location.join(format!("{}.conf", self.app()));

clean_paths.push(output_path.clone());
tmpl_data.push(Template {
template_path: self.master_tmpl_path(),
data: self.make_master_data(),
output_path,
});

let mut index = 0;
for (name, pe) in self.procfile.data.iter() {
let con = pe.concurrency.get();
let service_name = format!("{}-{}", self.app(), &name);
let output_path = self
.opts()
.opts
.location
.join(format!("{}-{}.conf", self.app(), &name));
let mut data = self.make_process_master_data();
self.clean(&output_path);
self.write_template(&self.process_master_tmpl_path(), &mut data, &output_path);

clean_paths.push(output_path.clone());
tmpl_data.push(Template {
template_path: self.process_master_tmpl_path(),
data: self.make_process_master_data(),
output_path,
});

for n in 0..con {
index += 1;
let process_name = format!("{}-{}-{}.conf", self.app(), &name, n + 1);
let output_path = self.opts().location.join(&process_name);
let mut data = self.make_process_data(pe, &service_name, index, n);
self.clean(&output_path);
self.write_template(&self.process_tmpl_path(), &mut data, &output_path);
let output_path = self.opts.location.join(&process_name);

clean_paths.push(output_path.clone());
tmpl_data.push(Template {
template_path: self.process_tmpl_path(),
data: self.make_process_data(pe, &service_name, index, n),
output_path,
});
}
}

for path in clean_paths {
self.clean(&path);
}

for tmpl in tmpl_data {
self.write_template(tmpl);
}

Ok(())
}

fn opts(&self) -> ExportOpts {
ExportOpts {
format: self.format.clone(),
location: self.location.clone(),
app: self.app.clone(),
formation: self.formation.clone(),
log_path: self.log_path.clone(),
run_path: self.run_path.clone(),
port: self.port.clone(),
template_path: self.template_path.clone(),
user: self.user.clone(),
env_path: self.env_path.clone(),
procfile_path: self.procfile_path.clone(),
root_path: self.root_path.clone(),
timeout: self.timeout.clone(),
}
fn ref_opts(&self) -> &ExportOpts {
&self.opts
}
}
Loading

0 comments on commit 037bbae

Please sign in to comment.