Add socket_dir cli option

This commit is contained in:
Ottatop 2024-05-07 19:37:29 -05:00
parent b5707daa86
commit 6802afe04a
2 changed files with 17 additions and 10 deletions

View file

@ -53,13 +53,16 @@ pub struct Cli {
#[arg(long)] #[arg(long)]
pub no_xwayland: bool, pub no_xwayland: bool,
/// Open the gRPC socket at the specified directory
#[arg(short, long, value_name("DIR"), value_hint(ValueHint::DirPath))]
pub socket_dir: Option<PathBuf>,
/// Cli subcommands /// Cli subcommands
#[command(subcommand)] #[command(subcommand)]
subcommand: Option<CliSubcommand>, subcommand: Option<CliSubcommand>,
} }
impl Cli { impl Cli {
//
pub fn parse_and_prompt() -> Option<Self> { pub fn parse_and_prompt() -> Option<Self> {
let mut cli = Cli::parse(); let mut cli = Cli::parse();

View file

@ -70,7 +70,7 @@ pub struct Metaconfig {
pub envs: Option<Table>, pub envs: Option<Table>,
pub reload_keybind: Option<Keybind>, pub reload_keybind: Option<Keybind>,
pub kill_keybind: Option<Keybind>, pub kill_keybind: Option<Keybind>,
pub socket_dir: Option<String>, pub socket_dir: Option<PathBuf>,
pub no_config: Option<bool>, pub no_config: Option<bool>,
pub no_xwayland: Option<bool>, pub no_xwayland: Option<bool>,
} }
@ -97,15 +97,18 @@ impl Metaconfig {
let default: Metaconfig = let default: Metaconfig =
toml::from_str(builtin::METACONFIG).expect("default metaconfig should be error-free"); toml::from_str(builtin::METACONFIG).expect("default metaconfig should be error-free");
let socket_dir = if let Some(socket_dir) = &self.socket_dir { let socket_dir = if let Some(socket_dir) = cli
let socket_dir = shellexpand::full(socket_dir)?.to_string(); .and_then(|cli| cli.socket_dir.as_ref())
.or(self.socket_dir.as_ref())
{
let socket_dir = shellexpand::path::full(socket_dir)?.to_path_buf();
// cd into the metaconfig dir and canonicalize to preserve relative paths // cd into the metaconfig dir and canonicalize to preserve relative paths
// like ./dir/here // like ./dir/here
let current_dir = std::env::current_dir()?; let current_dir = std::env::current_dir()?;
std::env::set_current_dir(config_dir)?; std::env::set_current_dir(config_dir)?;
let socket_dir = PathBuf::from(socket_dir).canonicalize()?; let socket_dir = socket_dir.canonicalize()?;
std::env::set_current_dir(current_dir)?; std::env::set_current_dir(current_dir)?;
socket_dir socket_dir
} else { } else {
@ -747,6 +750,9 @@ mod tests {
socket_dir = "/path/to/socket/dir" socket_dir = "/path/to/socket/dir"
no_config = true
no_xwayland = true
[envs] [envs]
MARCO = "polo" MARCO = "polo"
SUN = "chips" SUN = "chips"
@ -772,9 +778,9 @@ mod tests {
modifiers: vec![Modifier::Ctrl, Modifier::Alt, Modifier::Shift], modifiers: vec![Modifier::Ctrl, Modifier::Alt, Modifier::Shift],
key: Key::Escape, key: Key::Escape,
}), }),
socket_dir: Some("/path/to/socket/dir".to_string()), socket_dir: Some("/path/to/socket/dir".into()),
no_config: None, no_config: Some(true),
no_xwayland: None, no_xwayland: Some(true),
}; };
assert_eq!( assert_eq!(
@ -830,8 +836,6 @@ mod tests {
command = "lua" # not an array command = "lua" # not an array
reload_keybind = { modifiers = ["Ctrl", "Alt"], key = "r" } reload_keybind = { modifiers = ["Ctrl", "Alt"], key = "r" }
# Missing `kill_keybind`
# kill_keybind = { modifiers = ["Ctrl", "Alt", "Shift"], key = "escape" }
"#; "#;
let metaconfig_dir = tempfile::tempdir()?; let metaconfig_dir = tempfile::tempdir()?;