Skip to content

Commit

Permalink
feat(oli): implement oli cat (#1759)
Browse files Browse the repository at this point in the history
* feat(oli): implement `oli cat`

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>

* refactor: switch to tokio::io::copy

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>

---------

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
  • Loading branch information
knight42 authored Mar 25, 2023
1 parent 6f24705 commit 0069c92
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bin/oli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ futures = "0.3"
log = "0.4"
opendal = { version = "0.30", path = "../../core" }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1.26", features = ["fs", "macros", "rt-multi-thread"] }
tokio = { version = "1.26", features = ["fs", "macros", "rt-multi-thread", "io-std"] }
toml = "0.7.3"
url = "2.3.1"

Expand Down
4 changes: 4 additions & 0 deletions bin/oli/src/bin/oli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ async fn main() -> Result<()> {
let cmd = oli::commands::cli::cli(new_cmd("oli")?);
oli::commands::cli::main(&cmd.get_matches()).await?;
}
Some("ocat") => {
let cmd = oli::commands::cat::cli(new_cmd("ocat")?);
oli::commands::cat::main(&cmd.get_matches()).await?;
}
Some("ocp") => {
let cmd = oli::commands::cp::cli(new_cmd("ocp")?);
oli::commands::cp::main(&cmd.get_matches()).await?;
Expand Down
47 changes: 47 additions & 0 deletions bin/oli/src/commands/cat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::path::PathBuf;

use anyhow::{anyhow, Result};
use clap::{Arg, ArgMatches, Command};
use tokio::io;

use crate::config::Config;

pub async fn main(args: &ArgMatches) -> Result<()> {
let config_path = args
.get_one::<PathBuf>("config")
.ok_or_else(|| anyhow!("missing config path"))?;
let cfg = Config::load(config_path)?;

let target = args
.get_one::<String>("target")
.ok_or_else(|| anyhow!("missing target"))?;
let (op, path) = cfg.parse_location(target)?;

let mut reader = op.reader(path).await?;
let mut stdout = io::stdout();
io::copy(&mut reader, &mut stdout).await?;
Ok(())
}

pub fn cli(cmd: Command) -> Command {
cmd.version("0.10.0")
.about("display object content")
.arg(Arg::new("target").required(true))
}
2 changes: 2 additions & 0 deletions bin/oli/src/commands/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use clap::Command;

pub async fn main(args: &ArgMatches) -> Result<()> {
match args.subcommand() {
Some(("cat", sub_args)) => super::cat::main(sub_args).await?,
Some(("cp", sub_args)) => super::cp::main(sub_args).await?,
Some(("ls", sub_args)) => super::ls::main(sub_args).await?,
_ => return Err(anyhow!("not handled")),
Expand All @@ -33,6 +34,7 @@ pub async fn main(args: &ArgMatches) -> Result<()> {
pub fn cli(cmd: Command) -> Command {
cmd.version("0.10.0")
.about("OpenDAL Command Line Interface")
.subcommand(super::cat::cli(Command::new("cat")))
.subcommand(super::cp::cli(Command::new("cp")))
.subcommand(super::ls::cli(Command::new("ls")))
}
1 change: 1 addition & 0 deletions bin/oli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
//! }
//! ```

pub mod cat;
pub mod cli;
pub mod cp;
pub mod ls;

0 comments on commit 0069c92

Please sign in to comment.