From 95e9530dad098e69de560a3f4d198b5c05cc53ba Mon Sep 17 00:00:00 2001 From: Rain Date: Tue, 5 Mar 2024 01:49:40 -0800 Subject: fix(build): make atuin compile on non-win/mac/linux platforms (#1825) Hi! I've been trying to get atuin set up on the illumos machine I built for work @oxidecomputer, and I ran into a few issues which are fixed here: 1. The `clipboard` feature was only supported on Windows, Mac and Linux. I've added a platform gate for that. 2. The `atomic-write-file` crate needed an update to the version of `nix` -- that is included. 3. As part of this, I found a [security bug](https://rustsec.org/advisories/RUSTSEC-2024-0020.html) in the whoami crate. The bug has been fixed upstream and I've included it. whoami 1.5.0 deprecates the `hostname` function, which produced some fresh warnings. While fixing the warnings I also took the liberty of doing some code rearrangement, adding a few functions that wrap some common operations. I didn't really know where to put those functions, so I created a new `utils` module for it. If you have a better place to put them, I'm happy to change the PR. Feel free to make any changes to this PR if you like before landing it, or to ask for review. As a followup I'm also happy to set up a cross-compile build for atuin on illumos. It's a bit harder to run tests in CI for illumos at the moment, but I'm trying to get a project started up to make that happen in the future as well. --- atuin-client/src/api_client.rs | 10 ++-------- atuin-client/src/database.rs | 11 +++++------ atuin-client/src/history.rs | 9 ++------- atuin-client/src/import/xonsh.rs | 11 ++--------- atuin-client/src/import/xonsh_sqlite.rs | 11 ++--------- atuin-client/src/import/zsh_histdb.rs | 10 +--------- atuin-client/src/lib.rs | 2 ++ atuin-client/src/utils.rs | 14 ++++++++++++++ 8 files changed, 30 insertions(+), 48 deletions(-) create mode 100644 atuin-client/src/utils.rs (limited to 'atuin-client/src') 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, ) -> Result { - 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) -> Result { } } -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> { 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) -> Result { } } -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, 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()) +} -- cgit v1.3.1