diff options
| author | Vlad Stepanov <8uk.8ak@gmail.com> | 2023-06-15 14:29:40 +0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-15 10:29:40 +0000 |
| commit | 4077c33adfdacaf0ed68657a1955a7b69a78d373 (patch) | |
| tree | 432d5c23992388a6c1bd4b11d41785ea00d56905 /atuin-client/src/import | |
| parent | Add namespaces to kv store (#1052) (diff) | |
| download | atuin-4077c33adfdacaf0ed68657a1955a7b69a78d373.zip | |
Builder interface for History objects (#933)
* [feature] store env variables in History records
WIP: remove `HistoryWithoutDelete`, add some docstrings, tests
* Create History objects through builders.
Assure in compile-time that all required fields
are set for the given construction scenario
* (from #882) split Cmd::run into subfns
* Update `History` doc
* remove rmp-serde from history
* update warning
---------
Co-authored-by: Conrad Ludgate <conrad.ludgate@truelayer.com>
Diffstat (limited to 'atuin-client/src/import')
| -rw-r--r-- | atuin-client/src/import/bash.rs | 14 | ||||
| -rw-r--r-- | atuin-client/src/import/fish.rs | 28 | ||||
| -rw-r--r-- | atuin-client/src/import/nu.rs | 14 | ||||
| -rw-r--r-- | atuin-client/src/import/nu_histdb.rs | 23 | ||||
| -rw-r--r-- | atuin-client/src/import/resh.rs | 23 | ||||
| -rw-r--r-- | atuin-client/src/import/zsh.rs | 35 | ||||
| -rw-r--r-- | atuin-client/src/import/zsh_histdb.rs | 36 |
7 files changed, 66 insertions, 107 deletions
diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs index 9901c1f3..25ede053 100644 --- a/atuin-client/src/import/bash.rs +++ b/atuin-client/src/import/bash.rs @@ -80,17 +80,9 @@ impl Importer for Bash { next_timestamp = t; } LineType::Command(c) => { - let entry = History::new( - next_timestamp, - c.into(), - "unknown".into(), - -1, - -1, - None, - None, - None, - ); - h.push(entry).await?; + let imported = History::import().timestamp(next_timestamp).command(c); + + h.push(imported.build().into()).await?; next_timestamp += timestamp_increment; } } diff --git a/atuin-client/src/import/fish.rs b/atuin-client/src/import/fish.rs index e54ca730..90ecabc3 100644 --- a/atuin-client/src/import/fish.rs +++ b/atuin-client/src/import/fish.rs @@ -73,19 +73,9 @@ impl Importer for Fish { // first, we must deal with the prev cmd if let Some(cmd) = cmd.take() { let time = time.unwrap_or(now); + let entry = History::import().timestamp(time).command(cmd); - loader - .push(History::new( - time, - cmd, - "unknown".into(), - -1, - -1, - None, - None, - None, - )) - .await?; + loader.push(entry.build().into()).await?; } // using raw strings to avoid needing escaping. @@ -109,19 +99,9 @@ impl Importer for Fish { // we might have a trailing cmd if let Some(cmd) = cmd.take() { let time = time.unwrap_or(now); + let entry = History::import().timestamp(time).command(cmd); - loader - .push(History::new( - time, - cmd, - "unknown".into(), - -1, - -1, - None, - None, - None, - )) - .await?; + loader.push(entry.build().into()).await?; } Ok(()) diff --git a/atuin-client/src/import/nu.rs b/atuin-client/src/import/nu.rs index 0f107604..46600325 100644 --- a/atuin-client/src/import/nu.rs +++ b/atuin-client/src/import/nu.rs @@ -58,17 +58,9 @@ impl Importer for Nu { let offset = chrono::Duration::nanoseconds(counter); counter += 1; - h.push(History::new( - now - offset, // preserve ordering - cmd, - String::from("unknown"), - -1, - -1, - None, - None, - None, - )) - .await?; + let entry = History::import().timestamp(now - offset).command(cmd); + + h.push(entry.build().into()).await?; } Ok(()) diff --git a/atuin-client/src/import/nu_histdb.rs b/atuin-client/src/import/nu_histdb.rs index 0fb5192e..34568d80 100644 --- a/atuin-client/src/import/nu_histdb.rs +++ b/atuin-client/src/import/nu_histdb.rs @@ -30,16 +30,19 @@ impl From<HistDbEntry> for History { fn from(histdb_item: HistDbEntry) -> Self { let ts_secs = histdb_item.start_timestamp / 1000; let ts_ns = (histdb_item.start_timestamp % 1000) * 1_000_000; - History::new( - DateTime::from_utc(NaiveDateTime::from_timestamp(ts_secs, ts_ns as u32), Utc), - String::from_utf8(histdb_item.command_line).unwrap(), - String::from_utf8(histdb_item.cwd).unwrap(), - histdb_item.exit_status, - histdb_item.duration_ms, - Some(format!("{:x}", histdb_item.session_id)), - Some(String::from_utf8(histdb_item.hostname).unwrap()), - None, - ) + let imported = History::import() + .timestamp(DateTime::from_utc( + NaiveDateTime::from_timestamp(ts_secs, ts_ns as u32), + Utc, + )) + .command(String::from_utf8(histdb_item.command_line).unwrap()) + .cwd(String::from_utf8(histdb_item.cwd).unwrap()) + .exit(histdb_item.exit_status) + .duration(histdb_item.duration_ms) + .session(format!("{:x}", histdb_item.session_id)) + .hostname(String::from_utf8(histdb_item.hostname).unwrap()); + + imported.build().into() } } diff --git a/atuin-client/src/import/resh.rs b/atuin-client/src/import/resh.rs index 6fa27b5a..3c5b799e 100644 --- a/atuin-client/src/import/resh.rs +++ b/atuin-client/src/import/resh.rs @@ -122,18 +122,17 @@ impl Importer for Resh { difference.num_nanoseconds().unwrap_or(0) }; - h.push(History { - id: uuid_v7().as_simple().to_string(), - timestamp, - duration, - exit: entry.exit_code, - command: entry.cmd_line, - cwd: entry.pwd, - session: uuid_v7().as_simple().to_string(), - hostname: entry.host, - deleted_at: None, - }) - .await?; + let imported = History::import() + .command(entry.cmd_line) + .timestamp(timestamp) + .duration(duration) + .exit(entry.exit_code) + .cwd(entry.pwd) + .hostname(entry.host) + // CHECK: should we add uuid here? It's not set in the other importers + .session(uuid_v7().as_simple().to_string()); + + h.push(imported.build().into()).await?; } Ok(()) diff --git a/atuin-client/src/import/zsh.rs b/atuin-client/src/import/zsh.rs index 19917daf..e98819e2 100644 --- a/atuin-client/src/import/zsh.rs +++ b/atuin-client/src/import/zsh.rs @@ -82,17 +82,12 @@ impl Importer for Zsh { let offset = chrono::Duration::seconds(counter); counter += 1; - h.push(History::new( - now - offset, // preserve ordering - command.trim_end().to_string(), - String::from("unknown"), - -1, - -1, - None, - None, - None, - )) - .await?; + let imported = History::import() + // preserve ordering + .timestamp(now - offset) + .command(command.trim_end().to_string()); + + h.push(imported.build().into()).await?; } } } @@ -113,19 +108,15 @@ fn parse_extended(line: &str, counter: i64) -> History { let time = Utc.timestamp(time, 0); let time = time + offset; + // use nanos, because why the hell not? we won't display them. let duration = duration.parse::<i64>().map_or(-1, |t| t * 1_000_000_000); - // use nanos, because why the hell not? we won't display them. - History::new( - time, - command.trim_end().to_string(), - String::from("unknown"), - 0, // assume 0, we have no way of knowing :( - duration, - None, - None, - None, - ) + let imported = History::import() + .timestamp(time) + .command(command.trim_end().to_string()) + .duration(duration); + + imported.build().into() } #[cfg(test)] diff --git a/atuin-client/src/import/zsh_histdb.rs b/atuin-client/src/import/zsh_histdb.rs index 2f9a192d..78a7176b 100644 --- a/atuin-client/src/import/zsh_histdb.rs +++ b/atuin-client/src/import/zsh_histdb.rs @@ -61,27 +61,29 @@ pub struct HistDbEntry { impl From<HistDbEntry> for History { fn from(histdb_item: HistDbEntry) -> Self { - History::new( - DateTime::from_utc(histdb_item.start_time, Utc), // must assume UTC? - String::from_utf8(histdb_item.argv) - .unwrap_or_else(|_e| String::from("")) - .trim_end() - .to_string(), - String::from_utf8(histdb_item.dir) - .unwrap_or_else(|_e| String::from("")) - .trim_end() - .to_string(), - 0, // assume 0, we have no way of knowing :( - histdb_item.duration, - None, - Some( + let imported = History::import() + .timestamp(DateTime::from_utc(histdb_item.start_time, Utc)) + .command( + String::from_utf8(histdb_item.argv) + .unwrap_or_else(|_e| String::from("")) + .trim_end() + .to_string(), + ) + .cwd( + String::from_utf8(histdb_item.dir) + .unwrap_or_else(|_e| String::from("")) + .trim_end() + .to_string(), + ) + .duration(histdb_item.duration) + .hostname( String::from_utf8(histdb_item.host) .unwrap_or_else(|_e| String::from("")) .trim_end() .to_string(), - ), - None, - ) + ); + + imported.build().into() } } |
