Skip to content

Commit

Permalink
Improve dispatch for tools in tools package
Browse files Browse the repository at this point in the history
* Change name of primary binary to stratisd-tools
* Update CI
* Add struct based dispatch

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jan 9, 2024
1 parent 689c0d0 commit d3868e9
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fedora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- task: PROFILEDIR=debug make -f Makefile build-no-ipc
toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
components: cargo
- task: PROFILEDIR=debug make -f Makefile stratis-dumpmetadata
- task: PROFILEDIR=debug make -f Makefile stratisd-tools
toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
components: cargo
- task: make -f Makefile docs-ci
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- task: PROFILEDIR=debug make -f Makefile build-no-ipc
toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
components: cargo
- task: PROFILEDIR=debug make -f Makefile stratis-dumpmetadata
- task: PROFILEDIR=debug make -f Makefile stratisd-tools
toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
components: cargo
- task: make -f Makefile docs-ci
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ name = "stratisd"
required-features = ["engine"]

[[bin]]
name = "stratis-dumpmetadata"
name = "stratisd-tools"
required-features = ["engine", "extras", "min"]

[[bin]]
Expand Down
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ audit-all-rust: build-all-rust
./target/${PROFILEDIR}/stratis-utils \
./target/${PROFILEDIR}/stratis-str-cmp \
./target/${PROFILEDIR}/stratis-base32-decode \
./target/${PROFILEDIR}/stratis-dumpmetadata
./target/${PROFILEDIR}/stratisd-tools

## Check for spelling errors
check-typos:
Expand Down Expand Up @@ -253,12 +253,12 @@ build-stratis-base32-decode:
# so we use two distinct targets to build the two binaries
build-udev-utils: build-stratis-str-cmp build-stratis-base32-decode

## Build the stratis-dumpmetadata program
stratis-dumpmetadata:
## Build the stratisd-tools program
stratisd-tools:
PKG_CONFIG_ALLOW_CROSS=1 \
RUSTFLAGS="${DENY}" \
cargo ${BUILD} ${RELEASE_FLAG} \
--bin=stratis-dumpmetadata ${EXTRAS_FEATURES} ${TARGET_ARGS}
--bin=stratisd-tools ${EXTRAS_FEATURES} ${TARGET_ARGS}

## Build stratis-min for early userspace
stratis-min:
Expand Down Expand Up @@ -314,8 +314,11 @@ install-binaries:
mkdir -p $(DESTDIR)$(BINDIR)
mkdir -p $(DESTDIR)$(UNITGENDIR)
$(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-min

$(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratisd-tools
ln --force --verbose $(DESTDIR)$(BINDIR)/stratisd-tools $(DESTDIR)$(BINDIR)/stratis-dumpmetadata

$(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-utils
$(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-dumpmetadata
mv --force --verbose $(DESTDIR)$(BINDIR)/stratis-utils $(DESTDIR)$(BINDIR)/stratis-predict-usage
ln --force --verbose $(DESTDIR)$(BINDIR)/stratis-predict-usage $(DESTDIR)$(UNITGENDIR)/stratis-clevis-setup-generator
ln --force --verbose $(DESTDIR)$(BINDIR)/stratis-predict-usage $(DESTDIR)$(UNITGENDIR)/stratis-setup-generator
Expand All @@ -341,7 +344,7 @@ install-daemons:
install: install-udev-cfg install-man-cfg install-dbus-cfg install-dracut-cfg install-systemd-cfg install-binaries install-udev-binaries install-fstab-script install-daemons

## Build all Rust artifacts
build-all-rust: build build-min build-udev-utils stratis-dumpmetadata
build-all-rust: build build-min build-udev-utils stratisd-tools

## Build all man pages
build-all-man: docs/stratisd.8 docs/stratis-dumpmetadata.8
Expand All @@ -366,6 +369,7 @@ clean-ancillary:
rm -fv $(DESTDIR)$(UDEVDIR)/stratis-str-cmp
rm -fv $(DESTDIR)$(UDEVDIR)/stratis-base32-decode
rm -fv $(DESTDIR)$(BINDIR)/stratis-predict-usage
rm -fv $(DESTDIR)$(BINDIR)/stratisd-tools
rm -fv $(DESTDIR)$(BINDIR)/stratis-dumpmetadata
rm -fv $(DESTDIR)$(UNITGENDIR)/stratis-setup-generator
rm -fv $(DESTDIR)$(UNITGENDIR)/stratis-clevis-setup-generator
Expand Down Expand Up @@ -506,7 +510,7 @@ clippy: clippy-macros clippy-min clippy-udev-utils clippy-no-ipc
install-binaries
install-daemons
install-dbus-cfg
install-dracut-cfg
sinstall-dracut-cfg
install-fstab-script
install-man-cfg
install-systemd-cfg
Expand Down
88 changes: 0 additions & 88 deletions src/bin/stratis-dumpmetadata.rs

This file was deleted.

76 changes: 76 additions & 0 deletions src/bin/stratisd-tools.rs
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);
}
}
80 changes: 80 additions & 0 deletions src/bin/tools/cmds.rs
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();
}
}
3 changes: 3 additions & 0 deletions src/bin/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
// 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;

0 comments on commit d3868e9

Please sign in to comment.