examples_fxa_client/
devices.rs

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 http://mozilla.org/MPL/2.0/. */
4
5use clap::{Args, Subcommand};
6use fxa_client::FirefoxAccount;
7
8use crate::{persist_fxa_state, Result};
9
10#[derive(Args)]
11pub struct DeviceArgs {
12    #[command(subcommand)]
13    command: Option<Command>,
14}
15
16#[derive(Subcommand)]
17enum Command {
18    List,
19    SetName { name: String },
20}
21
22pub fn run(account: &FirefoxAccount, args: DeviceArgs) -> Result<()> {
23    match args.command.unwrap_or(Command::List) {
24        Command::List => list(account),
25        Command::SetName { name } => set_name(account, name),
26    }
27}
28
29fn list(account: &FirefoxAccount) -> Result<()> {
30    for device in account.get_devices(false)? {
31        println!("{}: {}", device.id, device.display_name);
32    }
33    Ok(())
34}
35
36fn set_name(account: &FirefoxAccount, name: String) -> Result<()> {
37    account.set_device_name(&name)?;
38    println!("Display name set to {name}");
39    persist_fxa_state(account)?;
40    Ok(())
41}