Skip to content

Commit

Permalink
Add a idns man command.
Browse files Browse the repository at this point in the history
  • Loading branch information
partim committed May 2, 2024
1 parent b8fd622 commit c5c83e4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bytes = "1"
clap = { version = "4", features = ["derive", "unstable-doc"] }
chrono = { version = "0.4.38", features = [ "alloc", "clock" ] }
domain = { git="https://github.com/NLnetLabs/domain.git", branch="message-for-slice", features = ["resolv", "unstable-client-transport"]}
tempfile = "3.1.0"
tokio = { version = "1.33", features = ["rt-multi-thread"] }


55 changes: 55 additions & 0 deletions src/idns/commands/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! The help command of _idns._
use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;
use crate::idns::error::Error;


//------------ Help ----------------------------------------------------------

#[derive(Clone, Debug, clap::Args)]
pub struct Help {
/// The command to show the man page for
#[arg(value_name="COMMAND")]
command: Option<String>,
}

impl Help {
pub fn execute(self) -> Result<(), Error> {
let page = match self.command.as_ref().map(String::as_str) {

Check failure on line 20 in src/idns/commands/help.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

called `.as_ref().map(String::as_str)` on an `Option` value

Check failure on line 20 in src/idns/commands/help.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

called `.as_ref().map(String::as_str)` on an `Option` value

Check failure on line 20 in src/idns/commands/help.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

called `.as_ref().map(String::as_str)` on an `Option` value
None => Self::IDNS_1,
Some("query") => Self::IDNS_QUERY_1,
Some(command) => {
return Err(format!("Unknown command '{}'.", command).into());
}
};

let mut file = NamedTempFile::new().map_err(|err| {
format!(
"Can't display man page: \
Failed to create temporary file: {}.",
err
)
})?;
file.write_all(page).map_err(|err| {
format!(
"Can't display man page: \
Failed to write to temporary file: {}.",
err
)
})?;
let _ = Command::new("man").arg(file.path()).status().map_err(|err| {
format!("Failed to run man: {}", err)
})?;
Ok(())
}
}

impl Help {
const IDNS_1: &'static [u8] = include_bytes!("../../../doc/idns.1");
const IDNS_QUERY_1: &'static [u8] = include_bytes!(
"../../../doc/idns-query.1"
);
}

5 changes: 5 additions & 0 deletions src/idns/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The various commands of _idsn._
pub mod help;
pub mod query;


Expand All @@ -10,12 +11,16 @@ use super::error::Error;
pub enum Commands {
/// Query the DNS.
Query(self::query::Query),

/// Show the manual pages.
Man(self::help::Help),
}

impl Commands {
pub fn execute(self) -> Result<(), Error> {
match self {
Self::Query(query) => query.execute(),
Self::Man(help) => help.execute(),
}
}
}
Expand Down

0 comments on commit c5c83e4

Please sign in to comment.