Skip to content

Commit

Permalink
Simplify deserialization for layouts, config
Browse files Browse the repository at this point in the history
Simplify deserialzation for layouts, config and config options.

Move the logic responsible to `Setup::from_options()` in order
to be able to parse `main.rs` as well as adding new command easier.
  • Loading branch information
a-kenji committed Jul 28, 2021
1 parent a62dea4 commit 8ebed72
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
30 changes: 4 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 3 additions & 12 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,16 @@ impl Layout {
layout: Option<&PathBuf>,
layout_path: Option<&PathBuf>,
layout_dir: Option<PathBuf>,
) -> Option<Layout> {
let layout_result = layout
) -> Option<Result<Layout, ConfigError>> {
layout
.map(|p| Layout::from_dir(p, layout_dir.as_ref()))
.or_else(|| layout_path.map(|p| Layout::new(p)))
.or_else(|| {
Some(Layout::from_dir(
&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
Expand Down
68 changes: 65 additions & 3 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<Layout>, 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(());
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 8ebed72

Please sign in to comment.