grr-cli 0.4.0

Google tools from the terminal, at maximum performance: zero-config Gmail, Calendar, Drive, Contacts, Chat and Forms over HTTP/3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Contacts (People API) CLI commands

use std::path::PathBuf;

use crate::output::{OutputFormat, print_output};
use crate::people::{
    EmailAddress, ListConnectionsOptions, Name, PeopleClient, Person, PhoneNumber,
};
use anyhow::{Result, bail};
use base64::Engine as _;
use clap::{Args, Subcommand};

#[derive(Subcommand, Debug)]
pub enum ContactsCommands {
    /// List connections (the user's contacts)
    List(ListArgs),
    /// Search the user's contacts with a prefix query
    Search(SearchArgs),
    /// Get a person by resource name
    Get(GetArgs),
    /// Create a new contact
    Create(CreateArgs),
    /// Update an existing contact
    Update(UpdateArgs),
    /// Delete a contact
    Delete(DeleteArgs),
    #[command(name = "groups", about = "List contact groups")]
    Groups(GroupsArgs),
    #[command(name = "group", about = "Get a contact group")]
    Group(GroupArgs),
    #[command(name = "group-new", about = "Create a contact group")]
    GroupNew(GroupNewArgs),
    #[command(name = "group-rename", about = "Rename a contact group")]
    GroupRename(GroupRenameArgs),
    #[command(name = "group-delete", about = "Delete a contact group")]
    GroupDelete(GroupDeleteArgs),
    #[command(name = "group-add", about = "Add contacts to a group")]
    GroupAdd(GroupMembersArgs),
    #[command(name = "group-remove", about = "Remove contacts from a group")]
    GroupRemove(GroupMembersArgs),
    #[command(name = "get-batch", about = "Get multiple people")]
    GetBatch(GetBatchArgs),
    #[command(name = "others", about = "List other contacts")]
    Others(OthersArgs),
    #[command(name = "adopt", about = "Copy an other contact into my contacts")]
    Adopt(AdoptArgs),
    #[command(name = "photos", about = "List a person's photos")]
    Photos(PhotosArgs),
    #[command(name = "photo-set", about = "Update a contact's photo")]
    PhotoSet(PhotoSetArgs),
    #[command(name = "photo-remove", about = "Delete a contact's photo")]
    PhotoRemove(PhotoRemoveArgs),
}

#[derive(Args, Debug)]
pub struct ListArgs {
    /// Filter connections by name (plain-text prefix query)
    #[arg(long)]
    pub query_name: Option<String>,

    /// Maximum number of contacts to return
    #[arg(long, default_value_t = 100)]
    pub max: usize,

