-
-
Notifications
You must be signed in to change notification settings - Fork 631
Feature csv import #2669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wyattearp
wants to merge
5
commits into
atuinsh:main
Choose a base branch
from
wyattearp:feature-csv-import
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−2
Open
Feature csv import #2669
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dba4cf0
feat: csv import from file
wyattearp cca2fac
adding in command to client, formatting
wyattearp df38582
Merge branch 'atuinsh:main' into feature-csv-import
wyattearp 2a03a05
rename to match other importers
wyattearp 0190be8
resolving formatting error
wyattearp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::fs::File; | ||
use std::io::{BufRead, BufReader}; | ||
use std::path::PathBuf; | ||
|
||
use async_trait::async_trait; | ||
use csv::Reader; | ||
use eyre::{Result, eyre}; | ||
use time::OffsetDateTime; | ||
|
||
use super::{Importer, Loader}; | ||
use crate::history::History; | ||
|
||
const HISTORY_FILE: &str = "history.csv"; // Global constant for history file | ||
|
||
#[derive(Debug)] | ||
pub struct CsvHistoryImporter; | ||
|
||
fn default_histpath() -> Result<PathBuf> { | ||
let histpath = PathBuf::from(HISTORY_FILE); | ||
if histpath.exists() { | ||
Ok(histpath) | ||
} else { | ||
Err(eyre!( | ||
"Could not find history file. Please create 'history.csv' in the current directory." | ||
)) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Importer for CsvHistoryImporter { | ||
const NAME: &'static str = "csv_history"; | ||
|
||
async fn new() -> Result<Self> { | ||
let _ = default_histpath()?; // Ensure the file exists | ||
Ok(Self) | ||
} | ||
|
||
async fn entries(&mut self) -> Result<usize> { | ||
let file = File::open(default_histpath()?)?; | ||
let reader = BufReader::new(file); | ||
Ok(reader.lines().count() - 1) // Exclude header row | ||
} | ||
|
||
async fn load(self, h: &mut impl Loader) -> Result<()> { | ||
let file = File::open(default_histpath()?)?; | ||
let mut reader = Reader::from_reader(file); | ||
|
||
for result in reader.records() { | ||
let record = result?; | ||
if let (Some(timestamp), Some(duration), Some(command)) = | ||
(record.get(0), record.get(1), record.get(2)) | ||
{ | ||
let timestamp = timestamp | ||
.parse::<i64>() | ||
.ok() | ||
.and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok()) | ||
.unwrap_or_else(OffsetDateTime::now_utc); | ||
let duration = duration.parse::<i64>().map_or(-1, |t| t * 1_000_000_000); | ||
|
||
let imported = History::import() | ||
.timestamp(timestamp) | ||
.command(command.trim().to_string()) | ||
.duration(duration); | ||
|
||
h.push(imported.build().into()).await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::import::tests::TestLoader; | ||
use itertools::assert_equal; | ||
use std::fs; | ||
use std::io::Write; | ||
use tempfile::NamedTempFile; | ||
|
||
#[tokio::test] | ||
async fn test_parse_file() { | ||
let mut file = NamedTempFile::new().unwrap(); | ||
writeln!(file, "timestamp,duration,command").unwrap(); | ||
writeln!(file, "1613322469,10,cargo install atuin").unwrap(); | ||
writeln!(file, "1613322470,5,ls -la").unwrap(); | ||
writeln!(file, "1613322471,3,git status").unwrap(); | ||
|
||
let path = file.path().to_path_buf(); | ||
fs::copy(&path, HISTORY_FILE).unwrap(); | ||
|
||
let mut importer = CsvHistoryImporter::new().await.unwrap(); | ||
assert_eq!(importer.entries().await.unwrap(), 3); | ||
|
||
let mut loader = TestLoader::default(); | ||
importer.load(&mut loader).await.unwrap(); | ||
|
||
assert_equal( | ||
loader.buf.iter().map(|h| h.command.as_str()), | ||
["cargo install atuin", "ls -la", "git status"], | ||
); | ||
|
||
fs::remove_file(HISTORY_FILE).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
default_histpath
approach made sense for classic shell history import where there is usually an expected location for data, but I think in this case it isn't neededInstead, we should make it so that the import csv subcommand takes a mandatory argument of the file to import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's not a facility i'm seeing in any of the other importers to specify the location of a file as part of the command line parsing - am i missing it somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trying to dig more on this (excuse my rust n00bness) - it looks like it would be either a modification to the main Importer class, which means everything would get an option, but only one of them would support it, or i create a single "one off" function to basically mimic
async fn import
but takes a path.are there better options i'm not seeing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellie - any suggestions on the above comment?