Skip to content

Commit 781c7ed

Browse files
committed
feat(package): add unstable --message-format flag
The behavior is not implemented yet
1 parent 6a70640 commit 781c7ed

File tree

7 files changed

+312
-38
lines changed

7 files changed

+312
-38
lines changed

src/bin/cargo/commands/package.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::command_prelude::*;
22

3-
use cargo::ops::{self, PackageOpts};
3+
use cargo::ops;
4+
use cargo::ops::PackageMessageFormat;
5+
use cargo::ops::PackageOpts;
46

57
pub fn cli() -> Command {
68
subcommand("package")
@@ -30,6 +32,13 @@ pub fn cli() -> Command {
3032
"exclude-lockfile",
3133
"Don't include the lock file when packaging",
3234
))
35+
.arg(
36+
opt("message-format", "Output representation (unstable)")
37+
.value_name("FMT")
38+
// This currently requires and only works with `--list`.
39+
.requires("list")
40+
.value_parser(PackageMessageFormat::POSSIBLE_VALUES),
41+
)
3342
.arg_silent_suggestion()
3443
.arg_package_spec_no_all(
3544
"Package(s) to assemble",
@@ -75,12 +84,21 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7584
}
7685
let specs = args.packages_from_flags()?;
7786

87+
let fmt = if let Some(fmt) = args._value_of("message-format") {
88+
gctx.cli_unstable()
89+
.fail_if_stable_opt("--message-format", 11666)?;
90+
fmt.parse()?
91+
} else {
92+
PackageMessageFormat::Human
93+
};
94+
7895
ops::package(
7996
&ws,
8097
&PackageOpts {
8198
gctx,
8299
verify: !args.flag("no-verify"),
83100
list: args.flag("list"),
101+
fmt,
84102
check_metadata: !args.flag("no-metadata"),
85103
allow_dirty: args.flag("allow-dirty"),
86104
include_lockfile: !args.flag("exclude-lockfile"),

src/cargo/ops/cargo_package/mod.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,38 @@ use unicase::Ascii as UncasedAscii;
4040
mod vcs;
4141
mod verify;
4242

43+
/// Message format for `cargo package`.
44+
///
45+
/// Currently only affect the output of the `--list` flag.
46+
#[derive(Debug, Clone)]
47+
pub enum PackageMessageFormat {
48+
Human,
49+
Json,
50+
}
51+
52+
impl PackageMessageFormat {
53+
pub const POSSIBLE_VALUES: [&str; 2] = ["human", "json"];
54+
55+
pub const DEFAULT: &str = "human";
56+
}
57+
58+
impl std::str::FromStr for PackageMessageFormat {
59+
type Err = anyhow::Error;
60+
61+
fn from_str(s: &str) -> Result<PackageMessageFormat, anyhow::Error> {
62+
match s {
63+
"human" => Ok(PackageMessageFormat::Human),
64+
"json" => Ok(PackageMessageFormat::Json),
65+
f => bail!("unknown message format `{f}`"),
66+
}
67+
}
68+
}
69+
4370
#[derive(Clone)]
4471
pub struct PackageOpts<'gctx> {
4572
pub gctx: &'gctx GlobalContext,
4673
pub list: bool,
74+
pub fmt: PackageMessageFormat,
4775
pub check_metadata: bool,
4876
pub allow_dirty: bool,
4977
pub include_lockfile: bool,
@@ -236,8 +264,17 @@ fn do_package<'a>(
236264
let ar_files = prepare_archive(ws, &pkg, &opts)?;
237265

238266
if opts.list {
239-
for ar_file in &ar_files {
240-
drop_println!(ws.gctx(), "{}", ar_file.rel_str);
267+
match opts.fmt {
268+
PackageMessageFormat::Human => {
269+
// While this form is called "human",
270+
// it keeps the old file-per-line format for compatibility.
271+
for ar_file in &ar_files {
272+
drop_println!(ws.gctx(), "{}", ar_file.rel_str);
273+
}
274+
}
275+
PackageMessageFormat::Json => {
276+
let _ = ws.gctx().shell().print_json(&HashMap::<(), ()>::new());
277+
}
241278
}
242279
} else {
243280
let tarball = create_package(ws, &pkg, ar_files, local_reg.as_ref())?;

src/cargo/ops/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ pub use self::cargo_fetch::{fetch, FetchOptions};
1010
pub use self::cargo_install::{install, install_list};
1111
pub use self::cargo_new::{init, new, NewOptions, NewProjectKind, VersionControl};
1212
pub use self::cargo_output_metadata::{output_metadata, ExportInfo, OutputMetadataOptions};
13-
pub use self::cargo_package::{check_yanked, package, PackageOpts};
13+
pub use self::cargo_package::check_yanked;
14+
pub use self::cargo_package::package;
15+
pub use self::cargo_package::PackageMessageFormat;
16+
pub use self::cargo_package::PackageOpts;
1417
pub use self::cargo_pkgid::pkgid;
1518
pub use self::cargo_read_manifest::read_package;
1619
pub use self::cargo_run::run;

src/cargo/ops/registry/publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
144144
gctx: opts.gctx,
145145
verify: opts.verify,
146146
list: false,
147+
fmt: ops::PackageMessageFormat::Human,
147148
check_metadata: true,
148149
allow_dirty: opts.allow_dirty,
149150
include_lockfile: true,

tests/testsuite/cargo_package/help/stdout.term.svg

Lines changed: 36 additions & 34 deletions
Loading

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ mod open_namespaces;
135135
mod owner;
136136
mod package;
137137
mod package_features;
138+
mod package_message_format;
138139
mod patch;
139140
mod path;
140141
mod paths;

0 commit comments

Comments
 (0)