Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for fallback configuration directories #394

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clipcat-menu/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ impl Cli {
_ => {}
}

let mut config = Config::load_or_default(config_file.unwrap_or_else(Config::default_path));
let mut config =
Config::load_or_default(config_file.unwrap_or_else(Config::search_config_file_path));
if let Some(log_level) = log_level {
config.log.level = log_level;
}
Expand Down
21 changes: 21 additions & 0 deletions clipcat-menu/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ pub struct Config {
}

impl Config {
pub fn search_config_file_path() -> PathBuf {
let paths = vec![Self::default_path()]
.into_iter()
.chain(clipcat_base::fallback_project_config_directories().into_iter().map(
|mut path| {
path.push(clipcat_base::MENU_CONFIG_NAME);
path
},
))
.collect::<Vec<_>>();
for path in paths {
let Ok(exists) = path.try_exists() else {
continue;
};
if exists {
return path;
}
}
Self::default_path()
}

#[inline]
pub fn default_path() -> PathBuf {
[
Expand Down
5 changes: 3 additions & 2 deletions clipcatctl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ impl Default for Cli {

impl Cli {
fn load_config(&self) -> Config {
let mut config =
Config::load_or_default(self.config_file.clone().unwrap_or_else(Config::default_path));
let mut config = Config::load_or_default(
self.config_file.clone().unwrap_or_else(Config::search_config_file_path),
);
if let Some(endpoint) = &self.server_endpoint {
config.server_endpoint = endpoint.clone();
}
Expand Down
21 changes: 21 additions & 0 deletions clipcatctl/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ impl Default for Config {
}

impl Config {
pub fn search_config_file_path() -> PathBuf {
let paths = vec![Self::default_path()]
.into_iter()
.chain(clipcat_base::fallback_project_config_directories().into_iter().map(
|mut path| {
path.push(clipcat_base::CTL_CONFIG_NAME);
path
},
))
.collect::<Vec<_>>();
for path in paths {
let Ok(exists) = path.try_exists() else {
continue;
};
if exists {
return path;
}
}
Self::default_path()
}

#[inline]
pub fn default_path() -> PathBuf {
[
Expand Down
2 changes: 1 addition & 1 deletion clipcatd/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Cli {
}

fn load_config(&self) -> Result<Config, Error> {
let config_file = &self.config_file.clone().unwrap_or_else(Config::default_path);
let config_file = &self.config_file.clone().unwrap_or_else(Config::search_config_file_path);
let mut config = Config::load(config_file)?;

config.daemonize = !self.no_daemon;
Expand Down
21 changes: 21 additions & 0 deletions clipcatd/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ impl Default for Config {
}

impl Config {
pub fn search_config_file_path() -> PathBuf {
let paths = vec![Self::default_path()]
.into_iter()
.chain(clipcat_base::fallback_project_config_directories().into_iter().map(
|mut path| {
path.push(clipcat_base::DAEMON_CONFIG_NAME);
path
},
))
.collect::<Vec<_>>();
for path in paths {
let Ok(exists) = path.try_exists() else {
continue;
};
if exists {
return path;
}
}
Self::default_path()
}

#[inline]
pub fn default_path() -> PathBuf {
[
Expand Down
13 changes: 12 additions & 1 deletion crates/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
net::{IpAddr, Ipv4Addr},
path::PathBuf,
path::{Path, PathBuf},
};

use bytes::Bytes;
Expand Down Expand Up @@ -77,6 +77,17 @@ pub static ref PROJECT_CONFIG_DIR: PathBuf = ProjectDirs::from("", PROJECT_NAME,
.to_path_buf();
}

#[must_use]
pub fn fallback_project_config_directories() -> Vec<PathBuf> {
let Some(user_dirs) = directories::UserDirs::new() else {
return Vec::new();
};
vec![
[user_dirs.home_dir(), &Path::new(".config"), &Path::new(PROJECT_NAME)].iter().collect(),
[user_dirs.home_dir(), &Path::new(&format!(".{PROJECT_NAME}"))].iter().collect(),
]
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum ClipboardContent {
Plaintext(String),
Expand Down
Loading