diff --git a/bin/oli/src/args/config.rs b/bin/oli/src/args/config.rs new file mode 100644 index 000000000000..22bb1fa584d5 --- /dev/null +++ b/bin/oli/src/args/config.rs @@ -0,0 +1,35 @@ +// 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::ffi::OsString; +use std::path::PathBuf; + +#[derive(Debug, clap::Args)] +pub struct ConfigArgs { + /// Path to the config file. + #[arg( + long, + default_value = default_config_path(), + value_parser = clap::value_parser!(PathBuf) + )] + pub config: PathBuf, +} + +fn default_config_path() -> OsString { + let d = dirs::config_dir().expect("unknown config dir"); + d.join("oli/config.toml").as_os_str().to_owned() +} diff --git a/bin/oli/src/args/mod.rs b/bin/oli/src/args/mod.rs new file mode 100644 index 000000000000..4c8509c74f30 --- /dev/null +++ b/bin/oli/src/args/mod.rs @@ -0,0 +1,20 @@ +// 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. + +//! Provides the implementation of common arguments. + +pub mod config; diff --git a/bin/oli/src/commands/cat.rs b/bin/oli/src/commands/cat.rs index 55456300caaa..471cda961769 100644 --- a/bin/oli/src/commands/cat.rs +++ b/bin/oli/src/commands/cat.rs @@ -15,12 +15,10 @@ // specific language governing permissions and limitations // under the License. -use std::path::PathBuf; - use anyhow::Result; use futures::io; -use crate::commands::default_config_path; +use crate::args::config::ConfigArgs; use crate::config::Config; #[derive(Debug, clap::Parser)] @@ -30,20 +28,15 @@ use crate::config::Config; disable_version_flag = true )] pub struct CatCmd { - /// Path to the config file. - #[arg( - long, - default_value = default_config_path(), - value_parser = clap::value_parser!(PathBuf) - )] - pub config: PathBuf, + #[command(flatten)] + pub config_args: ConfigArgs, #[arg()] pub target: String, } impl CatCmd { pub async fn run(&self) -> Result<()> { - let cfg = Config::load(&self.config)?; + let cfg = Config::load(&self.config_args.config)?; let (op, path) = cfg.parse_location(&self.target)?; diff --git a/bin/oli/src/commands/cp.rs b/bin/oli/src/commands/cp.rs index 583b69ea202b..f97be9ee17de 100644 --- a/bin/oli/src/commands/cp.rs +++ b/bin/oli/src/commands/cp.rs @@ -16,25 +16,19 @@ // under the License. use std::path::Path; -use std::path::PathBuf; use anyhow::Result; use futures::AsyncWriteExt; use futures::TryStreamExt; -use crate::commands::default_config_path; +use crate::args::config::ConfigArgs; use crate::config::Config; #[derive(Debug, clap::Parser)] #[command(name = "cp", about = "Copy object", disable_version_flag = true)] pub struct CopyCmd { - /// Path to the config file. - #[arg( - long, - default_value = default_config_path(), - value_parser = clap::value_parser!(PathBuf) - )] - pub config: PathBuf, + #[command(flatten)] + pub config_args: ConfigArgs, #[arg()] pub source: String, #[arg()] @@ -46,7 +40,7 @@ pub struct CopyCmd { impl CopyCmd { pub async fn run(&self) -> Result<()> { - let cfg = Config::load(&self.config)?; + let cfg = Config::load(&self.config_args.config)?; let (src_op, src_path) = cfg.parse_location(&self.source)?; diff --git a/bin/oli/src/commands/ls.rs b/bin/oli/src/commands/ls.rs index c3a6253b1184..64e4d8336900 100644 --- a/bin/oli/src/commands/ls.rs +++ b/bin/oli/src/commands/ls.rs @@ -15,24 +15,17 @@ // specific language governing permissions and limitations // under the License. -use std::path::PathBuf; - use anyhow::Result; use futures::TryStreamExt; -use crate::commands::default_config_path; +use crate::args::config::ConfigArgs; use crate::config::Config; #[derive(Debug, clap::Parser)] #[command(name = "ls", about = "List object", disable_version_flag = true)] pub struct LsCmd { - /// Path to the config file. - #[arg( - long, - default_value = default_config_path(), - value_parser = clap::value_parser!(PathBuf) - )] - pub config: PathBuf, + #[command(flatten)] + pub config_args: ConfigArgs, #[arg()] pub target: String, /// List objects recursively. @@ -42,7 +35,7 @@ pub struct LsCmd { impl LsCmd { pub async fn run(&self) -> Result<()> { - let cfg = Config::load(&self.config)?; + let cfg = Config::load(&self.config_args.config)?; let (op, path) = cfg.parse_location(&self.target)?; diff --git a/bin/oli/src/commands/mod.rs b/bin/oli/src/commands/mod.rs index 3d65d617c0ed..e70d0c101a69 100644 --- a/bin/oli/src/commands/mod.rs +++ b/bin/oli/src/commands/mod.rs @@ -15,9 +15,7 @@ // specific language governing permissions and limitations // under the License. -//! Commands provides the implementation of each command. - -use std::ffi::OsString; +//! Provides the implementation of each command. pub mod cat; pub mod cp; @@ -25,11 +23,6 @@ pub mod ls; pub mod rm; pub mod stat; -fn default_config_path() -> OsString { - let d = dirs::config_dir().expect("unknown config dir"); - d.join("oli/config.toml").as_os_str().to_owned() -} - #[derive(Debug, clap::Subcommand)] pub enum OliSubcommand { Cat(cat::CatCmd), diff --git a/bin/oli/src/commands/rm.rs b/bin/oli/src/commands/rm.rs index 581e609edc2b..07c4283f957a 100644 --- a/bin/oli/src/commands/rm.rs +++ b/bin/oli/src/commands/rm.rs @@ -15,23 +15,16 @@ // specific language governing permissions and limitations // under the License. -use std::path::PathBuf; - use anyhow::Result; -use crate::commands::default_config_path; +use crate::args::config::ConfigArgs; use crate::config::Config; #[derive(Debug, clap::Parser)] #[command(name = "rm", about = "Remove object", disable_version_flag = true)] pub struct RmCmd { - /// Path to the config file. - #[arg( - long, - default_value = default_config_path(), - value_parser = clap::value_parser!(PathBuf) - )] - pub config: PathBuf, + #[command(flatten)] + pub config_args: ConfigArgs, #[arg()] pub target: String, /// Remove objects recursively. @@ -41,7 +34,7 @@ pub struct RmCmd { impl RmCmd { pub async fn run(&self) -> Result<()> { - let cfg = Config::load(&self.config)?; + let cfg = Config::load(&self.config_args.config)?; let (op, path) = cfg.parse_location(&self.target)?; diff --git a/bin/oli/src/commands/stat.rs b/bin/oli/src/commands/stat.rs index 26c24fe4a977..19fe0b587234 100644 --- a/bin/oli/src/commands/stat.rs +++ b/bin/oli/src/commands/stat.rs @@ -15,11 +15,9 @@ // specific language governing permissions and limitations // under the License. -use std::path::PathBuf; - use anyhow::Result; -use crate::commands::default_config_path; +use crate::args::config::ConfigArgs; use crate::config::Config; #[derive(Debug, clap::Parser)] @@ -29,20 +27,15 @@ use crate::config::Config; disable_version_flag = true )] pub struct StatCmd { - /// Path to the config file. - #[arg( - long, - default_value = default_config_path(), - value_parser = clap::value_parser!(PathBuf) - )] - pub config: PathBuf, + #[command(flatten)] + pub config_args: ConfigArgs, #[arg()] pub target: String, } impl StatCmd { pub async fn run(&self) -> Result<()> { - let cfg = Config::load(&self.config)?; + let cfg = Config::load(&self.config_args.config)?; let target = &self.target; println!("path: {target}"); diff --git a/bin/oli/src/lib.rs b/bin/oli/src/lib.rs index 7f0fce639553..23a3b29811cb 100644 --- a/bin/oli/src/lib.rs +++ b/bin/oli/src/lib.rs @@ -15,5 +15,6 @@ // specific language governing permissions and limitations // under the License. +pub mod args; pub mod commands; pub mod config;