Store host IDs, not hostnames

Why? Hostnames can change a lot, and therefore host filtering can be
funky. Really, all we want is a unique ID per machine + do not care what
it might be.
This commit is contained in:
Ellie Huxtable 2023-05-09 17:31:06 -04:00
parent c65e7528f4
commit 0fa1537fbd
2 changed files with 20 additions and 1 deletions

View File

@ -16,13 +16,14 @@ use sqlx::{
use super::{
history::History,
ordering,
settings::{FilterMode, SearchMode},
settings::{FilterMode, SearchMode, Settings},
};
pub struct Context {
pub session: String,
pub cwd: String,
pub hostname: String,
pub host_id: String,
}
#[derive(Default, Clone)]
@ -45,11 +46,13 @@ pub fn current_context() -> Context {
};
let hostname = format!("{}:{}", whoami::hostname(), whoami::username());
let cwd = utils::get_current_dir();
let host_id = Settings::host_id().expect("failed to load host ID");
Context {
session,
hostname,
cwd,
host_id,
}
}

View File

@ -17,6 +17,7 @@ pub const HISTORY_PAGE_SIZE: i64 = 100;
pub const LAST_SYNC_FILENAME: &str = "last_sync_time";
pub const LAST_VERSION_CHECK_FILENAME: &str = "last_version_check_time";
pub const LATEST_VERSION_FILENAME: &str = "latest_version";
pub const HOST_ID_FILENAME: &str = "host_id";
#[derive(Clone, Debug, Deserialize, Copy, ValueEnum, PartialEq)]
pub enum SearchMode {
@ -222,6 +223,21 @@ impl Settings {
Settings::load_time_from_file(LAST_VERSION_CHECK_FILENAME)
}
pub fn host_id() -> Option<String> {
let id = Settings::read_from_data_dir(HOST_ID_FILENAME);
if id.is_some() {
return id;
}
let uuid = atuin_common::utils::uuid_v7();
Settings::save_to_data_dir(HOST_ID_FILENAME, uuid.as_simple().to_string().as_ref())
.expect("Could not write host ID to data dir");
Some(uuid.as_simple().to_string())
}
pub fn should_sync(&self) -> Result<bool> {
if !self.auto_sync || !PathBuf::from(self.session_path.as_str()).exists() {
return Ok(false);