Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ConfigParams to reduce boilerplate
Browse files Browse the repository at this point in the history
koushiro committed Oct 23, 2024

Verified

This commit was signed with the committer’s verified signature.
steveloughran Steve Loughran
1 parent baeb5fd commit b7b0642
Showing 9 changed files with 77 additions and 62 deletions.
15 changes: 4 additions & 11 deletions bin/oli/src/commands/cat.rs
Original file line number Diff line number Diff line change
@@ -15,13 +15,11 @@
// 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::config::Config;
use crate::params::config::ConfigParams;

#[derive(Debug, clap::Parser)]
#[command(
@@ -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_params: ConfigParams,
#[arg()]
pub target: String,
}

impl CatCmd {
pub async fn run(&self) -> Result<()> {
let cfg = Config::load(&self.config)?;
let cfg = Config::load(&self.config_params.config)?;

let (op, path) = cfg.parse_location(&self.target)?;

14 changes: 4 additions & 10 deletions bin/oli/src/commands/cp.rs
Original file line number Diff line number Diff line change
@@ -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::config::Config;
use crate::params::config::ConfigParams;

#[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_params: ConfigParams,
#[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_params.config)?;

let (src_op, src_path) = cfg.parse_location(&self.source)?;

15 changes: 4 additions & 11 deletions bin/oli/src/commands/ls.rs
Original file line number Diff line number Diff line change
@@ -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::config::Config;
use crate::params::config::ConfigParams;

#[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_params: ConfigParams,
#[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_params.config)?;

let (op, path) = cfg.parse_location(&self.target)?;

9 changes: 1 addition & 8 deletions bin/oli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -15,21 +15,14 @@
// 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;
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),
15 changes: 4 additions & 11 deletions bin/oli/src/commands/rm.rs
Original file line number Diff line number Diff line change
@@ -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::config::Config;
use crate::params::config::ConfigParams;

#[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_params: ConfigParams,
#[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_params.config)?;

let (op, path) = cfg.parse_location(&self.target)?;

15 changes: 4 additions & 11 deletions bin/oli/src/commands/stat.rs
Original file line number Diff line number Diff line change
@@ -15,12 +15,10 @@
// specific language governing permissions and limitations
// under the License.

use std::path::PathBuf;

use anyhow::Result;

use crate::commands::default_config_path;
use crate::config::Config;
use crate::params::config::ConfigParams;

#[derive(Debug, clap::Parser)]
#[command(
@@ -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_params: ConfigParams,
#[arg()]
pub target: String,
}

impl StatCmd {
pub async fn run(&self) -> Result<()> {
let cfg = Config::load(&self.config)?;
let cfg = Config::load(&self.config_params.config)?;

let target = &self.target;
println!("path: {target}");
1 change: 1 addition & 0 deletions bin/oli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -17,3 +17,4 @@

pub mod commands;
pub mod config;
pub mod params;
35 changes: 35 additions & 0 deletions bin/oli/src/params/config.rs
Original file line number Diff line number Diff line change
@@ -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 ConfigParams {
/// 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()
}
20 changes: 20 additions & 0 deletions bin/oli/src/params/mod.rs
Original file line number Diff line number Diff line change
@@ -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 parameters.
pub mod config;

0 comments on commit b7b0642

Please sign in to comment.