Skip to content
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

cp: compile without clap (for nushell) #5358

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
733066c
let cp can without clap dep when no-default-features flag is specific
tommady Oct 5, 2023
1955616
let uucore clap can be optional
tommady Oct 5, 2023
b29b31f
let cp can optional out uucore clap
tommady Oct 5, 2023
2f68fd4
let clap dep could be optional in the uucore
tommady Oct 5, 2023
fe47bf0
fix date is using the uucore clap
tommady Oct 5, 2023
4cb66c4
Update src/uucore/src/lib/mods/error.rs
tommady Oct 5, 2023
d867b1b
use dep:clap instead of just a clap to mark this feature is depends o…
tommady Oct 6, 2023
4362184
fix-5203 but cargo tree --no-default uucore still has clap dep
tommady Oct 6, 2023
9f0f0a9
Merge branch 'fix-5203' of github.com:tommady/coreutils into fix-5203
tommady Oct 6, 2023
af8690d
fixing clippy and check errors
tommady Oct 7, 2023
968e901
let cp can without clap dep when no-default-features flag is specific
tommady Oct 5, 2023
fea371a
let uucore clap can be optional
tommady Oct 5, 2023
27e2dc4
let cp can optional out uucore clap
tommady Oct 5, 2023
ee93c10
let clap dep could be optional in the uucore
tommady Oct 5, 2023
33db724
fix date is using the uucore clap
tommady Oct 5, 2023
e28c71d
Update src/uucore/src/lib/mods/error.rs
tommady Oct 5, 2023
243fa6a
use dep:clap instead of just a clap to mark this feature is depends o…
tommady Oct 6, 2023
51cf10f
fix-5203 but cargo tree --no-default uucore still has clap dep
tommady Oct 6, 2023
c08de95
fixing clippy and check errors
tommady Oct 7, 2023
20735c9
cp: fix formatting of Cargo.toml
cakebaker Oct 15, 2023
10f5d98
remove temp unused allow(dead_code)
tommady Oct 16, 2023
e38bcf3
Merge branch 'fix-5203' of github.com:tommady/coreutils into fix-5203
tommady Oct 16, 2023
fea1a2e
remove temp unused allow(dead_code)
tommady Oct 16, 2023
7c2adb5
fix the style toml check
tommady Oct 16, 2023
a54dd0e
Merge branch 'main' into fix-5203
tommady Oct 16, 2023
7f5d90a
cp: fix formatting of Cargo.toml
cakebaker Oct 17, 2023
8ca000e
Merge branch 'main' into fix-5203
tommady Dec 26, 2023
8efa790
fix cargo fmt
tommady Dec 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ blake3 = "1.5.0"
sm3 = "0.4.2"
digest = "0.10.7"

uucore = { version = ">=0.0.19", package = "uucore", path = "src/uucore" }
uucore = { version = ">=0.0.19", package = "uucore", path = "src/uucore", default-features = false }
uucore_procs = { version = ">=0.0.19", package = "uucore_procs", path = "src/uucore_procs" }
uu_ls = { version = ">=0.0.18", path = "src/uu/ls" }
uu_base32 = { version = ">=0.0.18", path = "src/uu/base32" }
Expand Down
20 changes: 13 additions & 7 deletions src/uu/cp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ edition = "2021"
path = "src/cp.rs"

[dependencies]
clap = { workspace = true }
clap = { workspace = true, optional = true }
filetime = { workspace = true }
libc = { workspace = true }
quick-error = { workspace = true }
selinux = { workspace = true, optional = true }
uucore = { workspace = true, features = [
"backup-control",
walkdir = { workspace = true }
indicatif = { workspace = true }

[dependencies.uucore]
workspace = true
default-features = false
features = [
"entries",
"fs",
"perms",
"mode",
"perms",
"update-control",
] }
walkdir = { workspace = true }
indicatif = { workspace = true }
"backup-control",
]

