Skip to content

Commit

Permalink
Update config-rs (#280)
Browse files Browse the repository at this point in the history
* Update config-rs

Also fix our call to current_dir

This should resolve #195

Thanks @conradludgate for the upstream fix!

* Format
ellie authored Mar 17, 2022
1 parent 3248883 commit d270798
Showing 6 changed files with 646 additions and 350 deletions.
884 changes: 582 additions & 302 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion atuin-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
68 changes: 37 additions & 31 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
@@ -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")?;

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))?;
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("_"));

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)
}
}
2 changes: 1 addition & 1 deletion atuin-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 19 additions & 14 deletions atuin-server/src/settings.rs
Original file line number Diff line number Diff line change
@@ -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 s = Config::new();

if config_file.exists() {
s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))?;
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("_"));

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))
}
}
7 changes: 6 additions & 1 deletion src/command/history.rs
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit d270798

Please sign in to comment.