feat(core): introduce command line argument and env variables to customize config path. #267
This commit is contained in:
parent
f252c6a119
commit
47587681bd
|
@ -20,17 +20,20 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
use clap::{App, AppSettings, Arg, SubCommand};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||||
use cli::{CliModule, CliModuleArgs};
|
use cli::{CliModule, CliModuleArgs};
|
||||||
|
use log::{error, info};
|
||||||
use logging::FileProxy;
|
use logging::FileProxy;
|
||||||
use simplelog::{
|
use simplelog::{
|
||||||
CombinedLogger, ConfigBuilder, LevelFilter, TermLogger, TerminalMode, WriteLogger,
|
CombinedLogger, ConfigBuilder, LevelFilter, TermLogger, TerminalMode, WriteLogger,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod util;
|
|
||||||
mod engine;
|
mod engine;
|
||||||
mod logging;
|
mod logging;
|
||||||
|
mod util;
|
||||||
|
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const LOG_FILE_NAME: &str = "espanso.log";
|
const LOG_FILE_NAME: &str = "espanso.log";
|
||||||
|
@ -82,6 +85,18 @@ fn main() {
|
||||||
.multiple(true)
|
.multiple(true)
|
||||||
.help("Sets the level of verbosity"),
|
.help("Sets the level of verbosity"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("config_dir")
|
||||||
|
.long("config_dir")
|
||||||
|
.takes_value(true)
|
||||||
|
.help("Specify a custom path from which espanso should read the configuration"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("runtime_dir")
|
||||||
|
.long("runtime_dir")
|
||||||
|
.takes_value(true)
|
||||||
|
.help("Specify a custom path for the espanso runtime directory"),
|
||||||
|
)
|
||||||
// .subcommand(SubCommand::with_name("cmd")
|
// .subcommand(SubCommand::with_name("cmd")
|
||||||
// .about("Send a command to the espanso daemon.")
|
// .about("Send a command to the espanso daemon.")
|
||||||
// .subcommand(SubCommand::with_name("exit")
|
// .subcommand(SubCommand::with_name("exit")
|
||||||
|
@ -239,8 +254,14 @@ fn main() {
|
||||||
let mut cli_args: CliModuleArgs = CliModuleArgs::default();
|
let mut cli_args: CliModuleArgs = CliModuleArgs::default();
|
||||||
|
|
||||||
if handler.requires_paths || handler.requires_config {
|
if handler.requires_paths || handler.requires_config {
|
||||||
// TODO: here take into account env variable and/or command line flag
|
let force_config_path = get_path_override(&matches, "config_dir", "ESPANSO_CONFIG_DIR");
|
||||||
let paths = espanso_path::resolve_paths();
|
let force_runtime_path = get_path_override(&matches, "runtime_dir", "ESPANSO_RUNTIME_DIR");
|
||||||
|
|
||||||
|
let paths = espanso_path::resolve_paths(force_config_path.as_deref(), force_runtime_path.as_deref());
|
||||||
|
|
||||||
|
info!("reading configs from: {:?}", paths.config);
|
||||||
|
info!("reading packages from: {:?}", paths.packages);
|
||||||
|
info!("using runtime dir: {:?}", paths.runtime);
|
||||||
|
|
||||||
if handler.requires_config {
|
if handler.requires_config {
|
||||||
let (config_store, match_store, is_legacy_config) =
|
let (config_store, match_store, is_legacy_config) =
|
||||||
|
@ -281,3 +302,25 @@ fn main() {
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_path_override(matches: &ArgMatches, argument: &str, env_var: &str) -> Option<PathBuf> {
|
||||||
|
if let Some(path) = matches.value_of(argument) {
|
||||||
|
let path = PathBuf::from(path.trim());
|
||||||
|
if path.is_dir() {
|
||||||
|
return Some(path)
|
||||||
|
} else {
|
||||||
|
error!("{} argument was specified, but it doesn't point to a valid directory. Make sure to create it first.", argument);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
} else if let Ok(path) = std::env::var(env_var) {
|
||||||
|
let path = PathBuf::from(path.trim());
|
||||||
|
if path.is_dir() {
|
||||||
|
return Some(path)
|
||||||
|
} else {
|
||||||
|
error!("{} env variable was specified, but it doesn't point to a valid directory. Make sure to create it first.", env_var);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user