[target.'cfg(unix)'.dependencies]
xattr = { workspace = true }
Expand All @@ -44,5 +48,7 @@ name = "cp"
path = "src/main.rs"

[features]
default = ["cli-parser"]
feat_selinux = ["selinux"]
feat_acl = ["exacl"]
cli-parser = ["dep:clap", "uucore/cli-parser"]
32 changes: 22 additions & 10 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use quick_error::quick_error;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::env;
#[cfg(not(windows))]
use std::ffi::CString;
use std::fs::{self, File, OpenOptions};
Expand All @@ -22,6 +21,7 @@ use std::os::unix::fs::{FileTypeExt, PermissionsExt};
use std::path::{Path, PathBuf, StripPrefixError};
use std::string::ToString;

#[cfg(feature = "cli-parser")]
use clap::{builder::ValueParser, crate_version, Arg, ArgAction, ArgMatches, Command};
use filetime::FileTime;
use indicatif::{ProgressBar, ProgressStyle};
Expand All @@ -31,7 +31,9 @@ use quick_error::ResultExt;

use platform::copy_on_write;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
use uucore::error::UError;
#[cfg(feature = "cli-parser")]
use uucore::error::{set_exit_code, UClapError, UResult, UUsageError};
use uucore::fs::{
canonicalize, is_symlink_loop, paths_refer_to_same_file, FileInformation, MissingHandling,
ResolveMode,
Expand All @@ -40,10 +42,9 @@ use uucore::{backup_control, update_control};
// These are exposed for projects (e.g. nushell) that want to create an `Options` value, which
// requires these enum.
pub use uucore::{backup_control::BackupMode, update_control::UpdateMode};
use uucore::{
crash, format_usage, help_about, help_section, help_usage, prompt_yes, show_error,
show_warning, util_name,
};
use uucore::{crash, prompt_yes, show_error, show_warning, util_name};
#[cfg(feature = "cli-parser")]
use uucore::{format_usage, help_about, help_section, help_usage};

use crate::copydir::copy_directory;

Expand Down Expand Up @@ -350,13 +351,10 @@ fn show_debug(copy_debug: &CopyDebug) {
);
}

const ABOUT: &str = help_about!("cp.md");
const USAGE: &str = help_usage!("cp.md");
const AFTER_HELP: &str = help_section!("after help", "cp.md");

static EXIT_ERR: i32 = 1;

