Skip to content

Commit 7f604be

Browse files
authored
Add shortcut commands to adding and removing access to rfds (#340)
* Add shortcut commands to adding and removing access to rfds * Add missing license * Fix path arg in call. Fix duplicate group argument
1 parent df8d3a5 commit 7f604be

File tree

3 files changed

+153
-6
lines changed

3 files changed

+153
-6
lines changed

rfd-cli/src/cmd/shortcut/access.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
use anyhow::{bail, Ok, Result};
6+
use clap::{Parser, Subcommand};
7+
use rfd_sdk::types::RfdPermission;
8+
9+
use crate::Context;
10+
11+
#[derive(Debug, Parser)]
12+
pub struct AccessShortcut {
13+
#[clap(subcommand)]
14+
pub access: AccessShortcuts,
15+
}
16+
17+
#[derive(Debug, Subcommand)]
18+
pub enum AccessShortcuts {
19+
/// Grant and revoke access to RFDs
20+
Rfd(RfdAccessShortcut),
21+
}
22+
23+
#[derive(Debug, Parser)]
24+
pub struct RfdAccessShortcut {
25+
#[clap(subcommand)]
26+
pub rfd: RfdAccessShortcuts,
27+
}
28+
29+
#[derive(Debug, Subcommand)]
30+
pub enum RfdAccessShortcuts {
31+
/// Grant access to an RFD
32+
Add(AddRfdAccessShortcut),
33+
/// Revoke access to an RFD
34+
Remove(RemoveRfdAccessShortcut),
35+
}
36+
37+
#[derive(Debug, Parser)]
38+
pub struct AddRfdAccessShortcut {
39+
/// Group name or id to grant access to
40+
pub group: String,
41+
/// RFD to grant access to
42+
pub number: i32,
43+
}
44+
45+
#[derive(Debug, Parser)]
46+
pub struct RemoveRfdAccessShortcut {
47+
/// Group name or id to revoke access to
48+
pub group: String,
49+
/// RFD to revoke access to
50+
pub number: i32,
51+
}
52+
53+
impl AddRfdAccessShortcut {
54+
pub async fn run(&self, ctx: &mut Context) -> Result<()> {
55+
let client = ctx.client()?;
56+
let groups = client.get_groups().send().await?.into_inner();
57+
let group = groups
58+
.iter()
59+
.find(|g| g.id.to_string() == self.group)
60+
.or_else(|| groups.iter().find(|g| g.name == self.group));
61+
62+
if let Some(mut group) = group.cloned() {
63+
group.permissions.0.push(RfdPermission::GetRfd(self.number));
64+
let response = client
65+
.update_group()
66+
.group_id(group.id)
67+
.body_map(|body| body.name(group.name).permissions(group.permissions))
68+
.send()
69+
.await?
70+
.into_inner();
71+
72+
println!("Added access to RFD {} to {}", self.number, self.group);
73+
Ok(())
74+
} else {
75+
bail!("Unable to find requested group")
76+
}
77+
}
78+
}
79+
80+
impl RemoveRfdAccessShortcut {
81+
pub async fn run(&self, ctx: &mut Context) -> Result<()> {
82+
let client = ctx.client()?;
83+
let groups = client.get_groups().send().await?.into_inner();
84+
let group = groups
85+
.iter()
86+
.find(|g| g.id.to_string() == self.group)
87+
.or_else(|| groups.iter().find(|g| g.name == self.group));
88+
89+
if let Some(mut group) = group.cloned() {
90+
group.permissions.0 = group
91+
.permissions
92+
.0
93+
.into_iter()
94+
.filter(|permission| match permission {
95+
RfdPermission::GetRfd(number) if *number == self.number => false,
96+
_ => true,
97+
})
98+
.collect::<Vec<_>>();
99+
100+
let response = client
101+
.update_group()
102+
.group_id(group.id)
103+
.body_map(|body| body.name(group.name).permissions(group.permissions))
104+
.send()
105+
.await?
106+
.into_inner();
107+
108+
println!("Removed access to RFD {} from {}", self.number, self.group);
109+
Ok(())
110+
} else {
111+
bail!("Unable to find requested group")
112+
}
113+
}
114+
}

rfd-cli/src/cmd/shortcut/mapper.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ use uuid::Uuid;
1111

1212
use crate::{printer::CliOutput, Context};
1313

14+
#[derive(Debug, Parser)]
15+
pub struct MapperShortcut {
16+
#[clap(subcommand)]
17+
pub mapper: MapperShortcuts,
18+
}
19+
20+
#[derive(Debug, Subcommand)]
21+
pub enum MapperShortcuts {
22+
Email(EmailMapper),
23+
#[command(name = "github")]
24+
GitHub(GitHubMapper),
25+
}
26+
1427
#[derive(Debug, Parser)]
1528
/// Add a new one-time mapping for for a GitHub user
1629
pub struct GitHubMapper {

rfd-cli/src/cmd/shortcut/mod.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
use anyhow::Result;
66
use clap::{Parser, Subcommand};
77

8-
use crate::Context;
8+
use crate::{
9+
cmd::shortcut::{
10+
access::{AccessShortcut, AccessShortcuts, RfdAccessShortcut, RfdAccessShortcuts},
11+
mapper::{MapperShortcut, MapperShortcuts},
12+
},
13+
Context,
14+
};
915

1016
use self::mapper::{EmailMapper, GitHubMapper};
1117

18+
mod access;
1219
mod mapper;
1320

1421
#[derive(Debug, Parser)]
@@ -21,16 +28,29 @@ pub struct ShortcutCmd {
2128

2229
#[derive(Debug, Subcommand)]
2330
pub enum Shortcut {
24-
EmailMapper(EmailMapper),
25-
#[command(name = "github-mapper")]
26-
GitHubMapper(GitHubMapper),
31+
/// Grant and revoke access to resources for groups
32+
Access(AccessShortcut),
33+
/// Create new mappers
34+
Mapper(MapperShortcut),
2735
}
2836

2937
impl ShortcutCmd {
3038
pub async fn run(&self, ctx: &mut Context) -> Result<()> {
3139
match &self.shortcut {
32-
Shortcut::EmailMapper(cmd) => cmd.run(ctx).await?,
33-
Shortcut::GitHubMapper(cmd) => cmd.run(ctx).await?,
40+
Shortcut::Access(shortcut) => match &shortcut.access {
41+
AccessShortcuts::Rfd(method) => match &method.rfd {
42+
RfdAccessShortcuts::Add(cmd) => {
43+
cmd.run(ctx).await?;
44+
}
45+
RfdAccessShortcuts::Remove(cmd) => {
46+
cmd.run(ctx).await?;
47+
}
48+
},
49+
},
50+
Shortcut::Mapper(shortcut) => match &shortcut.mapper {
51+
MapperShortcuts::Email(cmd) => cmd.run(ctx).await?,
52+
MapperShortcuts::GitHub(cmd) => cmd.run(ctx).await?,
53+
},
3454
}
3555

3656
Ok(())

0 commit comments

Comments
 (0)