mz/command/
config.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Implementation of the `mz config` command.
17//!
18//! Consult the user-facing documentation for details.
19
20use serde::{Deserialize, Serialize};
21use tabled::Tabled;
22
23use crate::context::Context;
24use crate::error::Error;
25use crate::ui::OptionalStr;
26
27/// Represents the args to retrieve a *global* configuration value.
28pub struct GetArgs<'a> {
29    /// Represents the configuration field name.
30    pub name: &'a str,
31}
32
33/// Shows the value of a *global* configuration field.
34pub fn get(cx: &Context, GetArgs { name }: GetArgs<'_>) -> Result<(), Error> {
35    let value = cx.config_file().get_param(name)?;
36    cx.output_formatter().output_scalar(value)
37}
38
39/// Shows all the possible field and its values in the *global* configuration.
40pub fn list(cx: &Context) -> Result<(), Error> {
41    #[derive(Deserialize, Serialize, Tabled)]
42    pub struct ConfigParam<'a> {
43        #[tabled(rename = "Name")]
44        name: &'a str,
45        #[tabled(rename = "Value")]
46        value: OptionalStr<'a>,
47    }
48
49    let values = cx
50        .config_file()
51        .list_params()
52        .into_iter()
53        .map(|(name, value)| ConfigParam {
54            name,
55            value: OptionalStr(value),
56        });
57    cx.output_formatter().output_table(values)
58}
59
60/// Represents the args to set the value of a *global* configuration field.
61pub struct SetArgs<'a> {
62    /// Represents the name of the field to set the value.
63    pub name: &'a str,
64    /// Represents the new value of the field.
65    pub value: &'a str,
66}
67
68/// Sets the value of a global configuration field.
69pub async fn set(cx: &Context, SetArgs { name, value }: SetArgs<'_>) -> Result<(), Error> {
70    cx.config_file().set_param(name, Some(value)).await
71}
72
73/// Represents the args to remove the value of a *global* configuration field.
74pub struct RemoveArgs<'a> {
75    /// Represents the name of the field to remove.
76    pub name: &'a str,
77}
78
79/// Removes the value from a *global* configuration field.
80pub async fn remove(cx: &Context, RemoveArgs { name }: RemoveArgs<'_>) -> Result<(), Error> {
81    cx.config_file().set_param(name, None).await
82}