diff --git a/config/src/lib.rs b/config/src/lib.rs index 366fe3ea0..4c73f8555 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -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> = Mutex::new(None); static ref MAKE_LUA: Mutex> = Mutex::new(Some(lua::make_lua_context)); static ref SHOW_ERROR: Mutex> = 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, } +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 { // 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), }, }; diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 2c073399a..38af692df 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -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, + #[structopt(subcommand)] cmd: Option, } @@ -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(); } diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rs index a7e48c178..e866168db 100644 --- a/wezterm-mux-server/src/main.rs +++ b/wezterm-mux-server/src/main.rs @@ -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, + /// 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(); } diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index ba0fa1da4..d6bc86dbe 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -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, + #[structopt(subcommand)] cmd: Option, } @@ -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(); }