-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new
datastore export
command (#166)
* Add a new `datastore export` command * Update CHANGELOG
- Loading branch information
1 parent
3f7c173
commit 1a5d3cd
Showing
14 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,20 +1,69 @@ | ||
use anyhow::Result; | ||
use anyhow::{Context, Result}; | ||
use tracing::info; | ||
|
||
use crate::args; | ||
use crate::args::{DatastoreArgs, DatastoreExportArgs, DatastoreInitArgs, GlobalArgs}; | ||
use noseyparker::datastore::Datastore; | ||
|
||
pub fn run(global_args: &args::GlobalArgs, args: &args::DatastoreArgs) -> Result<()> { | ||
pub fn run(global_args: &GlobalArgs, args: &DatastoreArgs) -> Result<()> { | ||
use crate::args::DatastoreCommand::*; | ||
match &args.command { | ||
args::DatastoreCommand::Init(args) => cmd_datastore_init(global_args, args), | ||
Init(args) => cmd_datastore_init(global_args, args), | ||
Export(args) => cmd_datastore_export(global_args, args), | ||
} | ||
} | ||
|
||
fn cmd_datastore_init( | ||
global_args: &args::GlobalArgs, | ||
args: &args::DatastoreInitArgs, | ||
) -> Result<()> { | ||
fn cmd_datastore_init(global_args: &GlobalArgs, args: &DatastoreInitArgs) -> Result<()> { | ||
let datastore = Datastore::create(&args.datastore, global_args.advanced.sqlite_cache_size)?; | ||
info!("Initialized new datastore at {}", &datastore.root_dir().display()); | ||
Ok(()) | ||
} | ||
|
||
fn cmd_datastore_export(global_args: &GlobalArgs, args: &DatastoreExportArgs) -> Result<()> { | ||
let datastore = Datastore::open(&args.datastore, global_args.advanced.sqlite_cache_size) | ||
.with_context(|| format!("Failed to open datastore at {}", args.datastore.display()))?; | ||
let output_path = &args.output; | ||
|
||
// XXX Move this code into datastore.rs? | ||
|
||
use crate::args::DatastoreExportOutputFormat::*; | ||
match args.format { | ||
Tgz => { | ||
use flate2::write::GzEncoder; | ||
use std::ffi::OsStr; | ||
use std::path::Path; | ||
use tempfile::NamedTempFile; | ||
|
||
let write_tar = |output_path: &Path| -> Result<()> { | ||
let prefix: &OsStr = output_path.file_name().unwrap_or("datastore.tgz".as_ref()); | ||
|
||
let tmp_output = match output_path.parent() { | ||
Some(p) => NamedTempFile::with_prefix_in(prefix, p), | ||
None => NamedTempFile::with_prefix(prefix), | ||
}?; | ||
|
||
let enc = GzEncoder::new(tmp_output, Default::default()); | ||
let mut tar = tar::Builder::new(enc); | ||
|
||
let root_dir = datastore.root_dir(); | ||
tar.append_path_with_name(root_dir.join(".gitignore"), ".gitignore")?; | ||
tar.append_path_with_name(root_dir.join("datastore.db"), "datastore.db")?; | ||
tar.append_dir_all("blobs", datastore.blobs_dir())?; | ||
let tmp_output = tar.into_inner()?.finish()?; | ||
|
||
tmp_output.persist(output_path)?; | ||
|
||
Ok(()) | ||
}; | ||
|
||
write_tar(&output_path).context("Failed to write tarfile")?; | ||
|
||
info!( | ||
"Exported datastore at {} to {}", | ||
&datastore.root_dir().display(), | ||
output_path.display() | ||
); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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,34 @@ | ||
use super::*; | ||
|
||
#[test] | ||
fn init() { | ||
let scan_env = ScanEnv::new(); | ||
assert_cmd_snapshot!(noseyparker_success!("datastore", "init", "-d", scan_env.dspath())); | ||
} | ||
|
||
/// Create a datastore, export it, extract it, and test that Nosey Parker still sees it as a valid | ||
/// datastore. | ||
#[test] | ||
fn export_empty() { | ||
let scan_env = ScanEnv::new(); | ||
// create datastore | ||
noseyparker_success!("datastore", "init", "-d", scan_env.dspath()); | ||
|
||
// export it | ||
let tgz = scan_env.root.child("export.tgz"); | ||
noseyparker_success!("datastore", "export", "-d", scan_env.dspath(), "-o", tgz.path()); | ||
tgz.assert(predicates::path::is_file()); | ||
|
||
// extract the archive | ||
let extract_dir = scan_env.root.child("export.np"); | ||
std::fs::create_dir(&extract_dir).unwrap(); | ||
|
||
let file = std::fs::File::open(tgz.path()).unwrap(); | ||
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(file)); | ||
archive.unpack(&extract_dir).unwrap(); | ||
|
||
// make sure the extracted datastore still works | ||
assert_cmd_snapshot!(noseyparker_success!("summarize", "-d", extract_dir.path())); | ||
} | ||
|
||
// TODO: add case for exporting to an already-existing output file |
6 changes: 6 additions & 0 deletions
6
...oseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__export_empty-2.snap
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,6 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: stdout | ||
--- | ||
Rule Total Findings Total Matches | ||
─────────────────────────────────────── |
5 changes: 5 additions & 0 deletions
5
...oseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__export_empty-3.snap
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,5 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: stderr | ||
--- | ||
|
5 changes: 5 additions & 0 deletions
5
.../noseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__export_empty.snap
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,5 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: status | ||
--- | ||
exit status: 0 |
5 changes: 5 additions & 0 deletions
5
crates/noseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__init-2.snap
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,5 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: stdout | ||
--- | ||
|
5 changes: 5 additions & 0 deletions
5
crates/noseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__init-3.snap
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,5 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: stderr | ||
--- | ||
|
5 changes: 5 additions & 0 deletions
5
crates/noseyparker-cli/tests/datastore/snapshots/test_noseyparker__datastore__init.snap
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,5 @@ | ||
--- | ||
source: crates/noseyparker-cli/tests/datastore/mod.rs | ||
expression: status | ||
--- | ||
exit status: 0 |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
mod common; | ||
use common::*; | ||
|
||
mod datastore; | ||
mod github; | ||
mod help; | ||
mod report; | ||
|