Add cli option --config-file (#459)

* Add cli option --config-file
* Update cli arg doc & make it conflict with skipping config
* When the config is given explicitly (either using --config-file or via WEZTERM_CONFIG_FILE), failing to load this file will use the default config.
* Otherwise the config file is searched one by one in a few directories.
This commit is contained in:
Benoit de Chezelles authored and GitHub committed 2021-02-07 08:47:33 -08:00
1 parent 70cb992924
commit c576b9d534
4 files changed
+79 -9

No files matched your search

+39 -6
View File
@@ -59,6 +59,7 @@ lazy_static! {
pub static ref CONFIG_DIR: PathBuf = xdg_config_home();
pub static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap();
static ref CONFIG: Configuration = Configuration::new();
static ref CONFIG_FILE_OVERRIDE: Mutex<Option<PathBuf>> = Mutex::new(None);
static ref MAKE_LUA: Mutex<Option<LuaFactory>> = Mutex::new(Some(lua::make_lua_context));
static ref SHOW_ERROR: Mutex<Option<ErrorCallback>> =
Mutex::new(Some(|e| log::error!("{}", e)));
@@ -239,6 +240,13 @@ fn xdg_config_home() -> PathBuf {
}
}
pub fn set_config_file_override(path: &Path) {
CONFIG_FILE_OVERRIDE
.lock()
.unwrap()
.replace(path.to_path_buf());
}
/// Discard the current configuration and replace it with
/// the default configuration
#[allow(dead_code)]
@@ -966,6 +974,25 @@ pub struct LoadedConfig {
lua: Option<mlua::Lua>,
}
struct PathPossibility {
path: PathBuf,
is_required: bool,
}
impl PathPossibility {
pub fn required(path: PathBuf) -> PathPossibility {
PathPossibility {
path,
is_required: true,
}
}
pub fn optional(path: PathBuf) -> PathPossibility {
PathPossibility {
path,
is_required: false,
}
}
}
impl Config {
pub fn load() -> Result<LoadedConfig, Error> {
// Note that the directories crate has methods for locating project
@@ -974,8 +1001,8 @@ impl Config {
// so we do this bit "by-hand"
let mut paths = vec![
CONFIG_DIR.join("wezterm.lua"),
HOME_DIR.join(".wezterm.lua"),
PathPossibility::optional(CONFIG_DIR.join("wezterm.lua")),
PathPossibility::optional(HOME_DIR.join(".wezterm.lua")),
];
if cfg!(windows) {
// On Windows, a common use case is to maintain a thumb drive
@@ -988,21 +1015,27 @@ impl Config {
// dir as the executable that will take precedence.
if let Ok(exe_name) = std::env::current_exe() {
if let Some(exe_dir) = exe_name.parent() {
paths.insert(0, exe_dir.join("wezterm.lua"));
paths.insert(0, PathPossibility::optional(exe_dir.join("wezterm.lua")));
}
}
}
if let Some(path) = std::env::var_os("WEZTERM_CONFIG_FILE") {
log::trace!("Note: WEZTERM_CONFIG_FILE is set in the environment");
paths.insert(0, path.into());
paths.insert(0, PathPossibility::required(path.into()));
}
for p in &paths {
if let Some(path) = CONFIG_FILE_OVERRIDE.lock().unwrap().as_ref() {
log::trace!("Note: config file override is set");
paths.insert(0, PathPossibility::required(path.clone()));
}
for path_item in &paths {
let p = path_item.path.as_path();
log::trace!("consider config: {}", p.display());
let mut file = match fs::File::open(p) {
Ok(file) => file,
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => continue,
std::io::ErrorKind::NotFound if !path_item.is_required => continue,
_ => bail!("Error opening {}: {}", p.display(), err),
},
};
+14 -1
View File
@@ -8,6 +8,7 @@ use mux::domain::{Domain, LocalDomain};
use mux::Mux;
use portable_pty::cmdbuilder::CommandBuilder;
use promise::spawn::block_on;
use std::ffi::OsString;
use std::rc::Rc;
use std::sync::Arc;
use structopt::StructOpt;
@@ -30,9 +31,18 @@ mod window_config;
)]
struct Opt {
/// Skip loading wezterm.lua
#[structopt(short = "n")]
#[structopt(name = "skip-config", short = "n")]
skip_config: bool,
/// Specify the configuration file to use, overrides the normal
/// configuration file resolution
#[structopt(
long = "config-file",
parse(from_os_str),
conflicts_with = "skip-config"
)]
config_file: Option<OsString>,
#[structopt(subcommand)]
cmd: Option<SubCommand>,
}
@@ -408,6 +418,9 @@ fn run() -> anyhow::Result<()> {
let _saver = umask::UmaskSaver::new();
let opts = Opt::from_args();
if let Some(config_file) = opts.config_file.as_ref() {
config::set_config_file_override(std::path::Path::new(config_file));
}
if !opts.skip_config {
config::reload();
}
+13 -1
View File
@@ -20,9 +20,18 @@ mod daemonize;
)]
struct Opt {
/// Skip loading wezterm.lua
#[structopt(short = "n")]
#[structopt(name = "skip-config", short = "n")]
skip_config: bool,
/// Specify the configuration file to use, overrides the normal
/// configuration file resolution
#[structopt(
long = "config-file",
parse(from_os_str),
conflicts_with = "skip-config"
)]
config_file: Option<OsString>,
/// Detach from the foreground and become a background process
#[structopt(long = "daemonize")]
daemonize: bool,
@@ -54,6 +63,9 @@ fn run() -> anyhow::Result<()> {
let _saver = umask::UmaskSaver::new();
let opts = Opt::from_args();
if let Some(config_file) = opts.config_file.as_ref() {
config::set_config_file_override(std::path::Path::new(config_file));
}
if !opts.skip_config {
config::reload();
}
+13 -1
View File
@@ -26,9 +26,18 @@ use wezterm_gui_subcommands::*;
)]
struct Opt {
/// Skip loading wezterm.lua
#[structopt(short = "n")]
#[structopt(name = "skip-config", short = "n")]
skip_config: bool,
/// Specify the configuration file to use, overrides the normal
/// configuration file resolution
#[structopt(
long = "config-file",
parse(from_os_str),
conflicts_with = "skip-config"
)]
config_file: Option<OsString>,
#[structopt(subcommand)]
cmd: Option<SubCommand>,
}
@@ -227,6 +236,9 @@ fn run() -> anyhow::Result<()> {
let saver = UmaskSaver::new();
let opts = Opt::from_args();
if let Some(config_file) = opts.config_file.as_ref() {
config::set_config_file_override(std::path::Path::new(config_file));
}
if !opts.skip_config {
config::reload();
}