// Argument constants
#[cfg(feature = "cli-parser")]
mod options {
pub const ARCHIVE: &str = "archive";
pub const ATTRIBUTES_ONLY: &str = "attributes-only";
Expand Down Expand Up @@ -391,6 +389,7 @@ mod options {
}

#[cfg(unix)]
#[allow(dead_code)]
static PRESERVABLE_ATTRIBUTES: &[&str] = &[
tommady marked this conversation as resolved.
Show resolved Hide resolved
"mode",
"ownership",
Expand All @@ -403,6 +402,7 @@ static PRESERVABLE_ATTRIBUTES: &[&str] = &[
];

#[cfg(not(unix))]
#[allow(dead_code)]
static PRESERVABLE_ATTRIBUTES: &[&str] = &[
"mode",
"timestamps",
Expand All @@ -413,6 +413,7 @@ static PRESERVABLE_ATTRIBUTES: &[&str] = &[
"all",
];

#[cfg(feature = "cli-parser")]
pub fn uu_app() -> Command {
const MODE_ARGS: &[&str] = &[
options::LINK,
Expand All @@ -421,6 +422,10 @@ pub fn uu_app() -> Command {
options::ATTRIBUTES_ONLY,
options::COPY_CONTENTS,
];
const ABOUT: &str = help_about!("cp.md");
const USAGE: &str = help_usage!("cp.md");
const AFTER_HELP: &str = help_section!("after help", "cp.md");

Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
Expand Down Expand Up @@ -696,6 +701,7 @@ pub fn uu_app() -> Command {
)
}

#[cfg(feature = "cli-parser")]
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args);
Expand Down Expand Up @@ -744,6 +750,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

impl ClobberMode {
#[cfg(feature = "cli-parser")]
fn from_matches(matches: &ArgMatches) -> Self {
if matches.get_flag(options::FORCE) {
Self::Force
Expand All @@ -756,6 +763,7 @@ impl ClobberMode {
}

impl OverwriteMode {
#[cfg(feature = "cli-parser")]
fn from_matches(matches: &ArgMatches) -> Self {
if matches.get_flag(options::INTERACTIVE) {
Self::Interactive(ClobberMode::from_matches(matches))
Expand All @@ -768,6 +776,7 @@ impl OverwriteMode {
}

impl CopyMode {
#[cfg(feature = "cli-parser")]
fn from_matches(matches: &ArgMatches) -> Self {
if matches.get_flag(options::LINK) {
Self::Link
Expand Down Expand Up @@ -887,6 +896,7 @@ impl Attributes {
}

impl Options {
#[cfg(feature = "cli-parser")]
#[allow(clippy::cognitive_complexity)]
fn from_matches(matches: &ArgMatches) -> CopyResult<Self> {
let not_implemented_opts = vec![
Expand Down Expand Up @@ -1076,6 +1086,7 @@ impl TargetType {
}

/// Returns tuple of (Source paths, Target)
#[cfg(feature = "cli-parser")]
fn parse_path_args(
mut paths: Vec<PathBuf>,
options: &Options,
Expand Down Expand Up @@ -1323,6 +1334,7 @@ pub(crate) fn copy_attributes(

// Ownership must be changed first to avoid interfering with mode change.
#[cfg(unix)]
#[cfg(feature = "cli-parser")]
handle_preserve(&attributes.ownership, || -> CopyResult<()> {
use std::os::unix::prelude::MetadataExt;
use uucore::perms::wrap_chown;
Expand Down
6 changes: 6 additions & 0 deletions src/uu/cp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#[cfg(feature = "cli-parser")]
uucore::bin!(uu_cp);

// for avoiding cargo check error
// consider adding a `main` function to `src/uu/cp/src/main.rs`
#[cfg(not(feature = "cli-parser"))]
fn main() {}
6 changes: 3 additions & 3 deletions src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ edition = "2021"
path = "src/lib/lib.rs"

[dependencies]
clap = { workspace = true }
clap = { workspace = true, optional = true }
uucore_procs = { workspace = true }
dns-lookup = { version = "2.0.4", optional = true }
dunce = { version = "1.0.4", optional = true }
Expand Down Expand Up @@ -69,8 +69,7 @@ windows-sys = { workspace = true, optional = true, default-features = false, fea
] }

[features]
default = []
# * non-default features
default = ["cli-parser"]
backup-control = []
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
entries = ["libc"]
Expand Down Expand Up @@ -103,3 +102,4 @@ utf8 = []
utmpx = ["time", "time/macros", "libc", "dns-lookup"]
version-cmp = []
wide = []
cli-parser = ["dep:clap"]
15 changes: 10 additions & 5 deletions src/uucore/src/lib/features/backup_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@

// spell-checker:ignore backupopt

use crate::{
display::Quotable,
error::{UError, UResult},
};
#[cfg(feature = "cli-parser")]
use crate::error::UResult;
use crate::{display::Quotable, error::UError};
#[cfg(feature = "cli-parser")]
use clap::ArgMatches;
#[cfg(feature = "cli-parser")]
use std::env;
use std::{
env,
error::Error,
fmt::{Debug, Display},
path::{Path, PathBuf},
Expand Down Expand Up @@ -201,6 +202,7 @@ impl Display for BackupError {
/// recommended to include the `clap` arguments via the functions provided here.
/// This way the backup-specific arguments are handled uniformly across
/// utilities and can be maintained in one central place.
#[cfg(feature = "cli-parser")]
pub mod arguments {
use clap::ArgAction;

Expand Down Expand Up @@ -249,6 +251,7 @@ pub mod arguments {
///
/// This function directly takes [`clap::ArgMatches`] as argument and looks for
/// the '-S' and '--suffix' arguments itself.
#[cfg(feature = "cli-parser")]
pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
let supplied_suffix = matches.get_one::<String>(arguments::OPT_SUFFIX);
if let Some(suffix) = supplied_suffix {
Expand Down Expand Up @@ -336,6 +339,7 @@ pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
/// show!(err);
/// }
/// ```
#[cfg(feature = "cli-parser")]
pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode> {
if matches.contains_id(arguments::OPT_BACKUP) {
// Use method to determine the type of backups to make. When this option
Expand Down Expand Up @@ -381,6 +385,7 @@ pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode> {
///
/// [10]: BackupError::InvalidArgument
/// [11]: BackupError::AmbiguousArgument
#[cfg(feature = "cli-parser")]
fn match_method(method: &str, origin: &str) -> UResult<BackupMode> {
let matches: Vec<&&str> = BACKUP_CONTROL_VALUES
.iter()
Expand Down
12 changes: 10 additions & 2 deletions src/uucore/src/lib/features/perms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
use crate::display::Quotable;
use crate::error::strip_errno;
use crate::error::UResult;
#[cfg(feature = "cli-parser")]
use crate::error::USimpleError;
pub use crate::features::entries;
use crate::fs::resolve_relative_path;
use crate::show_error;
use libc::{self, gid_t, uid_t};
use walkdir::WalkDir;

#[cfg(feature = "cli-parser")]
use clap::Arg;
#[cfg(feature = "cli-parser")]
use clap::ArgMatches;
#[cfg(feature = "cli-parser")]
use clap::Command;
use libc::{self, gid_t, uid_t};
use walkdir::WalkDir;

use std::io::Error as IOError;
use std::io::Result as IOResult;
Expand Down Expand Up @@ -469,6 +474,8 @@ pub struct GidUidOwnerFilter {
pub raw_owner: String,
pub filter: IfFrom,
}

#[cfg(feature = "cli-parser")]
type GidUidFilterOwnerParser = fn(&ArgMatches) -> UResult<GidUidOwnerFilter>;

/// Base implementation for `chgrp` and `chown`.
Expand All @@ -479,6 +486,7 @@ type GidUidFilterOwnerParser = fn(&ArgMatches) -> UResult<GidUidOwnerFilter>;
/// from `ArgMatches`.
/// `groups_only` determines whether verbose output will only mention the group.
#[allow(clippy::cognitive_complexity)]
#[cfg(feature = "cli-parser")]
pub fn chown_base(
mut command: Command,
args: impl crate::Args,
Expand Down
3 changes: 3 additions & 0 deletions src/uucore/src/lib/features/update_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
//! }
//! }
//! ```
#[cfg(feature = "cli-parser")]
use clap::ArgMatches;

// Available update mode
Expand All @@ -60,6 +61,7 @@ pub enum UpdateMode {
ReplaceIfOlder,
}

#[cfg(feature = "cli-parser")]
pub mod arguments {
use clap::ArgAction;

Expand Down Expand Up @@ -119,6 +121,7 @@ pub mod arguments {
/// let update_mode = update_control::determine_update_mode(&matches);
/// assert_eq!(update_mode, UpdateMode::ReplaceAll)
/// }
#[cfg(feature = "cli-parser")]
pub fn determine_update_mode(matches: &ArgMatches) -> UpdateMode {
if let Some(mode) = matches.get_one::<String>(arguments::OPT_UPDATE) {
match mode.as_str() {
Expand Down
1 change: 1 addition & 0 deletions src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub use crate::mods::panic;
pub use crate::parser::parse_glob;
pub use crate::parser::parse_size;
pub use crate::parser::parse_time;
#[cfg(feature = "cli-parser")]
pub use crate::parser::shortcut_value_parser;

// * feature-gated modules
Expand Down