diff --git a/src/main.rs b/src/main.rs index 22f373a2c6..1a3a16ed67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,14 @@ mod tests; use crate::install::populate_data_dir; use sessions::{assert_session, assert_session_ne, get_active_session, list_sessions}; -use std::convert::TryFrom; use std::process; use zellij_client::{os_input_output::get_client_os_input, start_client, ClientInfo}; use zellij_server::{os_input_output::get_server_os_input, start_server}; use zellij_utils::{ cli::{CliArgs, Command, Sessions}, consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR}, - input::{config::Config, layout::Layout, options::Options}, logging::*, - setup::{find_default_config_dir, get_default_data_dir, get_layout_dir, Setup}, + setup::{get_default_data_dir, Setup}, structopt::StructOpt, }; @@ -26,24 +24,13 @@ pub fn main() { list_sessions(); } - let config = match Config::try_from(&opts) { - Ok(config) => config, + let (config, layout, config_options) = match Setup::from_options(&opts) { + Ok(results) => results, Err(e) => { - eprintln!("There was an error in the config file:\n{}", e); + eprintln!("{}", e); process::exit(1); } }; - let config_options = Options::from_cli(&config.options, opts.command.clone()); - - if let Some(Command::Setup(ref setup)) = opts.command { - Setup::from_cli(setup, &opts, &config_options).map_or_else( - |e| { - eprintln!("{:?}", e); - process::exit(1); - }, - |_| {}, - ); - }; atomic_create_dir(&*ZELLIJ_TMP_DIR).unwrap(); atomic_create_dir(&*ZELLIJ_TMP_LOG_DIR).unwrap(); @@ -94,15 +81,6 @@ pub fn main() { #[cfg(not(disable_automatic_asset_installation))] populate_data_dir(&data_dir); - let layout_dir = config_options.layout_dir.or_else(|| { - get_layout_dir(opts.config_dir.clone().or_else(find_default_config_dir)) - }); - let layout = Layout::from_path_or_default( - opts.layout.as_ref(), - opts.layout_path.as_ref(), - layout_dir, - ); - start_client( Box::new(os_input), opts, diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index a48c352194..090ccb13fc 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -80,8 +80,8 @@ impl Layout { layout: Option<&PathBuf>, layout_path: Option<&PathBuf>, layout_dir: Option, - ) -> Option { - let layout_result = layout + ) -> Option> { + layout .map(|p| Layout::from_dir(p, layout_dir.as_ref())) .or_else(|| layout_path.map(|p| Layout::new(p))) .or_else(|| { @@ -89,16 +89,7 @@ impl Layout { &std::path::PathBuf::from("default"), layout_dir.as_ref(), )) - }); - - match layout_result { - None => None, - Some(Ok(layout)) => Some(layout), - Some(Err(e)) => { - eprintln!("There was an error in the layout file:\n{}", e); - std::process::exit(1); - } - } + }) } // Currently still needed but on nightly diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index aa0bb642b4..d498c6d77e 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -1,11 +1,17 @@ -use crate::cli::CliArgs; use crate::consts::{ FEATURES, SYSTEM_DEFAULT_CONFIG_DIR, SYSTEM_DEFAULT_DATA_DIR_PREFIX, VERSION, ZELLIJ_PROJ_DIR, }; use crate::input::options::Options; +use crate::{ + cli::{CliArgs, Command}, + input::{ + config::{Config, ConfigError}, + layout::Layout, + }, +}; use directories_next::BaseDirs; use serde::{Deserialize, Serialize}; -use std::{io::Write, path::Path, path::PathBuf}; +use std::{convert::TryFrom, io::Write, path::Path, path::PathBuf, process}; use structopt::StructOpt; const CONFIG_LOCATION: &str = ".config/zellij"; @@ -139,6 +145,63 @@ pub struct Setup { impl Setup { /// Entrypoint from main + /// Merges options from the config file and the command line options + /// into `[Options]`, the command line options superceding the config + /// file options: + /// 1. command line options (`zellij options`) + /// 2. config options (`config.yaml`) + pub fn from_options(opts: &CliArgs) -> Result<(Config, Option, Options), ConfigError> { + let clean = match &opts.command { + Some(Command::Setup(ref setup)) => setup.clean, + _ => false, + }; + + let config = if clean { + match Config::try_from(opts) { + Ok(config) => config, + Err(e) => { + eprintln!("There was an error in the config file:"); + return Err(e); + } + } + } else { + Config::default() + }; + + let config_options = Options::from_cli(&config.options, opts.command.clone()); + + let layout_dir = config_options + .layout_dir + .clone() + .or_else(|| get_layout_dir(opts.config_dir.clone().or_else(find_default_config_dir))); + let layout_result = crate::input::layout::Layout::from_path_or_default( + opts.layout.as_ref(), + opts.layout_path.as_ref(), + layout_dir, + ); + let layout = match layout_result { + None => None, + Some(Ok(layout)) => Some(layout), + Some(Err(e)) => { + eprintln!("There was an error in the layout file:"); + return Err(e); + } + }; + + if let Some(Command::Setup(ref setup)) = &opts.command { + setup.from_cli(opts, &config_options).map_or_else( + |e| { + eprintln!("{:?}", e); + process::exit(1); + }, + |_| {}, + ); + }; + + Ok((config, layout, config_options)) + } + + /// General setup helpers pub fn from_cli(&self, opts: &CliArgs, config_options: &Options) -> std::io::Result<()> { if self.clean { return Ok(()); @@ -201,7 +264,6 @@ impl Setup { } } if let Some(config_file) = config_file { - use crate::input::config::Config; message.push_str(&format!("[CONFIG FILE]: {:?}\n", config_file)); match Config::new(&config_file) { Ok(_) => message.push_str("[CONFIG FILE]: Well defined.\n"),