chcon: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 15:24:52 +02:00
parent 717402b46a
commit 7a3cb35352
2 changed files with 41 additions and 35 deletions

View file

@ -14,7 +14,7 @@ edition = "2021"
path = "src/chcon.rs" path = "src/chcon.rs"
[dependencies] [dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version = ">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } uucore = { version = ">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] }
selinux = { version = "0.3" } selinux = { version = "0.3" }
fts-sys = { version = "0.2" } fts-sys = { version = "0.2" }

View file

@ -7,7 +7,7 @@ use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::format_usage; use uucore::format_usage;
use uucore::{display::Quotable, show_error, show_warning}; use uucore::{display::Quotable, show_error, show_warning};
use clap::{Arg, Command}; use clap::{Arg, ArgAction, Command};
use selinux::{OpaqueSecurityContext, SecurityContext}; use selinux::{OpaqueSecurityContext, SecurityContext};
use std::borrow::Cow; use std::borrow::Cow;
@ -68,7 +68,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Err(r) => { Err(r) => {
if let Error::CommandLine(r) = &r { if let Error::CommandLine(r) = &r {
match r.kind() { match r.kind() {
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => { clap::error::ErrorKind::DisplayHelp
| clap::error::ErrorKind::DisplayVersion => {
println!("{}", r); println!("{}", r);
return Ok(()); return Ok(());
} }
@ -106,7 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} }
CommandLineMode::ContextBased { context } => { CommandLineMode::ContextBased { context } => {
let c_context = match os_str_to_c_string(context) { let c_context = match os_str_to_c_string(&context) {
Ok(context) => context, Ok(context) => context,
Err(_r) => { Err(_r) => {
@ -156,7 +157,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Err(libc::EXIT_FAILURE.into()) Err(libc::EXIT_FAILURE.into())
} }
pub fn uu_app<'a>() -> Command<'a> { pub fn uu_app() -> Command {
Command::new(uucore::util_name()) Command::new(uucore::util_name())
.version(VERSION) .version(VERSION)
.about(ABOUT) .about(ABOUT)
@ -165,7 +166,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.arg( .arg(
Arg::new(options::HELP) Arg::new(options::HELP)
.long(options::HELP) .long(options::HELP)
.help("Print help information."), .help("Print help information.")
.action(ArgAction::Help),
) )
.arg( .arg(
Arg::new(options::dereference::DEREFERENCE) Arg::new(options::dereference::DEREFERENCE)
@ -174,29 +176,32 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Affect the referent of each symbolic link (this is the default), \ "Affect the referent of each symbolic link (this is the default), \
rather than the symbolic link itself.", rather than the symbolic link itself.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::dereference::NO_DEREFERENCE) Arg::new(options::dereference::NO_DEREFERENCE)
.short('h') .short('h')
.long(options::dereference::NO_DEREFERENCE) .long(options::dereference::NO_DEREFERENCE)
.help("Affect symbolic links instead of any referenced file."), .help("Affect symbolic links instead of any referenced file.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::preserve_root::PRESERVE_ROOT) Arg::new(options::preserve_root::PRESERVE_ROOT)
.long(options::preserve_root::PRESERVE_ROOT) .long(options::preserve_root::PRESERVE_ROOT)
.conflicts_with(options::preserve_root::NO_PRESERVE_ROOT) .conflicts_with(options::preserve_root::NO_PRESERVE_ROOT)
.help("Fail to operate recursively on '/'."), .help("Fail to operate recursively on '/'.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::preserve_root::NO_PRESERVE_ROOT) Arg::new(options::preserve_root::NO_PRESERVE_ROOT)
.long(options::preserve_root::NO_PRESERVE_ROOT) .long(options::preserve_root::NO_PRESERVE_ROOT)
.help("Do not treat '/' specially (the default)."), .help("Do not treat '/' specially (the default).")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::REFERENCE) Arg::new(options::REFERENCE)
.long(options::REFERENCE) .long(options::REFERENCE)
.takes_value(true)
.value_name("RFILE") .value_name("RFILE")
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.conflicts_with_all(&[options::USER, options::ROLE, options::TYPE, options::RANGE]) .conflicts_with_all(&[options::USER, options::ROLE, options::TYPE, options::RANGE])
@ -210,7 +215,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::USER) Arg::new(options::USER)
.short('u') .short('u')
.long(options::USER) .long(options::USER)
.takes_value(true)
.value_name("USER") .value_name("USER")
.value_hint(clap::ValueHint::Username) .value_hint(clap::ValueHint::Username)
.help("Set user USER in the target security context.") .help("Set user USER in the target security context.")
@ -220,7 +224,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::ROLE) Arg::new(options::ROLE)
.short('r') .short('r')
.long(options::ROLE) .long(options::ROLE)
.takes_value(true)
.value_name("ROLE") .value_name("ROLE")
.help("Set role ROLE in the target security context.") .help("Set role ROLE in the target security context.")
.value_parser(ValueParser::os_string()), .value_parser(ValueParser::os_string()),
@ -229,7 +232,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::TYPE) Arg::new(options::TYPE)
.short('t') .short('t')
.long(options::TYPE) .long(options::TYPE)
.takes_value(true)
.value_name("TYPE") .value_name("TYPE")
.help("Set type TYPE in the target security context.") .help("Set type TYPE in the target security context.")
.value_parser(ValueParser::os_string()), .value_parser(ValueParser::os_string()),
@ -238,7 +240,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::RANGE) Arg::new(options::RANGE)
.short('l') .short('l')
.long(options::RANGE) .long(options::RANGE)
.takes_value(true)
.value_name("RANGE") .value_name("RANGE")
.help("Set range RANGE in the target security context.") .help("Set range RANGE in the target security context.")
.value_parser(ValueParser::os_string()), .value_parser(ValueParser::os_string()),
@ -247,7 +248,8 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::RECURSIVE) Arg::new(options::RECURSIVE)
.short('R') .short('R')
.long(options::RECURSIVE) .long(options::RECURSIVE)
.help("Operate on files and directories recursively."), .help("Operate on files and directories recursively.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) Arg::new(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK)
@ -260,7 +262,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"If a command line argument is a symbolic link to a directory, \ "If a command line argument is a symbolic link to a directory, \
traverse it. Only valid when -R is specified.", traverse it. Only valid when -R is specified.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::sym_links::FOLLOW_DIR_SYM_LINKS) Arg::new(options::sym_links::FOLLOW_DIR_SYM_LINKS)
@ -273,7 +276,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Traverse every symbolic link to a directory encountered. \ "Traverse every symbolic link to a directory encountered. \
Only valid when -R is specified.", Only valid when -R is specified.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::sym_links::NO_FOLLOW_SYM_LINKS) Arg::new(options::sym_links::NO_FOLLOW_SYM_LINKS)
@ -286,19 +290,21 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Do not traverse any symbolic links (default). \ "Do not traverse any symbolic links (default). \
Only valid when -R is specified.", Only valid when -R is specified.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::VERBOSE) Arg::new(options::VERBOSE)
.short('v') .short('v')
.long(options::VERBOSE) .long(options::VERBOSE)
.help("Output a diagnostic for every file processed."), .help("Output a diagnostic for every file processed.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new("FILE") Arg::new("FILE")
.multiple_occurrences(true) .action(ArgAction::Append)
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.min_values(1) .num_args(1..)
.value_parser(ValueParser::os_string()), .value_parser(ValueParser::os_string()),
) )
} }
@ -316,11 +322,11 @@ struct Options {
fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<Options> { fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<Options> {
let matches = config.try_get_matches_from(args)?; let matches = config.try_get_matches_from(args)?;
let verbose = matches.contains_id(options::VERBOSE); let verbose = matches.get_flag(options::VERBOSE);
let (recursive_mode, affect_symlink_referent) = if matches.contains_id(options::RECURSIVE) { let (recursive_mode, affect_symlink_referent) = if matches.get_flag(options::RECURSIVE) {
if matches.contains_id(options::sym_links::FOLLOW_DIR_SYM_LINKS) { if matches.get_flag(options::sym_links::FOLLOW_DIR_SYM_LINKS) {
if matches.contains_id(options::dereference::NO_DEREFERENCE) { if matches.get_flag(options::dereference::NO_DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!( return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require '-P'", "'--{}' with '--{}' require '-P'",
options::RECURSIVE, options::RECURSIVE,
@ -329,8 +335,8 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
} }
(RecursiveMode::RecursiveAndFollowAllDirSymLinks, true) (RecursiveMode::RecursiveAndFollowAllDirSymLinks, true)
} else if matches.contains_id(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) { } else if matches.get_flag(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) {
if matches.contains_id(options::dereference::NO_DEREFERENCE) { if matches.get_flag(options::dereference::NO_DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!( return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require '-P'", "'--{}' with '--{}' require '-P'",
options::RECURSIVE, options::RECURSIVE,
@ -340,7 +346,7 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
(RecursiveMode::RecursiveAndFollowArgDirSymLinks, true) (RecursiveMode::RecursiveAndFollowArgDirSymLinks, true)
} else { } else {
if matches.contains_id(options::dereference::DEREFERENCE) { if matches.get_flag(options::dereference::DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!( return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require either '-H' or '-L'", "'--{}' with '--{}' require either '-H' or '-L'",
options::RECURSIVE, options::RECURSIVE,
@ -351,12 +357,12 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
(RecursiveMode::RecursiveButDoNotFollowSymLinks, false) (RecursiveMode::RecursiveButDoNotFollowSymLinks, false)
} }
} else { } else {
let no_dereference = matches.contains_id(options::dereference::NO_DEREFERENCE); let no_dereference = matches.get_flag(options::dereference::NO_DEREFERENCE);
(RecursiveMode::NotRecursive, !no_dereference) (RecursiveMode::NotRecursive, !no_dereference)
}; };
// By default, do not preserve root. // By default, do not preserve root.
let preserve_root = matches.contains_id(options::preserve_root::PRESERVE_ROOT); let preserve_root = matches.get_flag(options::preserve_root::PRESERVE_ROOT);
let mut files = matches.get_many::<OsString>("FILE").unwrap_or_default(); let mut files = matches.get_many::<OsString>("FILE").unwrap_or_default();
@ -364,10 +370,10 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
CommandLineMode::ReferenceBased { CommandLineMode::ReferenceBased {
reference: PathBuf::from(path), reference: PathBuf::from(path),
} }
} else if matches.contains_id(options::USER) } else if matches.get_flag(options::USER)
|| matches.contains_id(options::ROLE) || matches.get_flag(options::ROLE)
|| matches.contains_id(options::TYPE) || matches.get_flag(options::TYPE)
|| matches.contains_id(options::RANGE) || matches.get_flag(options::RANGE)
{ {
CommandLineMode::Custom { CommandLineMode::Custom {
user: matches.get_one::<OsString>(options::USER).map(Into::into), user: matches.get_one::<OsString>(options::USER).map(Into::into),