-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve dispatch for tools in tools package
* Change name of primary binary to stratisd-tools * Update CI * Add struct based dispatch Signed-off-by: mulhern <[email protected]>
- Loading branch information
Showing
8 changed files
with
173 additions
and
98 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
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
mod tools; | ||
|
||
use std::{env, path::Path, process}; | ||
|
||
use clap::{Arg, Command}; | ||
use env_logger::Builder; | ||
|
||
use crate::tools::cmds; | ||
|
||
fn basename(path: &str) -> Option<&Path> { | ||
Path::new(path).file_name().map(Path::new) | ||
} | ||
|
||
/// Configure and initialize the logger. | ||
/// Read log configuration parameters from the environment if RUST_LOG | ||
/// is set. Otherwise, just accept the default configuration, which is | ||
/// to log at the severity of error only. | ||
fn initialize_log() { | ||
let mut builder = Builder::new(); | ||
|
||
if let Ok(s) = env::var("RUST_LOG") { | ||
builder.parse_filters(&s); | ||
} | ||
|
||
builder.init() | ||
} | ||
|
||
fn main() { | ||
initialize_log(); | ||
|
||
let mut command_line_args = env::args().peekable(); | ||
command_line_args.next_if(|p| { | ||
basename(p) | ||
.map(|n| n == Path::new("stratisd-tools")) | ||
.unwrap_or(false) | ||
}); | ||
|
||
let args = command_line_args.collect::<Vec<_>>(); | ||
if args.is_empty() { | ||
let command = Command::new("stratisd-tools").arg( | ||
Arg::new("executable") | ||
.required(true) | ||
.value_name("EXECUTABLE") | ||
.value_parser(cmds().iter().map(|x| x.name()).collect::<Vec<_>>()), | ||
); | ||
command | ||
.get_matches_from(args) | ||
.get_one::<String>("executable"); | ||
unreachable!(); | ||
} | ||
|
||
let argv1 = args[0].as_str(); | ||
|
||
let command_name = match basename(argv1).and_then(|n| n.to_str()) { | ||
Some(name) => name, | ||
None => { | ||
process::exit(1); | ||
} | ||
}; | ||
|
||
if let Some(c) = cmds().iter().find(|x| command_name == x.name()) { | ||
match c.run(args) { | ||
Ok(()) => {} | ||
Err(e) => { | ||
eprintln!("Error encountered: {}", e); | ||
process::exit(1); | ||
} | ||
} | ||
} else { | ||
process::exit(2); | ||
} | ||
} |
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,80 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
use clap::{Arg, ArgAction, Command}; | ||
|
||
use crate::tools::dump_metadata; | ||
|
||
pub trait ToolCommand<'a> { | ||
fn name(&self) -> &'a str; | ||
fn run(&self, command_line_args: Vec<String>) -> Result<(), String>; | ||
} | ||
|
||
struct StratisDumpMetadata; | ||
|
||
impl StratisDumpMetadata { | ||
fn cmd() -> Command { | ||
Command::new("stratis-dumpmetadata") | ||
.next_line_help(true) | ||
.arg( | ||
Arg::new("dev") | ||
.required(true) | ||
.help("Print metadata of given device"), | ||
) | ||
.arg( | ||
Arg::new("print_bytes") | ||
.long("print-bytes") | ||
.action(ArgAction::SetTrue) | ||
.num_args(0) | ||
.short('b') | ||
.help("Print byte buffer of signature block"), | ||
) | ||
.arg( | ||
Arg::new("only") | ||
.long("only") | ||
.action(ArgAction::Set) | ||
.value_name("PORTION") | ||
.value_parser(["pool"]) | ||
.help("Only print specified portion of the metadata"), | ||
) | ||
} | ||
} | ||
|
||
impl<'a> ToolCommand<'a> for StratisDumpMetadata { | ||
fn name(&self) -> &'a str { | ||
"stratis-dumpmetadata" | ||
} | ||
|
||
fn run(&self, command_line_args: Vec<String>) -> Result<(), String> { | ||
let matches = StratisDumpMetadata::cmd().get_matches_from(command_line_args); | ||
let devpath = matches | ||
.get_one::<String>("dev") | ||
.map(|s| s.as_str()) | ||
.expect("'dev' is a mandatory argument"); | ||
|
||
dump_metadata::run( | ||
devpath, | ||
matches.get_flag("print_bytes"), | ||
matches | ||
.get_one::<String>("only") | ||
.map(|v| v == "pool") | ||
.unwrap_or(false), | ||
) | ||
} | ||
} | ||
|
||
pub fn cmds<'a>() -> Vec<Box<dyn ToolCommand<'a>>> { | ||
vec![Box::new(StratisDumpMetadata)] | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::StratisDumpMetadata; | ||
|
||
#[test] | ||
fn test_dumpmetadata_parse_args() { | ||
StratisDumpMetadata::cmd().debug_assert(); | ||
} | ||
} |
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