Server: use option instead of positional arguments

More consistent and easier to debug
pull/428/head
Tpt 2 years ago committed by Thomas Tanon
parent e90d98bb2c
commit beca5e88ca
  1. 51
      server/src/main.rs

@ -103,7 +103,8 @@ enum Command {
#[arg(short, long)] #[arg(short, long)]
location: PathBuf, location: PathBuf,
/// Directory to backup to. /// Directory to backup to.
directory: PathBuf, #[arg(short, long)]
destination: PathBuf,
}, },
/// Load file(s) into the store. /// Load file(s) into the store.
Load { Load {
@ -115,16 +116,14 @@ enum Command {
/// If multiple files are provided they are loaded in parallel. /// If multiple files are provided they are loaded in parallel.
/// ///
/// If no file is given, stdin is read. /// If no file is given, stdin is read.
#[arg(num_args = 0..)] #[arg(short, long, global = true, num_args = 0..)]
file: Vec<PathBuf>, file: Vec<PathBuf>,
#[arg(short = 'f', long = "file", global = true, num_args = 0.., value_name = "FILE", hide = true)]
file_opt: Vec<PathBuf>, //TODO: remove on next breaking release
/// The format of the file(s) to load. /// The format of the file(s) to load.
/// ///
/// Can be an extension like "nt" or a MIME type like "application/n-triples". /// Can be an extension like "nt" or a MIME type like "application/n-triples".
/// ///
/// By default the format is guessed from the loaded file extension. /// By default the format is guessed from the loaded file extension.
#[arg(long, required_unless_present_any = ["file", "file_opt"])] #[arg(long, required_unless_present = "file")]
format: Option<String>, format: Option<String>,
/// Attempt to keep loading even if the data file is invalid. /// Attempt to keep loading even if the data file is invalid.
/// ///
@ -147,6 +146,7 @@ enum Command {
/// File to dump to. /// File to dump to.
/// ///
/// If no file is given, stdout is used. /// If no file is given, stdout is used.
#[arg(short, long)]
file: Option<PathBuf>, file: Option<PathBuf>,
/// The format of the file(s) to dump. /// The format of the file(s) to dump.
/// ///
@ -193,15 +193,14 @@ pub fn main() -> anyhow::Result<()> {
), ),
Command::Backup { Command::Backup {
location, location,
directory, destination,
} => { } => {
let store = Store::open_read_only(location)?; let store = Store::open_read_only(location)?;
store.backup(directory)?; store.backup(destination)?;
Ok(()) Ok(())
} }
Command::Load { Command::Load {
mut file, file,
file_opt,
location, location,
lenient, lenient,
format, format,
@ -213,10 +212,6 @@ pub fn main() -> anyhow::Result<()> {
eprintln!("Warning: opening an in-memory store. It will not be possible to read the written data."); eprintln!("Warning: opening an in-memory store. It will not be possible to read the written data.");
Store::new() Store::new()
}?; }?;
if !file_opt.is_empty() {
eprintln!("Warning: the --file option is deprecated. Please use a positional argument instead");
file.extend(file_opt);
}
let format = if let Some(format) = format { let format = if let Some(format) = format {
Some(GraphOrDatasetFormat::from_str(&format)?) Some(GraphOrDatasetFormat::from_str(&format)?)
} else { } else {
@ -1413,6 +1408,7 @@ mod tests {
.arg("load") .arg("load")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--file")
.arg(input_file.path()) .arg(input_file.path())
.assert() .assert()
.success(); .success();
@ -1422,6 +1418,7 @@ mod tests {
.arg("dump") .arg("dump")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--file")
.arg(output_file.path()) .arg(output_file.path())
.arg("--graph") .arg("--graph")
.arg("default") .arg("default")
@ -1442,6 +1439,7 @@ mod tests {
.arg("load") .arg("load")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--file")
.arg(input_file.path()) .arg(input_file.path())
.assert() .assert()
.success(); .success();
@ -1451,6 +1449,7 @@ mod tests {
.arg("dump") .arg("dump")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--file")
.arg(output_file.path()) .arg(output_file.path())
.assert() .assert()
.success(); .success();
@ -1461,6 +1460,7 @@ mod tests {
#[test] #[test]
fn cli_load_gzip_dataset() -> Result<()> { fn cli_load_gzip_dataset() -> Result<()> {
let store_dir = TempDir::new()?;
let file = NamedTempFile::new("sample.nq.gz")?; let file = NamedTempFile::new("sample.nq.gz")?;
let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder encoder
@ -1468,9 +1468,22 @@ mod tests {
file.write_binary(&encoder.finish()?)?; file.write_binary(&encoder.finish()?)?;
cli_command()? cli_command()?
.arg("load") .arg("load")
.arg("-l")
.arg(store_dir.path())
.arg("-f")
.arg(file.path()) .arg(file.path())
.assert() .assert()
.success(); .success();
cli_command()?
.arg("dump")
.arg("-l")
.arg(store_dir.path())
.arg("--format")
.arg("nq")
.assert()
.success()
.stdout("<http://example.com/s> <http://example.com/p> <http://example.com/o> .\n");
Ok(()) Ok(())
} }
@ -1478,11 +1491,12 @@ mod tests {
fn cli_load_and_dump_named_graph() -> Result<()> { fn cli_load_and_dump_named_graph() -> Result<()> {
let store_dir = TempDir::new()?; let store_dir = TempDir::new()?;
let input_file = NamedTempFile::new("input.nt")?; let input_file = NamedTempFile::new("input.nt")?;
input_file input_file.write_str(
.write_str("<http://example.com/s> <http://example.com/p> <http://example.com/o> .")?; "<http://example.com/s> <http://example.com/p> <http://example.com/o> .\n",
)?;
cli_command()? cli_command()?
.arg("load") .arg("load")
.arg("--location") .arg("-l")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("-f") .arg("-f")
.arg(input_file.path()) .arg(input_file.path())
@ -1494,8 +1508,9 @@ mod tests {
let output_file = NamedTempFile::new("output.nt")?; let output_file = NamedTempFile::new("output.nt")?;
cli_command()? cli_command()?
.arg("dump") .arg("dump")
.arg("--location") .arg("-l")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("-f")
.arg(output_file.path()) .arg(output_file.path())
.arg("--graph") .arg("--graph")
.arg("http://example.com/g") .arg("http://example.com/g")
@ -1528,6 +1543,7 @@ mod tests {
.arg("dump") .arg("dump")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--file")
.arg(output_file.path()) .arg(output_file.path())
.arg("--graph") .arg("--graph")
.arg("default") .arg("default")
@ -1584,6 +1600,7 @@ mod tests {
.arg("backup") .arg("backup")
.arg("--location") .arg("--location")
.arg(store_dir.path()) .arg(store_dir.path())
.arg("--destination")
.arg(backup_dir.path()) .arg(backup_dir.path())
.assert() .assert()
.success(); .success();

Loading…
Cancel
Save