You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/03/24 16:57:32 UTC
[incubator-opendal] branch main updated: feat(oli): implement `oli ls` (#1755)
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/main by this push:
new d2d56b37 feat(oli): implement `oli ls` (#1755)
d2d56b37 is described below
commit d2d56b3735b1687a920977c7728b8f130b5aa5a0
Author: Jian Zeng <an...@gmail.com>
AuthorDate: Sat Mar 25 00:57:27 2023 +0800
feat(oli): implement `oli ls` (#1755)
Signed-off-by: Jian Zeng <an...@gmail.com>
---
bin/oli/src/bin/oli.rs | 4 +++
bin/oli/src/commands/cli.rs | 2 ++
bin/oli/src/commands/ls.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++
bin/oli/src/commands/mod.rs | 1 +
4 files changed, 85 insertions(+)
diff --git a/bin/oli/src/bin/oli.rs b/bin/oli/src/bin/oli.rs
index 62876570..b3d58a8e 100644
--- a/bin/oli/src/bin/oli.rs
+++ b/bin/oli/src/bin/oli.rs
@@ -69,6 +69,10 @@ async fn main() -> Result<()> {
let cmd = oli::commands::cp::cli(new_cmd("ocp")?);
oli::commands::cp::main(&cmd.get_matches()).await?;
}
+ Some("ols") => {
+ let cmd = oli::commands::ls::cli(new_cmd("ols")?);
+ oli::commands::ls::main(&cmd.get_matches()).await?;
+ }
Some(v) => {
println!("{v} is not supported")
}
diff --git a/bin/oli/src/commands/cli.rs b/bin/oli/src/commands/cli.rs
index 645cfdbe..b29ee92b 100644
--- a/bin/oli/src/commands/cli.rs
+++ b/bin/oli/src/commands/cli.rs
@@ -23,6 +23,7 @@ use clap::Command;
pub async fn main(args: &ArgMatches) -> Result<()> {
match args.subcommand() {
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")),
}
@@ -33,4 +34,5 @@ pub fn cli(cmd: Command) -> Command {
cmd.version("0.10.0")
.about("OpenDAL Command Line Interface")
.subcommand(super::cp::cli(Command::new("cp")))
+ .subcommand(super::ls::cli(Command::new("ls")))
}
diff --git a/bin/oli/src/commands/ls.rs b/bin/oli/src/commands/ls.rs
new file mode 100644
index 00000000..65bd80c6
--- /dev/null
+++ b/bin/oli/src/commands/ls.rs
@@ -0,0 +1,78 @@
+// 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::collections::VecDeque;
+use std::path::PathBuf;
+
+use anyhow::{anyhow, Result};
+use clap::{Arg, ArgAction, ArgMatches, Command};
+use futures::TryStreamExt;
+use opendal::Metakey;
+
+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 recursive = args.get_flag("recursive");
+
+ let target = args
+ .get_one::<String>("target")
+ .ok_or_else(|| anyhow!("missing target"))?;
+ let (op, path) = cfg.parse_location(target)?;
+
+ if !recursive {
+ let mut ds = op.list(path).await?;
+ while let Some(de) = ds.try_next().await? {
+ println!("{}", de.name());
+ }
+ return Ok(());
+ }
+
+ let mut queue = VecDeque::from([path.to_string()]);
+ while !queue.is_empty() {
+ let path = queue.pop_front().unwrap();
+ let mut ds = op.list(&path).await?;
+ while let Some(de) = ds.try_next().await? {
+ let meta = op.metadata(&de, Metakey::Mode).await?;
+ if meta.mode().is_dir() {
+ let d = de.path().to_owned();
+ queue.push_back(d);
+ } else {
+ println!("{}", de.path());
+ }
+ }
+ }
+ Ok(())
+}
+
+pub fn cli(cmd: Command) -> Command {
+ cmd.version("0.10.0")
+ .about("ls")
+ .arg(Arg::new("target").required(true))
+ .arg(
+ Arg::new("recursive")
+ .required(false)
+ .long("recursive")
+ .short('r')
+ .help("List recursively")
+ .action(ArgAction::SetTrue),
+ )
+}
diff --git a/bin/oli/src/commands/mod.rs b/bin/oli/src/commands/mod.rs
index 29917e4d..1520c8fb 100644
--- a/bin/oli/src/commands/mod.rs
+++ b/bin/oli/src/commands/mod.rs
@@ -35,3 +35,4 @@
pub mod cli;
pub mod cp;
+pub mod ls;