From 4917f5c79ede365529fc4a16b0f09e565b9b2f3d Mon Sep 17 00:00:00 2001 From: Alexander Thaller Date: Tue, 5 Oct 2021 13:39:30 +0200 Subject: [PATCH 1/5] Add testcase for empty file --- tests/client_server_intigration.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/client_server_intigration.rs b/tests/client_server_intigration.rs index 5a6cd2e..2af35bd 100644 --- a/tests/client_server_intigration.rs +++ b/tests/client_server_intigration.rs @@ -374,3 +374,21 @@ fn write_newline_command() { assert_eq!(entries.len(), 0); } + +#[test] +fn existing_empty_file() { + let hostname = "testhostname".to_string(); + let data_dir = tempfile::tempdir().unwrap().into_path(); + std::fs::File::create(data_dir.join(format!("{}.csv", hostname))).unwrap(); + + dbg!(&data_dir); + + let store = store::new(data_dir.clone()); + let entries = store.get_entries(&Filter::default()).unwrap(); + + dbg!(&entries); + + assert_eq!(entries.len(), 0); + + std::fs::remove_dir_all(&data_dir).unwrap(); +} From d7a24cc28d0e3063e61b2ec3ab0b475662cf2d89 Mon Sep 17 00:00:00 2001 From: Alexander Thaller Date: Mon, 18 Oct 2021 11:09:01 +0200 Subject: [PATCH 2/5] Rename log errors to index errors to make them more clear. --- src/store/mod.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/store/mod.rs b/src/store/mod.rs index 4b3ecb6..c714ad2 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -13,11 +13,11 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum Error { - #[error("can not create log folder: {0}")] - CreateLogFolder(PathBuf, std::io::Error), + #[error("can not create index folder: {0}")] + CreateIndexFolder(PathBuf, std::io::Error), - #[error("can not open log file: {0}")] - OpenLogFile(PathBuf, std::io::Error), + #[error("can not open index file: {0}")] + OpenIndexFile(PathBuf, std::io::Error), #[error("can not serialize entry: {0}")] SerializeEntry(csv::Error), @@ -28,8 +28,8 @@ pub enum Error { #[error("problem while iterating glob: {0}")] GlobIteration(glob::GlobError), - #[error("can not read log file {0:?}: {1}")] - ReadLogFile(PathBuf, csv::Error), + #[error("can not read index file {0:?}: {1}")] + ReadIndexFile(PathBuf, csv::Error), #[error("{0}")] Filter(#[from] filter::Error), @@ -55,7 +55,7 @@ impl Store { let file_path = folder_path.join(format!("{}.csv", hostname)); fs::create_dir_all(&folder_path) - .map_err(|err| Error::CreateLogFolder(folder_path.to_path_buf(), err))?; + .map_err(|err| Error::CreateIndexFolder(folder_path.to_path_buf(), err))?; let mut builder = csv::WriterBuilder::new(); @@ -68,7 +68,7 @@ impl Store { .append(true) .create(true) .open(&file_path) - .map_err(|err| Error::OpenLogFile(file_path.clone(), err))?; + .map_err(|err| Error::OpenIndexFile(file_path.clone(), err))?; let mut writer = builder.from_writer(index_file); @@ -119,12 +119,12 @@ impl Store { fn read_log_file>(file_path: P) -> Result, Error> { let file = std::fs::File::open(&file_path) - .map_err(|err| Error::OpenLogFile(file_path.as_ref().to_path_buf(), err))?; + .map_err(|err| Error::OpenIndexFile(file_path.as_ref().to_path_buf(), err))?; let reader = std::io::BufReader::new(file); Self::read_metadata(reader) - .map_err(|err| Error::ReadLogFile(file_path.as_ref().to_path_buf(), err)) + .map_err(|err| Error::ReadIndexFile(file_path.as_ref().to_path_buf(), err)) } fn read_metadata(reader: R) -> Result, csv::Error> { From 9799ac502ea4076f42942633735bc44b54ab8bae Mon Sep 17 00:00:00 2001 From: Alexander Thaller Date: Mon, 4 Jul 2022 10:18:49 +0200 Subject: [PATCH 3/5] Implement text_excluded flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added following flag: ``` -T, --text_excluded Only print entries not containing the given regex ``` When specified it will exclude all commands containing the given regex. Example: ``` » echo 'test' test » hstdb tmn cmd 10:19 echo 'test' » hstdb -T test tmn cmd ``` --- src/opt.rs | 7 ++++++- src/store/filter.rs | 14 +++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/opt.rs b/src/opt.rs index 1ce50c4..85ec0e3 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -203,6 +203,10 @@ struct DefaultArgs { #[structopt(short = "t", long = "text")] command_text: Option, + /// Only print entries not containing the given regex + #[structopt(short = "T", long = "text_excluded")] + command_text_excluded: Option, + /// Only print entries that have been executed in the current directory #[structopt(short, long = "in", conflicts_with = "folder")] in_current: bool, @@ -335,6 +339,7 @@ impl Opt { let session_filter = self.default_args.session; let no_subdirs = self.default_args.no_subdirs; let command_text = self.default_args.command_text; + let command_text_excluded = self.default_args.command_text_excluded; let filter_failed = self.default_args.filter_failed; let find_status = self.default_args.find_status; let config = config::Config::open(self.default_args.config.config_path) @@ -359,7 +364,7 @@ impl Opt { .directory(folder, in_current, no_subdirs)? .hostname(hostname, all_hosts)? .count(entries_count) - .command(command, command_text) + .command(command, command_text, command_text_excluded) .session(session_filter) .filter_failed(filter_failed) .find_status(find_status); diff --git a/src/store/filter.rs b/src/store/filter.rs index dcdbac1..4d5ba70 100644 --- a/src/store/filter.rs +++ b/src/store/filter.rs @@ -19,6 +19,7 @@ pub struct Filter { pub command: Option, pub no_subdirs: bool, pub command_text: Option, + pub command_text_excluded: Option, pub count: usize, pub session: Option, pub filter_failed: bool, @@ -68,10 +69,16 @@ impl Filter { Self { count, ..self } } - pub fn command(self, command: Option, command_text: Option) -> Self { + pub fn command( + self, + command: Option, + command_text: Option, + command_text_excluded: Option, + ) -> Self { Self { command, command_text, + command_text_excluded, ..self } } @@ -98,6 +105,11 @@ impl Filter { .as_ref() .map_or(true, |regex| regex.is_match(&entry.command)) }) + .filter(|entry| { + self.command_text_excluded + .as_ref() + .map_or(true, |regex| !regex.is_match(&entry.command)) + }) .filter(|entry| { self.session .as_ref() From 9758becbf1d6e562ba7c055b9b98832465c7c0bd Mon Sep 17 00:00:00 2001 From: Alexander Thaller Date: Mon, 4 Jul 2022 10:35:47 +0200 Subject: [PATCH 4/5] Revert "Implement text_excluded flag" This reverts commit 9799ac502ea4076f42942633735bc44b54ab8bae. --- src/opt.rs | 7 +------ src/store/filter.rs | 14 +------------- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/src/opt.rs b/src/opt.rs index 85ec0e3..1ce50c4 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -203,10 +203,6 @@ struct DefaultArgs { #[structopt(short = "t", long = "text")] command_text: Option, - /// Only print entries not containing the given regex - #[structopt(short = "T", long = "text_excluded")] - command_text_excluded: Option, - /// Only print entries that have been executed in the current directory #[structopt(short, long = "in", conflicts_with = "folder")] in_current: bool, @@ -339,7 +335,6 @@ impl Opt { let session_filter = self.default_args.session; let no_subdirs = self.default_args.no_subdirs; let command_text = self.default_args.command_text; - let command_text_excluded = self.default_args.command_text_excluded; let filter_failed = self.default_args.filter_failed; let find_status = self.default_args.find_status; let config = config::Config::open(self.default_args.config.config_path) @@ -364,7 +359,7 @@ impl Opt { .directory(folder, in_current, no_subdirs)? .hostname(hostname, all_hosts)? .count(entries_count) - .command(command, command_text, command_text_excluded) + .command(command, command_text) .session(session_filter) .filter_failed(filter_failed) .find_status(find_status); diff --git a/src/store/filter.rs b/src/store/filter.rs index 4d5ba70..dcdbac1 100644 --- a/src/store/filter.rs +++ b/src/store/filter.rs @@ -19,7 +19,6 @@ pub struct Filter { pub command: Option, pub no_subdirs: bool, pub command_text: Option, - pub command_text_excluded: Option, pub count: usize, pub session: Option, pub filter_failed: bool, @@ -69,16 +68,10 @@ impl Filter { Self { count, ..self } } - pub fn command( - self, - command: Option, - command_text: Option, - command_text_excluded: Option, - ) -> Self { + pub fn command(self, command: Option, command_text: Option) -> Self { Self { command, command_text, - command_text_excluded, ..self } } @@ -105,11 +98,6 @@ impl Filter { .as_ref() .map_or(true, |regex| regex.is_match(&entry.command)) }) - .filter(|entry| { - self.command_text_excluded - .as_ref() - .map_or(true, |regex| !regex.is_match(&entry.command)) - }) .filter(|entry| { self.session .as_ref() From 6a29fe55db3d3fa3ee2087ef09c237738b726eaa Mon Sep 17 00:00:00 2001 From: Alexander Thaller Date: Thu, 11 Aug 2022 14:04:25 +0200 Subject: [PATCH 5/5] Improve error message related to index files --- src/server/mod.rs | 2 +- src/store/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 9504e40..fa4a9e6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -87,7 +87,7 @@ pub enum Error { #[error("can not remove entry from db: {0}")] RemoveDbEntry(db::Error), - #[error("can not add to storeo: {0}")] + #[error("can not add to store: {0}")] AddStore(crate::store::Error), #[error("db error: {0}")] diff --git a/src/store/mod.rs b/src/store/mod.rs index c714ad2..d6195c5 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -13,10 +13,10 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum Error { - #[error("can not create index folder: {0}")] + #[error("can not create index folder at path {0:?}: {1}")] CreateIndexFolder(PathBuf, std::io::Error), - #[error("can not open index file: {0}")] + #[error("can not open index file at path {0:?}: {1}")] OpenIndexFile(PathBuf, std::io::Error), #[error("can not serialize entry: {0}")]