2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-02-27 07:00:46 +00:00
|
|
|
use std::path::Path;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-02-18 06:44:14 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-12-12 08:00:42 +00:00
|
|
|
pub struct UserSettings {
|
|
|
|
config: config::Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RepoSettings {
|
2021-09-29 17:08:12 +00:00
|
|
|
_config: config::Config,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 18:58:33 +00:00
|
|
|
const TOO_MUCH_CONFIG_ERROR: &str =
|
|
|
|
"Both `$HOME/.jjconfig` and `$XDG_CONFIG_HOME/jj/config.toml` were found, please remove one.";
|
2022-02-25 23:08:18 +00:00
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
impl UserSettings {
|
|
|
|
pub fn from_config(config: config::Config) -> Self {
|
|
|
|
UserSettings { config }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_user() -> Result<Self, config::ConfigError> {
|
|
|
|
let mut config = config::Config::new();
|
|
|
|
|
2022-02-25 23:08:18 +00:00
|
|
|
let loaded_from_config_dir = match dirs::config_dir() {
|
|
|
|
None => false,
|
|
|
|
Some(config_dir) => {
|
|
|
|
let p = config_dir.join("jj/config.toml");
|
|
|
|
let exists = p.exists();
|
|
|
|
config.merge(
|
|
|
|
config::File::from(p)
|
|
|
|
.required(false)
|
|
|
|
.format(config::FileFormat::Toml),
|
|
|
|
)?;
|
|
|
|
exists
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
if let Some(home_dir) = dirs::home_dir() {
|
2022-02-25 23:08:18 +00:00
|
|
|
let p = home_dir.join(".jjconfig");
|
2022-02-27 18:58:33 +00:00
|
|
|
// we already loaded from the new location, prevent user confusion and make them
|
|
|
|
// remove the old one:
|
2022-02-25 23:08:18 +00:00
|
|
|
if loaded_from_config_dir && p.exists() {
|
2022-02-27 18:58:33 +00:00
|
|
|
return Err(config::ConfigError::Message(
|
|
|
|
TOO_MUCH_CONFIG_ERROR.to_string(),
|
|
|
|
));
|
2022-02-25 23:08:18 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
config.merge(
|
2022-02-25 23:08:18 +00:00
|
|
|
config::File::from(p)
|
2020-12-12 08:00:42 +00:00
|
|
|
.required(false)
|
|
|
|
.format(config::FileFormat::Toml),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(UserSettings { config })
|
|
|
|
}
|
|
|
|
|
2021-02-27 07:00:46 +00:00
|
|
|
pub fn with_repo(&self, repo_path: &Path) -> Result<RepoSettings, config::ConfigError> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut config = self.config.clone();
|
|
|
|
config.merge(
|
|
|
|
config::File::from(repo_path.join("config"))
|
|
|
|
.required(false)
|
|
|
|
.format(config::FileFormat::Toml),
|
|
|
|
)?;
|
|
|
|
|
2021-09-29 17:08:12 +00:00
|
|
|
Ok(RepoSettings { _config: config })
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_name(&self) -> String {
|
2021-05-16 20:45:08 +00:00
|
|
|
self.config
|
|
|
|
.get_str("user.name")
|
2021-05-19 18:52:28 +00:00
|
|
|
.unwrap_or_else(|_| "(no name configured)".to_string())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_email(&self) -> String {
|
|
|
|
self.config
|
|
|
|
.get_str("user.email")
|
2021-05-19 18:52:28 +00:00
|
|
|
.unwrap_or_else(|_| "(no email configured)".to_string())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config(&self) -> &config::Config {
|
|
|
|
&self.config
|
|
|
|
}
|
|
|
|
}
|