-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display embedded man pages for built-in commands. #8456
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use flate2::{Compression, GzBuilder}; | ||
use std::ffi::OsStr; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
compress_man(); | ||
} | ||
|
||
fn compress_man() { | ||
let out_path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("man.tgz"); | ||
let dst = fs::File::create(out_path).unwrap(); | ||
let encoder = GzBuilder::new() | ||
.filename("man.tar") | ||
.write(dst, Compression::best()); | ||
let mut ar = tar::Builder::new(encoder); | ||
|
||
let mut add_files = |dir, extension| { | ||
for entry in fs::read_dir(dir).unwrap() { | ||
let path = entry.unwrap().path(); | ||
if path.extension() != Some(extension) { | ||
continue; | ||
} | ||
println!("cargo:rerun-if-changed={}", path.display()); | ||
ar.append_path_with_name(&path, path.file_name().unwrap()) | ||
.unwrap(); | ||
} | ||
}; | ||
|
||
add_files(Path::new("src/etc/man"), OsStr::new("1")); | ||
add_files(Path::new("src/doc/man/generated_txt"), OsStr::new("txt")); | ||
let encoder = ar.into_inner().unwrap(); | ||
encoder.finish().unwrap(); | ||
} |
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
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,130 @@ | ||
use crate::aliased_command; | ||
use cargo::util::errors::CargoResult; | ||
use cargo::util::paths::resolve_executable; | ||
use cargo::Config; | ||
use flate2::read::GzDecoder; | ||
use std::ffi::OsString; | ||
use std::io::Read; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz")); | ||
|
||
/// Checks if the `help` command is being issued. | ||
/// | ||
/// This runs before clap processing, because it needs to intercept the `help` | ||
/// command if a man page is available. | ||
/// | ||
/// Returns `true` if a man page was displayed. In this case, Cargo should | ||
/// exit. | ||
pub fn handle_embedded_help(config: &Config) -> bool { | ||
match try_help(config) { | ||
Ok(true) => true, | ||
Ok(false) => false, | ||
Err(e) => { | ||
log::warn!("man failed: {:?}", e); | ||
false | ||
} | ||
} | ||
} | ||
|
||
fn try_help(config: &Config) -> CargoResult<bool> { | ||
let mut args = std::env::args_os() | ||
.skip(1) | ||
.skip_while(|arg| arg.to_str().map_or(false, |s| s.starts_with('-'))); | ||
if !args | ||
.next() | ||
.map_or(false, |arg| arg.to_str() == Some("help")) | ||
{ | ||
return Ok(false); | ||
} | ||
let subcommand = match args.next() { | ||
Some(arg) => arg, | ||
None => return Ok(false), | ||
}; | ||
let subcommand = match subcommand.to_str() { | ||
Some(s) => s, | ||
None => return Ok(false), | ||
}; | ||
// Check if this is a built-in command (or alias); | ||
let subcommand = match check_alias(config, subcommand) { | ||
Some(s) => s, | ||
None => return Ok(false), | ||
}; | ||
if resolve_executable(Path::new("man")).is_ok() { | ||
let man = match extract_man(&subcommand, "1") { | ||
Some(man) => man, | ||
None => return Ok(false), | ||
}; | ||
write_and_spawn(&man, "man")?; | ||
} else { | ||
let txt = match extract_man(&subcommand, "txt") { | ||
Some(txt) => txt, | ||
None => return Ok(false), | ||
}; | ||
if resolve_executable(Path::new("less")).is_ok() { | ||
write_and_spawn(&txt, "less")?; | ||
} else if resolve_executable(Path::new("more")).is_ok() { | ||
write_and_spawn(&txt, "more")?; | ||
} else { | ||
drop(std::io::stdout().write_all(&txt)); | ||
} | ||
} | ||
Ok(true) | ||
} | ||
|
||
/// Checks if the given subcommand is a built-in command (possibly via an alias). | ||
/// | ||
/// Returns None if it is not a built-in command. | ||
fn check_alias(config: &Config, subcommand: &str) -> Option<String> { | ||
if super::builtin_exec(subcommand).is_some() { | ||
return Some(subcommand.to_string()); | ||
} | ||
match aliased_command(config, subcommand) { | ||
Ok(Some(alias)) => { | ||
let alias = alias.into_iter().next()?; | ||
if super::builtin_exec(&alias).is_some() { | ||
Some(alias) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Extracts the given man page from the compressed archive. | ||
/// | ||
/// Returns None if the command wasn't found. | ||
fn extract_man(subcommand: &str, extension: &str) -> Option<Vec<u8>> { | ||
let extract_name = OsString::from(format!("cargo-{}.{}", subcommand, extension)); | ||
let gz = GzDecoder::new(COMPRESSED_MAN); | ||
let mut ar = tar::Archive::new(gz); | ||
// Unwraps should be safe here, since this is a static archive generated | ||
// by our build script. It should never be an invalid format! | ||
for entry in ar.entries().unwrap() { | ||
let mut entry = entry.unwrap(); | ||
let path = entry.path().unwrap(); | ||
if path.file_name().unwrap() != extract_name { | ||
continue; | ||
} | ||
let mut result = Vec::new(); | ||
entry.read_to_end(&mut result).unwrap(); | ||
return Some(result); | ||
} | ||
None | ||
} | ||
|
||
/// Write the contents of a man page to disk and spawn the given command to | ||
/// display it. | ||
fn write_and_spawn(contents: &[u8], command: &str) -> CargoResult<()> { | ||
let mut tmp = tempfile::Builder::new().prefix("cargo-man").tempfile()?; | ||
let f = tmp.as_file_mut(); | ||
f.write_all(&contents)?; | ||
f.flush()?; | ||
let mut cmd = std::process::Command::new(command) | ||
.arg(tmp.path()) | ||
.spawn()?; | ||
drop(cmd.wait()); | ||
Ok(()) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the tempfile required here? Programs like
less
I think we can just write everything to a pipe?I'm not actually sure what other common CLI apps do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, AFAIK, the
man
program does not support piped content.