Skip to main content

iggy_cli/commands/binary_system/
me.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::Table;
23use iggy_common::Client;
24use tracing::{Level, event};
25
26#[derive(Default)]
27pub struct GetMeCmd;
28
29impl GetMeCmd {
30    pub fn new() -> Self {
31        Self
32    }
33}
34
35#[async_trait]
36impl CliCommand for GetMeCmd {
37    fn explain(&self) -> String {
38        "me command".to_owned()
39    }
40
41    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
42        let client_info = client
43            .get_me()
44            .await
45            .with_context(|| "Problem sending get_me command".to_owned())?;
46
47        let mut table = Table::new();
48
49        table.set_header(vec!["Property", "Value"]);
50        table.add_row(vec![
51            "Client ID",
52            format!("{}", client_info.client_id).as_str(),
53        ]);
54        if let Some(user_id) = client_info.user_id {
55            table.add_row(vec!["User ID", format!("{user_id}").as_str()]);
56        }
57        table.add_row(vec!["Address", client_info.address.as_str()]);
58        table.add_row(vec!["Transport", client_info.transport.as_str()]);
59
60        event!(target: PRINT_TARGET, Level::INFO, "{table}");
61
62        Ok(())
63    }
64}