Update config-rs (#280)

* Update config-rs

Also fix our call to current_dir

This should resolve #195

Thanks @conradludgate for the upstream fix!

* Format
This commit is contained in:
Ellie Huxtable 2022-03-17 21:26:57 +00:00 committed by GitHub
parent 32488830b8
commit d270798277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 647 additions and 351 deletions

890
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@ directories = "3"
uuid = { version = "0.8", features = ["v4"] }
whoami = "1.1.2"
chrono-english = "0.1.4"
config = "0.11"
config = "0.12"
serde_derive = "1.0.125"
serde = "1.0.126"
serde_json = "1.0.75"

View File

@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use chrono::prelude::*;
use chrono::Utc;
use config::{Config, Environment, File as ConfigFile};
use config::{Config, Environment, File as ConfigFile, FileFormat};
use eyre::{eyre, Context, Result};
use parse_duration::parse;
@ -120,56 +120,62 @@ impl Settings {
config_file.push("config.toml");
let mut s = Config::new();
let db_path = data_dir.join("history.db");
let key_path = data_dir.join("key");
let session_path = data_dir.join("session");
s.set_default("db_path", db_path.to_str())?;
s.set_default("key_path", key_path.to_str())?;
s.set_default("session_path", session_path.to_str())?;
s.set_default("dialect", "us")?;
s.set_default("auto_sync", true)?;
s.set_default("sync_frequency", "1h")?;
s.set_default("sync_address", "https://api.atuin.sh")?;
s.set_default("search_mode", "prefix")?;
let mut config_builder = Config::builder()
.set_default("db_path", db_path.to_str())?
.set_default("key_path", key_path.to_str())?
.set_default("session_path", session_path.to_str())?
.set_default("dialect", "us")?
.set_default("auto_sync", true)?
.set_default("sync_frequency", "1h")?
.set_default("sync_address", "https://api.atuin.sh")?
.set_default("search_mode", "prefix")?
.set_default("session_token", "")?
.add_source(Environment::with_prefix("atuin").separator("_"));
if config_file.exists() {
s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))
.wrap_err_with(|| format!("could not load config file {:?}", config_file))?;
config_builder = if config_file.exists() {
config_builder.add_source(ConfigFile::new(
config_file.to_str().unwrap(),
FileFormat::Toml,
))
} else {
let example_config = include_bytes!("../config.toml");
let mut file = File::create(config_file).wrap_err("could not create config file")?;
file.write_all(example_config)
.wrap_err("could not write default config file")?;
}
s.merge(Environment::with_prefix("atuin").separator("_"))
.wrap_err("could not load environment")?;
config_builder
};
let config = config_builder.build()?;
let mut settings: Settings = config
.try_deserialize()
.map_err(|e| eyre!("failed to deserialize: {}", e))?;
// all paths should be expanded
let db_path = s.get_str("db_path")?;
let db_path = shellexpand::full(db_path.as_str())?;
s.set("db_path", db_path.to_string())?;
let db_path = settings.db_path;
let db_path = shellexpand::full(&db_path)?;
settings.db_path = db_path.to_string();
let key_path = s.get_str("key_path")?;
let key_path = shellexpand::full(key_path.as_str())?;
s.set("key_path", key_path.to_string())?;
let key_path = settings.key_path;
let key_path = shellexpand::full(&key_path)?;
settings.key_path = key_path.to_string();
let session_path = s.get_str("session_path")?;
let session_path = shellexpand::full(session_path.as_str())?;
s.set("session_path", session_path.to_string())?;
let session_path = settings.session_path;
let session_path = shellexpand::full(&session_path)?;
settings.session_path = session_path.to_string();
// Finally, set the auth token
if Path::new(session_path.to_string().as_str()).exists() {
let token = std::fs::read_to_string(session_path.to_string())?;
s.set("session_token", token.trim())?;
settings.session_token = token.trim().to_string();
} else {
s.set("session_token", "not logged in")?;
settings.session_token = String::from("not logged in");
}
s.try_into()
.map_err(|e| eyre!("failed to deserialize: {}", e))
Ok(settings)
}
}

View File

@ -16,7 +16,7 @@ chrono = { version = "0.4", features = ["serde"] }
eyre = "0.6"
uuid = { version = "0.8", features = ["v4"] }
whoami = "1.1.2"
config = "0.11"
config = "0.12"
serde_derive = "1.0.125"
serde = "1.0.126"
serde_json = "1.0.75"

View File

@ -2,12 +2,12 @@ use std::fs::{create_dir_all, File};
use std::io::prelude::*;
use std::path::PathBuf;
use config::{Config, Environment, File as ConfigFile};
use config::{Config, Environment, File as ConfigFile, FileFormat};
use eyre::{eyre, Result};
pub const HISTORY_PAGE_SIZE: i64 = 100;
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Settings {
pub host: String,
pub port: u16,
@ -33,25 +33,30 @@ impl Settings {
config_file.push("server.toml");
// create the config file if it does not exist
let mut config_builder = Config::builder()
.set_default("host", "127.0.0.1")?
.set_default("port", 8888)?
.set_default("open_registration", false)?
.set_default("db_uri", "default_uri")?
.add_source(Environment::with_prefix("atuin").separator("_"));
let mut s = Config::new();
if config_file.exists() {
s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))?;
config_builder = if config_file.exists() {
config_builder.add_source(ConfigFile::new(
config_file.to_str().unwrap(),
FileFormat::Toml,
))
} else {
let example_config = include_bytes!("../server.toml");
let mut file = File::create(config_file)?;
file.write_all(example_config)?;
}
s.set_default("host", "127.0.0.1")?;
s.set_default("port", 8888)?;
s.set_default("open_registration", false)?;
s.set_default("db_uri", "default_uri")?;
config_builder
};
s.merge(Environment::with_prefix("atuin").separator("_"))?;
let config = config_builder.build()?;
s.try_into()
config
.try_deserialize()
.map_err(|e| eyre!("failed to deserialize: {}", e))
}
}

View File

@ -114,7 +114,12 @@ impl Cmd {
return Ok(());
}
let cwd = env::current_dir()?.display().to_string();
// It's better for atuin to silently fail here and attempt to
// store whatever is ran, than to throw an error to the terminal
let cwd = match env::current_dir() {
Ok(dir) => dir.display().to_string(),
Err(_) => String::from(""),
};
let h = History::new(chrono::Utc::now(), command, cwd, -1, -1, None, None);