    /// Output format
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct SearchArgs {
    /// Prefix query (matches names, emails, phones, organizations)
    pub query: String,

    /// Output format
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GetArgs {
    /// Resource name (e.g. people/123)
    pub resource_name: String,

    /// Output format
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct CreateArgs {
    /// Given (first) name
    #[arg(long)]
    pub given_name: Option<String>,

    /// Family (last) name
    #[arg(long)]
    pub family_name: Option<String>,

    /// Email address
    #[arg(long)]
    pub email: Option<String>,

    /// Phone number
    #[arg(long)]
    pub phone: Option<String>,

    /// Output format
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct UpdateArgs {
    /// Resource name (e.g. people/123)
    pub resource_name: String,

    /// New given (first) name
    #[arg(long)]
    pub given_name: Option<String>,

    /// New family (last) name
    #[arg(long)]
    pub family_name: Option<String>,

    /// New email address (replaces all existing ones)
    #[arg(long)]
    pub email: Option<String>,

    /// New phone number (replaces all existing ones)
    #[arg(long)]
    pub phone: Option<String>,

    /// Output format
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct DeleteArgs {
    /// Resource name (e.g. people/123)
    pub resource_name: String,
}

#[derive(Args, Debug)]
pub struct GroupsArgs {
    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GroupArgs {
    #[arg(help = "Contact group ID or resource name")]
    pub id: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GroupNewArgs {
    #[arg(long, help = "Contact group name")]
    pub name: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GroupRenameArgs {
    #[arg(help = "Contact group ID or resource name")]
    pub id: String,

    #[arg(long, help = "New contact group name")]
    pub name: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GroupDeleteArgs {
    #[arg(help = "Contact group ID or resource name")]
    pub id: String,
}

#[derive(Args, Debug)]
pub struct GroupMembersArgs {
    #[arg(help = "Contact group ID or resource name")]
    pub id: String,

    #[arg(
        long,
        value_delimiter = ',',
        required = true,
        help = "Comma-separated people resource names"
    )]
    pub people: Vec<String>,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct GetBatchArgs {
    #[arg(
        long,
        value_delimiter = ',',
        required = true,
        help = "Comma-separated people resource names"
    )]
    pub people: Vec<String>,

    #[arg(
        long,
        default_value = "names,emailAddresses",
        help = "Comma-separated person fields"
    )]
    pub fields: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct OthersArgs {
    #[arg(long, default_value_t = 100, help = "Maximum number of contacts")]
    pub max: usize,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct AdoptArgs {
    #[arg(help = "Other-contact ID or resource name (people/123 is accepted)")]
    pub resource_name: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct PhotosArgs {
    #[arg(help = "Person resource name (e.g. people/123)")]
    pub resource_name: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct PhotoSetArgs {
    #[arg(help = "Person resource name (e.g. people/123)")]
    pub resource_name: String,

    #[arg(long, help = "Path to the photo file")]
    pub path: PathBuf,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct PhotoRemoveArgs {
    #[arg(help = "Person resource name (e.g. people/123)")]
    pub resource_name: String,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

pub async fn handle_contacts_cmd(client: &PeopleClient, cmd: ContactsCommands) -> Result<()> {
    match cmd {
        ContactsCommands::List(args) => {
            let page_size = u32::try_from(args.max.min(1000)).unwrap_or(1000);
            let people = client
                .list_connections(ListConnectionsOptions {
                    page_size,
                    query_name: args.query_name,
                    max_results: args.max,
                })
                .await?;
            print_output(&people, args.format)?;
        }
        ContactsCommands::Search(args) => {
            let people = client.search_contacts(&args.query).await?;
            print_output(&people, args.format)?;
        }
        ContactsCommands::Get(args) => {
            let person = client.get_person(&args.resource_name).await?;
            print_output(&person, args.format)?;
        }
        ContactsCommands::Create(args) => {
            ensure_any_contact_field(
                &args.given_name,
                &args.family_name,
                &args.email,
                &args.phone,
            )?;
            let person = Person {
                names: build_name(&args.given_name, &args.family_name),
                email_addresses: build_email(&args.email),
                phone_numbers: build_phone(&args.phone),
                ..Person::default()
            };
            let created = client.create_contact(person).await?;
            print_output(&created, args.format)?;
        }
        ContactsCommands::Update(args) => {
            ensure_any_contact_field(
                &args.given_name,
                &args.family_name,
                &args.email,
                &args.phone,
            )?;
            let mut person = client.get_person(&args.resource_name).await?;
            let mut fields: Vec<&str> = Vec::new();

            if args.given_name.is_some() || args.family_name.is_some() {
                fields.push("names");
                let names = person.names.get_or_insert_with(Vec::new);
                if names.is_empty() {
                    names.push(Name::default());
                }
                if let Some(name) = names.first_mut() {
                    if args.given_name.is_some() {
                        name.given_name = args.given_name.clone();
                    }
                    if args.family_name.is_some() {
                        name.family_name = args.family_name.clone();
                    }
                }
            }
            if let Some(email) = &args.email {
                fields.push("emailAddresses");
                person.email_addresses = Some(vec![EmailAddress {
                    value: Some(email.clone()),
                    ..EmailAddress::default()
                }]);
            }
            if let Some(phone) = &args.phone {
                fields.push("phoneNumbers");
                person.phone_numbers = Some(vec![PhoneNumber {
                    value: Some(phone.clone()),
                    ..PhoneNumber::default()
                }]);
            }

            let updated = client
                .update_contact(&args.resource_name, person, &fields)
                .await?;
            print_output(&updated, args.format)?;
        }
        ContactsCommands::Delete(args) => {
            client.delete_contact(&args.resource_name).await?;
            println!("Contact {} deleted", args.resource_name);
        }
        ContactsCommands::Groups(args) => {
            let groups = client.list_contact_groups().await?;
            print_output(&groups, args.format)?;
        }
        ContactsCommands::Group(args) => {
            let resource_name = contact_group_resource_name(&args.id)?;
            let group = client.get_contact_group(&resource_name).await?;
            print_output(&group, args.format)?;
        }
        ContactsCommands::GroupNew(args) => {
            let group = client.create_contact_group(&args.name).await?;
            print_output(&group, args.format)?;
        }
        ContactsCommands::GroupRename(args) => {
            let resource_name = contact_group_resource_name(&args.id)?;
            let group = client
                .update_contact_group(&resource_name, &args.name)
                .await?;
            print_output(&group, args.format)?;
        }
        ContactsCommands::GroupDelete(args) => {
            let resource_name = contact_group_resource_name(&args.id)?;
            client.delete_contact_group(&resource_name).await?;
            println!("Contact group {resource_name} deleted");
        }
        ContactsCommands::GroupAdd(args) => {
            let resource_name = contact_group_resource_name(&args.id)?;
            let response = client
                .modify_contact_group_members(&resource_name, &args.people, &[])
                .await?;
            print_output(&response, args.format)?;
        }
        ContactsCommands::GroupRemove(args) => {
            let resource_name = contact_group_resource_name(&args.id)?;
            let response = client
                .modify_contact_group_members(&resource_name, &[], &args.people)
                .await?;
            print_output(&response, args.format)?;
        }
        ContactsCommands::GetBatch(args) => {
            let response = client.get_people(&args.people, &args.fields).await?;
            print_output(&response, args.format)?;
        }
        ContactsCommands::Others(args) => {
            let contacts = client.list_other_contacts(args.max).await?;
            print_output(&contacts, args.format)?;
        }
        ContactsCommands::Adopt(args) => {
            let resource_name = other_contact_resource_name(&args.resource_name)?;
            let person = client
                .copy_other_contact_to_my_contacts_group(&resource_name)
                .await?;
            print_output(&person, args.format)?;
        }
        ContactsCommands::Photos(args) => {
            let photos = client.list_photos(&args.resource_name).await?;
            print_output(&photos, args.format)?;
        }
        ContactsCommands::PhotoSet(args) => {
            let bytes = tokio::fs::read(&args.path).await?;
            let photo_bytes = base64::engine::general_purpose::STANDARD.encode(bytes);
            let response = client
                .update_contact_photo(&args.resource_name, &photo_bytes)
                .await?;
            let photos = response
                .person
                .and_then(|person| person.photos)
                .unwrap_or_default();
            print_output(&photos, args.format)?;
        }
        ContactsCommands::PhotoRemove(args) => {
            let response = client.delete_contact_photo(&args.resource_name).await?;
            let photos = response
                .person
                .and_then(|person| person.photos)
                .unwrap_or_default();
            print_output(&photos, args.format)?;
        }
    }
    Ok(())
}

fn contact_group_resource_name(value: &str) -> Result<String> {
    let value = value.trim();
    if let Some(id) = value.strip_prefix("contactGroups/") {
        if id.is_empty() || id.contains('/') {
            bail!("contact group must have the form contactGroups/<id>");
        }
        return Ok(value.to_owned());
    }
    if value.is_empty() || value.contains('/') {
        bail!("contact group must be an ID or contactGroups/<id>");
    }
    Ok(format!("contactGroups/{value}"))
}

fn other_contact_resource_name(value: &str) -> Result<String> {
    let value = value.trim();
    if let Some(id) = value.strip_prefix("otherContacts/") {
        if id.is_empty() || id.contains('/') {
            bail!("other contact must have the form otherContacts/<id>");
        }
        return Ok(value.to_owned());
    }
    if let Some(id) = value.strip_prefix("people/") {
        if id.is_empty() || id.contains('/') {
            bail!("other contact must have the form otherContacts/<id>");
        }
        return Ok(format!("otherContacts/{id}"));
    }
    if value.is_empty() || value.contains('/') {
        bail!("other contact must be an ID, people/<id>, or otherContacts/<id>");
    }
    Ok(format!("otherContacts/{value}"))
}

/// A contact with no name, email, or phone cannot be created or updated.
fn ensure_any_contact_field(
    given_name: &Option<String>,
    family_name: &Option<String>,
    email: &Option<String>,
    phone: &Option<String>,
) -> Result<()> {
    if given_name.is_none() && family_name.is_none() && email.is_none() && phone.is_none() {
        bail!("at least one of --given-name, --family-name, --email, or --phone is required");
    }
    Ok(())
}

fn build_name(given_name: &Option<String>, family_name: &Option<String>) -> Option<Vec<Name>> {
    if given_name.is_none() && family_name.is_none() {
        return None;
    }
    Some(vec![Name {
        given_name: given_name.clone(),
        family_name: family_name.clone(),
        ..Name::default()
    }])
}

fn build_email(email: &Option<String>) -> Option<Vec<EmailAddress>> {
    email.as_ref().map(|value| {
        vec![EmailAddress {
            value: Some(value.clone()),
            ..EmailAddress::default()
        }]
    })
}

fn build_phone(phone: &Option<String>) -> Option<Vec<PhoneNumber>> {
    phone.as_ref().map(|value| {
        vec![PhoneNumber {
            value: Some(value.clone()),
            ..PhoneNumber::default()
        }]
    })
}