diff options
Diffstat (limited to 'atuin-client/src')
| -rw-r--r-- | atuin-client/src/api_client.rs | 10 | ||||
| -rw-r--r-- | atuin-client/src/database.rs | 11 | ||||
| -rw-r--r-- | atuin-client/src/history.rs | 9 | ||||
| -rw-r--r-- | atuin-client/src/import/xonsh.rs | 11 | ||||
| -rw-r--r-- | atuin-client/src/import/xonsh_sqlite.rs | 11 | ||||
| -rw-r--r-- | atuin-client/src/import/zsh_histdb.rs | 10 | ||||
| -rw-r--r-- | atuin-client/src/lib.rs | 2 | ||||
| -rw-r--r-- | atuin-client/src/utils.rs | 14 |
8 files changed, 30 insertions, 48 deletions
diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 709627b6..e5273c37 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -25,7 +25,7 @@ use semver::Version; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; -use crate::{history::History, sync::hash_str}; +use crate::{history::History, sync::hash_str, utils::get_host_user}; static APP_USER_AGENT: &str = concat!("atuin/", env!("CARGO_PKG_VERSION"),); @@ -236,13 +236,7 @@ impl<'a> Client<'a> { history_ts: OffsetDateTime, host: Option<String>, ) -> Result<SyncHistoryResponse> { - let host = host.unwrap_or_else(|| { - hash_str(&format!( - "{}:{}", - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()), - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()) - )) - }); + let host = host.unwrap_or_else(|| hash_str(&get_host_user())); let url = format!( "{}/sync/history?sync_ts={}&history_ts={}&host={}", diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 2be27ac8..8d64bf36 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -21,7 +21,10 @@ use sqlx::{ }; use time::OffsetDateTime; -use crate::history::{HistoryId, HistoryStats}; +use crate::{ + history::{HistoryId, HistoryStats}, + utils::get_host_user, +}; use super::{ history::History, @@ -55,11 +58,7 @@ pub fn current_context() -> Context { eprintln!("ERROR: Failed to find $ATUIN_SESSION in the environment. Check that you have correctly set up your shell."); std::process::exit(1); }; - let hostname = format!( - "{}:{}", - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()), - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()) - ); + let hostname = get_host_user(); let cwd = utils::get_current_dir(); let host_id = Settings::host_id().expect("failed to load host ID"); let git_root = utils::in_git_repo(cwd.as_str()); diff --git a/atuin-client/src/history.rs b/atuin-client/src/history.rs index 3dfa5c52..bc74aebd 100644 --- a/atuin-client/src/history.rs +++ b/atuin-client/src/history.rs @@ -10,6 +10,7 @@ use atuin_common::utils::uuid_v7; use eyre::{bail, eyre, Result}; use regex::RegexSet; +use crate::utils::get_host_user; use crate::{secrets::SECRET_PATTERNS, settings::Settings}; use time::OffsetDateTime; @@ -106,13 +107,7 @@ impl History { let session = session .or_else(|| env::var("ATUIN_SESSION").ok()) .unwrap_or_else(|| uuid_v7().as_simple().to_string()); - let hostname = hostname.unwrap_or_else(|| { - format!( - "{}:{}", - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()), - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()) - ) - }); + let hostname = hostname.unwrap_or_else(get_host_user); Self { id: uuid_v7().as_simple().to_string().into(), diff --git a/atuin-client/src/import/xonsh.rs b/atuin-client/src/import/xonsh.rs index 8a37c715..19ce4cf6 100644 --- a/atuin-client/src/import/xonsh.rs +++ b/atuin-client/src/import/xonsh.rs @@ -12,6 +12,7 @@ use uuid::Uuid; use super::{get_histpath, Importer, Loader}; use crate::history::History; +use crate::utils::get_host_user; // Note: both HistoryFile and HistoryData have other keys present in the JSON, we don't // care about them so we leave them unspecified so as to avoid deserializing unnecessarily. @@ -60,14 +61,6 @@ fn xonsh_hist_dir(xonsh_data_dir: Option<String>) -> Result<PathBuf> { } } -fn get_hostname() -> String { - format!( - "{}:{}", - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()), - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()), - ) -} - fn load_sessions(hist_dir: &Path) -> Result<Vec<HistoryData>> { let mut sessions = vec![]; for entry in fs::read_dir(hist_dir)? { @@ -111,7 +104,7 @@ impl Importer for Xonsh { let xonsh_data_dir = env::var("XONSH_DATA_DIR").ok(); let hist_dir = get_histpath(|| xonsh_hist_dir(xonsh_data_dir))?; let sessions = load_sessions(&hist_dir)?; - let hostname = get_hostname(); + let hostname = get_host_user(); Ok(Xonsh { sessions, hostname }) } diff --git a/atuin-client/src/import/xonsh_sqlite.rs b/atuin-client/src/import/xonsh_sqlite.rs index de59d477..2817dc63 100644 --- a/atuin-client/src/import/xonsh_sqlite.rs +++ b/atuin-client/src/import/xonsh_sqlite.rs @@ -12,6 +12,7 @@ use uuid::Uuid; use super::{get_histpath, Importer, Loader}; use crate::history::History; +use crate::utils::get_host_user; #[derive(Debug, FromRow)] struct HistDbEntry { @@ -79,14 +80,6 @@ fn xonsh_db_path(xonsh_data_dir: Option<String>) -> Result<PathBuf> { } } -fn get_hostname() -> String { - format!( - "{}:{}", - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()), - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()), - ) -} - #[derive(Debug)] pub struct XonshSqlite { pool: SqlitePool, @@ -109,7 +102,7 @@ impl Importer for XonshSqlite { })?; let pool = SqlitePool::connect(connection_str).await?; - let hostname = get_hostname(); + let hostname = get_host_user(); Ok(XonshSqlite { pool, hostname }) } diff --git a/atuin-client/src/import/zsh_histdb.rs b/atuin-client/src/import/zsh_histdb.rs index f58fd049..eb72baa3 100644 --- a/atuin-client/src/import/zsh_histdb.rs +++ b/atuin-client/src/import/zsh_histdb.rs @@ -33,7 +33,6 @@ // use std::collections::HashMap; -use std::env; use std::path::{Path, PathBuf}; use async_trait::async_trait; @@ -46,6 +45,7 @@ use time::PrimitiveDateTime; use super::Importer; use crate::history::History; use crate::import::Loader; +use crate::utils::{get_hostname, get_username}; #[derive(sqlx::FromRow, Debug)] pub struct HistDbEntryCount { @@ -64,14 +64,6 @@ pub struct HistDbEntry { pub session: i64, } -fn get_hostname() -> String { - env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()) -} - -fn get_username() -> String { - env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()) -} - #[derive(Debug)] pub struct ZshHistDb { histdb: Vec<HistDbEntry>, diff --git a/atuin-client/src/lib.rs b/atuin-client/src/lib.rs index 05b69450..66258af3 100644 --- a/atuin-client/src/lib.rs +++ b/atuin-client/src/lib.rs @@ -17,3 +17,5 @@ pub mod ordering; pub mod record; pub mod secrets; pub mod settings; + +mod utils; diff --git a/atuin-client/src/utils.rs b/atuin-client/src/utils.rs new file mode 100644 index 00000000..a7c6eab0 --- /dev/null +++ b/atuin-client/src/utils.rs @@ -0,0 +1,14 @@ +pub(crate) fn get_hostname() -> String { + std::env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| { + whoami::fallible::hostname().unwrap_or_else(|_| "unknown-host".to_string()) + }) +} + +pub(crate) fn get_username() -> String { + std::env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username()) +} + +/// Returns a pair of the hostname and username, separated by a colon. +pub(crate) fn get_host_user() -> String { + format!("{}:{}", get_hostname(), get_username()) +} |
