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 f7cb049 commit 7148472
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
14 changes: 13 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ mod minor;
mod patch;
mod update;

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 @@ -31,6 +33,16 @@ enum Commands {
Init(init::Arguments),
}

/**
* Resolve valid configuration file and parse struct.
*/
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 {
Expand Down
22 changes: 11 additions & 11 deletions src/commands/info.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
/* 'info' command displays data from config-file.
*/
use crate::config;
use crate::commands::require_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."));
let config = require_config();
if config.is_err() {
return Err(config.unwrap_err());
}
// Load config file.
let config_data = config::load_config().unwrap();
let config = config.unwrap();
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 7148472

Please sign in to comment.