diff --git a/README.md b/README.md index 9c8c2d8..a94c4df 100644 --- a/README.md +++ b/README.md @@ -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 git@github.com:yukihirop/ultraman.git && cd ultraman make install_man diff --git a/src/cmd/export/base.rs b/src/cmd/export/base.rs index db486be..741737f 100644 --- a/src/cmd/export/base.rs +++ b/src/cmd/export/base.rs @@ -16,13 +16,19 @@ pub struct EnvParameter { pub(crate) value: String, } +pub struct Template { + pub(crate) template_path: PathBuf, + pub(crate) data: Map, + pub(crate) output_path: PathBuf, +} + pub trait Exportable { fn export(&self) -> Result<(), Box>; //https://yajamon.hatenablog.com/entry/2018/01/30/202849 - fn opts(&self) -> ExportOpts; + fn ref_opts(&self) -> &ExportOpts; fn base_export(&self) -> Result<(), Box> { - 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)); @@ -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()) } @@ -83,36 +95,38 @@ pub trait Exportable { println!("[ultraman export] {}", msg) } - fn write_template( - &self, - template_path: &PathBuf, - data: &mut Map, - 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 { - 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 = vec![]; for (key, value) in env { diff --git a/src/cmd/export/daemon.rs b/src/cmd/export/daemon.rs index 611143a..857ce11 100644 --- a/src/cmd/export/daemon.rs +++ b/src/cmd/export/daemon.rs @@ -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; @@ -12,20 +12,7 @@ use std::path::PathBuf; pub struct Exporter { pub procfile: Procfile, - // ExportOpts - pub format: String, - pub location: PathBuf, - pub app: Option, - pub formation: String, - pub log_path: Option, - pub run_path: Option, - pub port: Option, - pub template_path: Option, - pub user: Option, - pub env_path: PathBuf, - pub procfile_path: PathBuf, - pub root_path: Option, - pub timeout: String, + pub opts: ExportOpts, } #[derive(Serialize)] @@ -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"), + }, } } } @@ -168,8 +157,13 @@ impl Exporter { } fn environment(&self, index: usize, con_index: usize) -> Vec { - 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![]; @@ -188,51 +182,60 @@ impl Exportable for Exporter { fn export(&self) -> Result<(), Box> { 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 = vec![]; + let mut tmpl_data: Vec