Skip to content

Commit

Permalink
add ConfigArgs to reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro committed Oct 23, 2024
1 parent baeb5fd commit 3808562
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 62 deletions.
35 changes: 35 additions & 0 deletions bin/oli/src/args/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 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()
}
20 changes: 20 additions & 0 deletions bin/oli/src/args/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 arguments.
pub mod config;
15 changes: 4 additions & 11 deletions bin/oli/src/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)?;

Expand Down
14 changes: 4 additions & 10 deletions bin/oli/src/commands/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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)?;

Expand Down
15 changes: 4 additions & 11 deletions bin/oli/src/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)?;

Expand Down
9 changes: 1 addition & 8 deletions bin/oli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
15 changes: 4 additions & 11 deletions bin/oli/src/commands/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)?;

Expand Down
15 changes: 4 additions & 11 deletions bin/oli/src/commands/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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}");
Expand Down
1 change: 1 addition & 0 deletions bin/oli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// specific language governing permissions and limitations
// under the License.

pub mod args;
pub mod commands;
pub mod config;

0 comments on commit 3808562

Please sign in to comment.