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
|
|
|
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu::commands::dispatch;
|
|
|
|
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") {
|
|
|
|
Some(PathBuf::from(config_path))
|
|
|
|
} else {
|
|
|
|
dirs::config_dir().map(|config_dir| config_dir.join("jj").join("config.toml"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:09:57 +00:00
|
|
|
fn read_config() -> Result<UserSettings, config::ConfigError> {
|
|
|
|
let mut config_builder = config::Config::builder();
|
|
|
|
|
2022-03-19 17:37:13 +00:00
|
|
|
if let Some(config_path) = config_path() {
|
2022-03-19 17:09:57 +00:00
|
|
|
config_builder = config_builder.add_source(
|
2022-03-19 17:37:13 +00:00
|
|
|
config::File::from(config_path)
|
2022-03-19 17:09:57 +00:00
|
|
|
.required(false)
|
|
|
|
.format(config::FileFormat::Toml),
|
|
|
|
);
|
2022-03-19 17:17:30 +00:00
|
|
|
};
|
2022-03-19 17:09:57 +00:00
|
|
|
|
|
|
|
// TODO: Make the config from environment a separate source instead? Seems
|
|
|
|
// cleaner to separate it like that, especially if the config::Config instance
|
|
|
|
// can keep track of where the config comes from then (it doesn't seem like it
|
|
|
|
// can, however - we don't give a name or anything to the Config object).
|
|
|
|
if let Ok(value) = env::var("JJ_USER") {
|
|
|
|
config_builder = config_builder.set_override("user.name", value)?;
|
|
|
|
}
|
|
|
|
if let Ok(value) = env::var("JJ_EMAIL") {
|
|
|
|
config_builder = config_builder.set_override("user.email", value)?;
|
|
|
|
}
|
|
|
|
if let Ok(value) = env::var("JJ_TIMESTAMP") {
|
|
|
|
config_builder = config_builder.set_override("user.timestamp", value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let config = config_builder.build()?;
|
|
|
|
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) => {
|
|
|
|
let ui = Ui::for_terminal(user_settings);
|
|
|
|
let status = dispatch(ui, &mut std::env::args_os());
|
|
|
|
std::process::exit(status);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|