Skip to main content

iggy_cli/commands/binary_context/
use_context.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 async_trait::async_trait;
20use tracing::{Level, event};
21
22use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
23use iggy_common::Client;
24
25use super::common::{ContextManager, DEFAULT_CONTEXT_NAME};
26
27pub struct UseContextCmd {
28    context_name: String,
29}
30
31impl UseContextCmd {
32    pub fn new(context_name: String) -> Self {
33        Self { context_name }
34    }
35}
36
37impl Default for UseContextCmd {
38    fn default() -> Self {
39        UseContextCmd {
40            context_name: DEFAULT_CONTEXT_NAME.to_string(),
41        }
42    }
43}
44
45#[async_trait]
46impl CliCommand for UseContextCmd {
47    fn explain(&self) -> String {
48        let context_name = &self.context_name;
49        format!("use context {context_name}")
50    }
51
52    fn login_required(&self) -> bool {
53        false
54    }
55
56    fn connection_required(&self) -> bool {
57        false
58    }
59
60    async fn execute_cmd(&mut self, _client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
61        let mut context_mgr = ContextManager::default();
62
63        context_mgr
64            .set_active_context_key(&self.context_name)
65            .await?;
66
67        event!(target: PRINT_TARGET, Level::INFO, "active context set to '{}'", self.context_name);
68
69        return Ok(());
70    }
71}