Skip to content

Commit

Permalink
refactor: config resolver require target directory from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 1, 2024
1 parent 0035d3b commit a415441
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod minor;
mod patch;
mod update;

use std::env::current_dir;

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

Expand Down Expand Up @@ -39,7 +41,8 @@ pub fn run_command() -> Result<()> {
if let Some(Commands::Init(args)) = cli.command {
return init::execute(&args);
};
let resolved = resolve_config();
let pwd = current_dir().unwrap();
let resolved = resolve_config(&pwd);
if resolved.is_err() {
return Err(anyhow!(resolved.unwrap_err()));
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub(crate) struct Arguments {
}

pub(crate) fn execute(args: &Arguments) -> Result<()> {
if config::resolve_config().is_ok() {
let pwd = current_dir().unwrap();
if config::resolve_config(&pwd).is_ok() {
return Err(anyhow!("Configuration file is already exists."));
}
let target = current_dir()?.join(Path::new(config::DEFAULT_FILENAME));
Expand Down
6 changes: 2 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use anyhow::{anyhow, Result};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::env::current_dir;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use toml::de::Error;
Expand All @@ -23,9 +22,8 @@ pub struct FileConfig {
pub replace: String,
}

pub fn resolve_config() -> Result<Config> {
let pwd = current_dir().unwrap();
let config_path = pwd.join(Path::new(DEFAULT_FILENAME));
pub fn resolve_config(root: &PathBuf) -> Result<Config> {
let config_path = root.join(Path::new(DEFAULT_FILENAME));
if !config_path.exists() {
return Err(anyhow!("Configuratio file is not exists."));
}
Expand Down

0 comments on commit a415441

Please sign in to comment.