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.
|
|
|
|
|
2022-03-19 17:09:57 +00:00
|
|
|
use std::env;
|
2022-03-19 17:37:13 +00:00
|
|
|
use std::path::PathBuf;
|
2022-03-19 17:09:57 +00:00
|
|
|
|
2022-05-07 14:04:25 +00:00
|
|
|
use jujutsu::commands::{dispatch, CommandError};
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu::ui::Ui;
|
|
|
|
use jujutsu_lib::settings::UserSettings;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-03-19 17:37:13 +00:00
|
|
|
fn config_path() -> Option<PathBuf> {
|
|
|
|
if let Ok(config_path) = env::var("JJ_CONFIG") {
|
2022-04-09 23:14:42 +00:00
|
|
|
// TODO: We should probably support colon-separated (std::env::split_paths)
|
|
|
|
// paths here
|
2022-03-19 17:37:13 +00:00
|
|
|
Some(PathBuf::from(config_path))
|
|
|
|
} else {
|
2022-04-09 23:14:42 +00:00
|
|
|
// TODO: Should we drop the final `/config.toml` and read all files in the
|
|
|
|
// directory?
|
2022-03-19 17:37:13 +00:00
|
|
|
dirs::config_dir().map(|config_dir| config_dir.join("jj").join("config.toml"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 05:42:27 +00:00
|
|
|
/// Environment variables that should be overridden by config values
|
|
|
|
fn env_base() -> config::Config {
|
|
|
|
let mut builder = config::Config::builder();
|
|
|
|
if let Ok(value) = env::var("EDITOR") {
|
|
|
|
builder = builder.set_override("ui.editor", value).unwrap();
|
|
|
|
}
|
|
|
|
builder.build().unwrap()
|
|
|
|
}
|
|
|
|
|
2022-04-10 02:35:32 +00:00
|
|
|
/// Environment variables that override config values
|
|
|
|
fn env_overrides() -> config::Config {
|
|
|
|
let mut builder = config::Config::builder();
|
|
|
|
if let Ok(value) = env::var("JJ_USER") {
|
|
|
|
builder = builder.set_override("user.name", value).unwrap();
|
|
|
|
}
|
|
|
|
if let Ok(value) = env::var("JJ_EMAIL") {
|
|
|
|
builder = builder.set_override("user.email", value).unwrap();
|
|
|
|
}
|
|
|
|
if let Ok(value) = env::var("JJ_TIMESTAMP") {
|
|
|
|
builder = builder.set_override("user.timestamp", value).unwrap();
|
|
|
|
}
|
2022-04-10 06:20:36 +00:00
|
|
|
if let Ok(value) = env::var("JJ_EDITOR") {
|
|
|
|
builder = builder.set_override("ui.editor", value).unwrap();
|
|
|
|
}
|
2022-04-10 02:35:32 +00:00
|
|
|
builder.build().unwrap()
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:09:57 +00:00
|
|
|
fn read_config() -> Result<UserSettings, config::ConfigError> {
|
2022-04-10 05:42:27 +00:00
|
|
|
let mut config_builder = config::Config::builder().add_source(env_base());
|
2022-03-19 17:09:57 +00:00
|
|
|
|
2022-03-19 17:37:13 +00:00
|
|
|
if let Some(config_path) = config_path() {
|
2022-04-09 23:14:42 +00:00
|
|
|
let mut files = vec![];
|
|
|
|
if config_path.is_dir() {
|
|
|
|
if let Ok(read_dir) = config_path.read_dir() {
|
|
|
|
// TODO: Walk the directory recursively?
|
|
|
|
for dir_entry in read_dir.flatten() {
|
|
|
|
let path = dir_entry.path();
|
|
|
|
if path.is_file() {
|
|
|
|
files.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files.sort();
|
|
|
|
} else {
|
|
|
|
files.push(config_path);
|
|
|
|
}
|
|
|
|
for file in files {
|
|
|
|
// TODO: Accept other formats and/or accept only certain file extensions?
|
|
|
|
config_builder = config_builder.add_source(
|
|
|
|
config::File::from(file)
|
|
|
|
.required(false)
|
|
|
|
.format(config::FileFormat::Toml),
|
|
|
|
);
|
|
|
|
}
|
2022-03-19 17:17:30 +00:00
|
|
|
};
|
2022-03-19 17:09:57 +00:00
|
|
|
|
2022-04-10 02:35:32 +00:00
|
|
|
let config = config_builder.add_source(env_overrides()).build()?;
|
2022-03-19 17:09:57 +00:00
|
|
|
Ok(UserSettings::from_config(config))
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
fn main() {
|
|
|
|
// TODO: We need to do some argument parsing here, at least for things like
|
|
|
|
// --config, and for reading user configs from the repo pointed to by
|
|
|
|
// -R.
|
2022-03-19 17:09:57 +00:00
|
|
|
match read_config() {
|
2022-02-18 06:44:14 +00:00
|
|
|
Ok(user_settings) => {
|
2022-05-07 15:09:26 +00:00
|
|
|
let mut ui = Ui::for_terminal(user_settings);
|
2022-05-07 14:04:25 +00:00
|
|
|
match dispatch(&mut ui, &mut std::env::args_os()) {
|
|
|
|
Ok(_) => {
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
Err(CommandError::UserError(message)) => {
|
|
|
|
ui.write_error(&format!("Error: {}\n", message)).unwrap();
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
Err(CommandError::BrokenPipe) => {
|
|
|
|
std::process::exit(2);
|
|
|
|
}
|
|
|
|
Err(CommandError::InternalError(message)) => {
|
|
|
|
ui.write_error(&format!("Internal error: {}\n", message))
|
|
|
|
.unwrap();
|
|
|
|
std::process::exit(255);
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 06:44:14 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut ui = Ui::for_terminal(UserSettings::default());
|
|
|
|
ui.write_error(&format!("Invalid config: {}\n", err))
|
|
|
|
.unwrap();
|
2022-02-20 07:38:23 +00:00
|
|
|
std::process::exit(1);
|
2022-02-18 06:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|