-
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.
Merge pull request #3530 from mulkieran/issue_stratisd_3529
Tidy up stratis-dumpmetadata
- Loading branch information
Showing
8 changed files
with
186 additions
and
91 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 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,84 @@ | ||
// 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 executable_name = "stratisd-tools"; | ||
|
||
let args = env::args().collect::<Vec<_>>(); | ||
let argv1 = &args[0]; | ||
|
||
let stripped_args = if basename(argv1.as_str()) | ||
.map(|n| n == Path::new(executable_name)) | ||
.unwrap_or(false) | ||
{ | ||
let command = Command::new(executable_name) | ||
.arg( | ||
Arg::new("executable") | ||
.required(true) | ||
.value_name("EXECUTABLE") | ||
.value_parser(cmds().iter().map(|x| x.name()).collect::<Vec<_>>()), | ||
) | ||
.arg_required_else_help(true); | ||
|
||
let truncated_args = if args.len() > 1 { | ||
vec![argv1, &args[1]] | ||
} else { | ||
vec![argv1] | ||
}; | ||
|
||
command.get_matches_from(truncated_args); | ||
args[1..].to_vec() | ||
} else { | ||
args | ||
}; | ||
|
||
let command_name = match basename(&stripped_args[0]).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(stripped_args) { | ||
Ok(()) => {} | ||
Err(e) => { | ||
eprintln!("Error encountered: {e}"); | ||
process::exit(1); | ||
} | ||
} | ||
} else { | ||
eprintln!("Unknown executable name {command_name}"); | ||
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
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,8 @@ | ||
// 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 cmds; | ||
pub mod dump_metadata; | ||
|
||
pub use cmds::cmds; |