Skip to content

Commit

Permalink
refactor: Delegate generationg config to require_config
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Mar 20, 2024
1 parent 0edbd4c commit 52f06ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
19 changes: 17 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ mod patch;
mod update;
mod version;

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};

use crate::config;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
Expand All @@ -34,10 +36,23 @@ enum Commands {
Init(init::Arguments),
}

fn require_config() -> Result<config::Config> {
if config::find_config_path().is_none() {
return Err(anyhow!("Configuratio file is not exists."));
}
return Ok(config::load_config().unwrap());
}

pub fn run_command() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Info(args)) => info::execute(args),
Some(Commands::Info(args)) => {
let config = require_config();
if config.is_err() {
return Err(config.unwrap_err());
}
info::execute(args, config.unwrap())
}
Some(Commands::Version(args)) => version::execute(args),
Some(Commands::Init(args)) => init::execute(args),
Some(Commands::Update(args)) => update::execute(args),
Expand Down
21 changes: 8 additions & 13 deletions src/commands/info.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
/* 'info' command displays data from config-file.
*/
use crate::config;
use crate::config::Config;
use crate::versioning;

use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::Args;

#[derive(Args)]
pub(crate) struct Arguments {}

pub(crate) fn execute(_args: &Arguments) -> Result<()> {
if config::find_config_path().is_none() {
return Err(anyhow!("Configuratio file is not exists."));
}
// Load config file.
let config_data = config::load_config().unwrap();
pub(crate) fn execute(_args: &Arguments, config: Config) -> Result<()> {
println!("# Version info");
println!();
println!("- Current: {}", config_data.current_version);
println!("- Current: {}", config.current_version);
println!(
"- Next major: {}",
versioning::up_major(&config_data.current_version)
versioning::up_major(&config.current_version)
);
println!(
"- Next minor: {}",
versioning::up_minor(&config_data.current_version)
versioning::up_minor(&config.current_version)
);
println!(
"- Next patch: {}",
versioning::up_patch(&config_data.current_version)
versioning::up_patch(&config.current_version)
);
println!();
println!("# Replace targets");
println!();
for f in config_data.get_files() {
for f in config.get_files() {
println!("- {}", f.path.to_str().unwrap())
}
// Display infomation data.
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ search = "version = \"{{current_version}}\""
replace = "version = \"{{new_version}}\""
"#;

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub current_version: Version,
files: Vec<FileConfig>,
}

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileConfig {
pub path: PathBuf,
pub search: String,
Expand Down

0 comments on commit 52f06ae

Please sign in to comment.