2021-02-13 13:21:49 -07:00
|
|
|
use std::env;
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
use chrono::Utc;
|
2022-04-28 11:53:59 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2023-04-11 09:26:16 -06:00
|
|
|
use atuin_common::utils::uuid_v7;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
// Any new fields MUST be Optional<>!
|
2022-08-21 15:06:14 -06:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow)]
|
2020-10-04 17:59:28 -06:00
|
|
|
pub struct History {
|
2021-02-13 10:02:52 -07:00
|
|
|
pub id: String,
|
2021-04-13 12:14:07 -06:00
|
|
|
pub timestamp: chrono::DateTime<Utc>,
|
2021-02-13 10:02:52 -07:00
|
|
|
pub duration: i64,
|
|
|
|
pub exit: i64,
|
2020-10-04 17:59:28 -06:00
|
|
|
pub command: String,
|
|
|
|
pub cwd: String,
|
2021-02-13 13:21:49 -07:00
|
|
|
pub session: String,
|
|
|
|
pub hostname: String,
|
2023-03-20 03:26:54 -06:00
|
|
|
pub deleted_at: Option<chrono::DateTime<Utc>>,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
|
2023-03-20 15:26:37 -06:00
|
|
|
// Forgive me, for I have sinned
|
|
|
|
// I need to replace rmp with something that is more backwards-compatible.
|
|
|
|
// Protobuf, or maybe just json
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow)]
|
|
|
|
pub struct HistoryWithoutDelete {
|
|
|
|
pub id: String,
|
|
|
|
pub timestamp: chrono::DateTime<Utc>,
|
|
|
|
pub duration: i64,
|
|
|
|
pub exit: i64,
|
|
|
|
pub command: String,
|
|
|
|
pub cwd: String,
|
|
|
|
pub session: String,
|
|
|
|
pub hostname: String,
|
|
|
|
}
|
|
|
|
|
2020-10-04 17:59:28 -06:00
|
|
|
impl History {
|
2023-03-20 03:26:54 -06:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-02-13 13:21:49 -07:00
|
|
|
pub fn new(
|
2021-04-13 12:14:07 -06:00
|
|
|
timestamp: chrono::DateTime<Utc>,
|
2021-02-13 13:21:49 -07:00
|
|
|
command: String,
|
|
|
|
cwd: String,
|
|
|
|
exit: i64,
|
|
|
|
duration: i64,
|
|
|
|
session: Option<String>,
|
|
|
|
hostname: Option<String>,
|
2023-03-20 03:26:54 -06:00
|
|
|
deleted_at: Option<chrono::DateTime<Utc>>,
|
2021-02-14 08:15:26 -07:00
|
|
|
) -> Self {
|
2021-02-14 11:00:41 -07:00
|
|
|
let session = session
|
|
|
|
.or_else(|| env::var("ATUIN_SESSION").ok())
|
2023-04-11 09:26:16 -06:00
|
|
|
.unwrap_or_else(|| uuid_v7().as_simple().to_string());
|
2021-02-14 10:18:02 -07:00
|
|
|
let hostname =
|
2021-04-13 12:14:07 -06:00
|
|
|
hostname.unwrap_or_else(|| format!("{}:{}", whoami::hostname(), whoami::username()));
|
2021-02-13 13:21:49 -07:00
|
|
|
|
2021-02-14 08:15:26 -07:00
|
|
|
Self {
|
2023-04-11 09:26:16 -06:00
|
|
|
id: uuid_v7().as_simple().to_string(),
|
2021-02-13 12:37:00 -07:00
|
|
|
timestamp,
|
2020-10-05 10:20:48 -06:00
|
|
|
command,
|
|
|
|
cwd,
|
2021-02-13 10:02:52 -07:00
|
|
|
exit,
|
|
|
|
duration,
|
2021-02-13 13:21:49 -07:00
|
|
|
session,
|
|
|
|
hostname,
|
2023-03-20 03:26:54 -06:00
|
|
|
deleted_at,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 00:13:30 -06:00
|
|
|
|
|
|
|
pub fn success(&self) -> bool {
|
|
|
|
self.exit == 0 || self.duration == -1
|
|
|
|
}
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|