Skip to content

Commit 979ace9

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

File tree

7 files changed

+314
-39
lines changed

7 files changed

+314
-39
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: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::collections::{BTreeSet, HashMap};
1+
use std::collections::BTreeSet;
2+
use std::collections::HashMap;
23
use std::fs::{self, File};
34
use std::io::prelude::*;
45
use std::io::SeekFrom;
@@ -40,10 +41,38 @@ use unicase::Ascii as UncasedAscii;
4041
mod vcs;
4142
mod verify;
4243

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

238267
if opts.list {
239-
for ar_file in &ar_files {
240-
drop_println!(ws.gctx(), "{}", ar_file.rel_str);
268+
match opts.fmt {
269+
PackageMessageFormat::Human => {
270+
// While this form is called "human",
271+
// it keeps the old file-per-line format for compatibility.
272+
for ar_file in &ar_files {
273+
drop_println!(ws.gctx(), "{}", ar_file.rel_str);
274+
}
275+
}
276+
PackageMessageFormat::Json => {
277+
let _ = ws.gctx().shell().print_json(&HashMap::<(), ()>::new());
278+
}
241279
}
242280
} else {
243281
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)