Skip to content

Commit

Permalink
refactor cli config adjustments into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Jun 5, 2024
1 parent 5990048 commit 859b671
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
47 changes: 47 additions & 0 deletions src/cli_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{
cli::{
CategoryOperation, ConfigSubcommands,
ConfigSubcommands::{Category, ChangeType, LegacyVersion, Show, Spelling, TargetRepo},
HashMapOperation, OptionalOperation,
},
config, errors,
};
use std::fs;

// Handles the CLI subcommands to adjust the configuration file.
pub fn adjust_config(config_subcommand: ConfigSubcommands) -> Result<(), errors::CLIError> {
let mut configuration = config::load()?;

match config_subcommand {
Category(args) => match args.command {
CategoryOperation::Add { value } => config::add_category(&mut configuration, value)?,
CategoryOperation::Remove { value } => {
config::remove_category(&mut configuration, value)?
}
},
ChangeType(args) => match args.command {
HashMapOperation::Add { key, value } => {
config::add_into_hashmap(&mut configuration.change_types, key, value)?
}
HashMapOperation::Remove { key } => {
config::remove_from_hashmap(&mut configuration.change_types, key)?
}
},
Show => println!("{}", configuration),
Spelling(args) => match args.command {
HashMapOperation::Add { key, value } => {
config::add_into_hashmap(&mut configuration.expected_spellings, key, value)?
}
HashMapOperation::Remove { key } => {
config::remove_from_hashmap(&mut configuration.expected_spellings, key)?
}
},
LegacyVersion(args) => match args.command {
OptionalOperation::Set { value } => configuration.legacy_version = Some(value),
OptionalOperation::Unset => configuration.legacy_version = None,
},
TargetRepo(args) => config::set_target_repo(&mut configuration, args.value)?,
}

Ok(fs::write(".clconfig.json", format!("{}", configuration))?)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod change_type;
mod changelog;
pub mod cli;
pub mod cli_config;
pub mod config;
mod entry;
pub mod errors;
Expand Down
49 changes: 2 additions & 47 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,15 @@
Main file to run the changelog utils application.
*/
use clap::Parser;
use clu::{
cli::{
CategoryOperation, ChangelogCLI,
ConfigSubcommands::{Category, ChangeType, LegacyVersion, Show, Spelling, TargetRepo},
HashMapOperation, OptionalOperation,
},
config,
errors::CLIError,
init, lint,
};
use std::fs;
use clu::{cli::ChangelogCLI, cli_config, errors::CLIError, init, lint};

fn main() -> Result<(), CLIError> {
match ChangelogCLI::parse() {
ChangelogCLI::Fix => Ok(lint::run(true)?),
ChangelogCLI::Lint => Ok(lint::run(false)?),
ChangelogCLI::Init => Ok(init::run()?),
ChangelogCLI::Config(config_subcommand) => {
let mut configuration = config::load()?;

match config_subcommand {
Category(args) => match args.command {
CategoryOperation::Add { value } => {
config::add_category(&mut configuration, value)?
}
CategoryOperation::Remove { value } => {
config::remove_category(&mut configuration, value)?
}
},
ChangeType(args) => match args.command {
HashMapOperation::Add { key, value } => {
config::add_into_hashmap(&mut configuration.change_types, key, value)?
}
HashMapOperation::Remove { key } => {
config::remove_from_hashmap(&mut configuration.change_types, key)?
}
},
Show => println!("{}", configuration),
Spelling(args) => match args.command {
HashMapOperation::Add { key, value } => {
config::add_into_hashmap(&mut configuration.expected_spellings, key, value)?
}
HashMapOperation::Remove { key } => {
config::remove_from_hashmap(&mut configuration.expected_spellings, key)?
}
},
LegacyVersion(args) => match args.command {
OptionalOperation::Set { value } => configuration.legacy_version = Some(value),
OptionalOperation::Unset => configuration.legacy_version = None,
},
TargetRepo(args) => config::set_target_repo(&mut configuration, args.value)?,
}

Ok(fs::write(".clconfig.json", format!("{}", configuration))?)
Ok(cli_config::adjust_config(config_subcommand)?)
}
}
}

0 comments on commit 859b671

Please sign in to comment.