Skip to content

Commit

Permalink
Implement owner subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Jul 17, 2024
1 parent 15064eb commit 89141bf
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ But this will also work on single crates because by default every individual cra
7. [Publish](#publish)
8. [Rename](#rename)
9. [Plan](#plan)
10. [Owner](#owner)
3. [Config](#config)
4. [Changelog](#changelog)

Expand Down Expand Up @@ -298,6 +299,27 @@ LIST OPTIONS:
-l, --long Show extended information
```

### Owner

Manage the owners of the workspaces crates.

```
USAGE:
cargo workspaces owner [OPTIONS]
OPTIONS:
-h, --help Print help information
OWNER OPTIONS:
-a, --add <ADD> Name of a user or team to invite as an owner
-l, --list List owners for each crate in the workspace
-r, --remove <REMOVE> Name of a user or team to remove as an owner
REGISTRY OPTIONS:
--registry <REGISTRY> The Cargo registry to use
--token <TOKEN> The token to use for accessing the registry
```

## Config

There are two kind of options.
Expand Down
3 changes: 3 additions & 0 deletions cargo-workspaces/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod create;
mod exec;
mod init;
mod list;
mod owner;
mod plan;
mod publish;
mod rename;
Expand All @@ -26,6 +27,7 @@ enum Subcommand {
Rename(rename::Rename),
Init(init::Init),
Plan(plan::Plan),
Owner(owner::Owner),
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -86,6 +88,7 @@ fn main() {
Subcommand::Create(x) => x.run(metadata),
Subcommand::Rename(x) => x.run(metadata),
Subcommand::Plan(x) => x.run(metadata),
Subcommand::Owner(x) => x.run(metadata),
_ => unreachable!(),
}
};
Expand Down
83 changes: 83 additions & 0 deletions cargo-workspaces/src/owner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::utils::{cargo, dag, filter_private, info, Error, RegistryOpt, Result, INTERNAL_ERR};

use cargo_metadata::Metadata;
use clap::Parser;

/// Manage the owners of the workspaces crates
#[derive(Debug, Parser)]
#[clap(next_help_heading = "OWNER OPTIONS")]
pub struct Owner {
/// Name of a user or team to invite as an owner
#[clap(short, long)]
add: Option<String>,

/// Name of a user or team to remove as an owner
#[clap(short, long)]
remove: Option<String>,

/// List owners for each crate in the workspace
#[clap(short, long)]
list: bool,

#[clap(flatten)]
registry: RegistryOpt,
}

impl Owner {
pub fn run(self, metadata: Metadata) -> Result {
let pkgs = metadata
.packages
.iter()
.map(|x| (x.clone(), x.version.to_string()))
.collect::<Vec<_>>();

let (names, visited) = dag(&pkgs);

// Filter out private packages
let visited = filter_private(visited, &pkgs);

for p in &visited {
let (pkg, _version) = names.get(p).expect(INTERNAL_ERR);
let name = pkg.name.clone();

let mut args = vec!["owner"];

if let Some(ref registry) = self.registry.registry {
args.push("--registry");
args.push(registry);
}

if let Some(ref token) = self.registry.token {
args.push("--token");
args.push(token);
}

if let Some(ref add) = self.add {
args.push("--add");
args.push(add);
}

if let Some(ref remove) = self.remove {
args.push("--remove");
args.push(remove);
}

if self.list {
args.push("--list");
}

// `cargo owner` doesn't support `manifest-path`
let crate_path = pkg.manifest_path.parent().expect(INTERNAL_ERR);

let (stdout, stderr) = cargo(crate_path, &args, &[])?;
// `cargo owner` uses `stdout` to print the names.
eprintln!("{}", stdout);
if stderr.contains("error:") {
return Err(Error::Owner(name));
}
}

info!("success", "ok");
Ok(())
}
}
3 changes: 3 additions & 0 deletions cargo-workspaces/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub enum Error {
#[error("unable to update Cargo.lock")]
Update,

#[error("unable to modify owners for package {0}")]
Owner(String),

#[error("{0} value must contain '%n'")]
MustContainPercentN(String),

Expand Down
12 changes: 12 additions & 0 deletions cargo-workspaces/tests/owner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod utils;

#[test]
fn test_list() {
// Check current crate (`cargo-workspaces`).
let err = utils::run_err("./", &["ws", "owner", "--list"]);
assert!(
err.contains("pksunkara (Pavan Kumar Sunkara)"),
"stderr: {}",
err
);
}

0 comments on commit 89141bf

Please sign in to comment.