Skip to main content

cli/metadata/control/
put.rs

1// Copyright 2023 Greptime Team
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 at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod key;
16mod table;
17
18use clap::Subcommand;
19use common_error::ext::BoxedError;
20use snafu::ResultExt;
21use tokio::io::{AsyncRead, AsyncReadExt};
22
23use crate::Tool;
24use crate::error::FileIoSnafu;
25use crate::metadata::control::put::key::PutKeyCommand;
26use crate::metadata::control::put::table::PutTableCommand;
27
28pub(crate) async fn read_value<R>(mut reader: R) -> Result<Vec<u8>, BoxedError>
29where
30    R: AsyncRead + Unpin,
31{
32    let mut value = Vec::new();
33    reader
34        .read_to_end(&mut value)
35        .await
36        .context(FileIoSnafu)
37        .map_err(BoxedError::new)?;
38    Ok(value)
39}
40
41/// Subcommand for putting metadata into the metadata store.
42#[derive(Subcommand)]
43pub enum PutCommand {
44    Key(PutKeyCommand),
45    #[clap(subcommand)]
46    Table(PutTableCommand),
47}
48
49impl PutCommand {
50    pub async fn build(&self) -> Result<Box<dyn Tool>, BoxedError> {
51        match self {
52            PutCommand::Key(cmd) => cmd.build().await,
53            PutCommand::Table(cmd) => cmd.build().await,
54        }
55    }
56}