diff options
author | Niklas Claesson <nicke.claesson@gmail.com> | 2017-05-29 02:42:36 +0200 |
---|---|---|
committer | Joe Wilm <jwilm@users.noreply.github.com> | 2017-05-28 17:42:36 -0700 |
commit | 81116fb8a4f91f28b5751827e7bcda22f6fcbaf0 (patch) | |
tree | b9b446b5f8ed18c31189eb45f24f02ffe767657f /src/main.rs | |
parent | 13eac6b673a089afe6f15adb73d6812916c73f73 (diff) | |
download | alacritty-81116fb8a4f91f28b5751827e7bcda22f6fcbaf0.tar.gz alacritty-81116fb8a4f91f28b5751827e7bcda22f6fcbaf0.zip |
Add config file as cli option (#576)
* Parse cli arguments before configuration file
Parsing the cli arguments before the configuration file allows `--help`
and `--version` to be used even if the configuration file is broken.
* Add configuration file to command line arguments
This commit adds a new command line flag `--config-file` to override the default
configuration file location. If the specified file is unavailable,
Alacritty will quit instead of generating a fallback. If the specified
file is invalid, i.e. /dev/null, the compiled in defaults will be loaded
instead.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs index b7db4dec..f4ee72b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,28 +37,9 @@ use alacritty::tty::{self, process_should_exit}; use alacritty::util::fmt::Red; fn main() { - - // Load configuration - let config = Config::load().unwrap_or_else(|err| { - match err { - // Use default config when not found - config::Error::NotFound => { - match Config::write_defaults() { - Ok(path) => err_println!("Config file not found; write defaults config to {:?}", path), - Err(err) => err_println!("Write defaults config failure: {}", err) - } - - Config::load().unwrap() - }, - - // If there's a problem with the config file, print an error - // and exit. - _ => die!("{}", err), - } - }); - - // Load command line options + // Load command line options and config let options = cli::Options::load(); + let config = load_config(&options); // Run alacritty if let Err(err) = run(config, options) { @@ -68,6 +49,32 @@ fn main() { info!("Goodbye."); } +/// Load configuration +/// +/// If a configuration file is given as a command line argument we don't +/// generate a default file. If an empty configuration file is given, i.e. +/// /dev/null, we load the compiled-in defaults. +fn load_config(options: &cli::Options) -> Config { + let config_path = options.config_path() + .or_else(|| Config::installed_config()) + .unwrap_or_else(|| { + Config::write_defaults() + .unwrap_or_else(|err| die!("Write defaults config failure: {}", err)) + }); + + Config::load_from(&*config_path).unwrap_or_else(|err| { + match err { + config::Error::NotFound => { + die!("Config file not found at: {}", config_path.display()); + }, + config::Error::Empty => { + err_println!("Empty config; Loading defaults"); + Config::default() + }, + _ => die!("{}", err), + } + }) +} /// Run Alacritty /// |