Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/32: refactoring - move Config and questions to their own crates #33

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: cargo install cargo-v

- name: Generate new version
run: cargo v ${{ github.event.inputs.version }}
run: cargo v ${{ github.event.inputs.version }} -y

- name: Push new version tag to repository
run: git push origin main --tags
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ The above command will do:
- create a git commit with new version;
- and create a git tag with new version.

Before do a `git commit` and `git tag`, you will be asked if everything is ok with your project.
To automatically answer **yes** to all questions, just use the flag `-y` or `--yes`:

```sh
cargo v <version> -y
```

To see all possible options, just run `cargo v --help`.

## Author
Expand Down
4 changes: 3 additions & 1 deletion src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ fn help_message() {
let usage = concat!(
"USAGE:\n",
" cargo v <version>\n",
" cargo v <version> -y\n",
" cargo v [options]\n",
"ARGUMENTS:\n",
" version Can be one of \"patch\", \"minor\", \"major\" or a string like \"v1.2.5\"\n",
"OPTIONS:\n",
" -h, --help Prints this message."
" -h, --help Prints this message.",
" -y, --yes Answer \"yes\" for all questions",
);

println!("{usage}");
Expand Down
78 changes: 7 additions & 71 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
use std::error::Error;
use std::{env, io};

pub mod help;
pub mod main_actions;
use main_actions::*;

#[derive(Debug)]
struct Config {
version: Option<String>,
help: bool,
yes_to_all: bool,
}

impl Config {
fn from_iter(args: &mut impl Iterator<Item = String>) -> Self {
let mut config = Self {
version: None,
help: false,
yes_to_all: false,
};

for arg in args.by_ref() {
match arg.as_str() {
"-h" | "--help" => config.help = true,
"-y" | "--yes" => config.yes_to_all = true,
other => config.version = Some(String::from(other)),
};
}
mod parse_cli;
mod questions;

config
}
}
use main_actions::*;
use parse_cli::*;
use std::env;

fn main() {
let mut args = env::args().skip(2);
let config = Config::from_iter(&mut args);

dbg!(&config);
let config = CliConfig::from_iter(&mut args);

if config.help {
help::usage();
Expand All @@ -51,7 +24,7 @@ fn main() {
let cargo_lock = get_cargo_file("./Cargo.lock");
let new_version = get_new_version(&version, &cargo_toml);

should_show_questions(config.yes_to_all);
questions::ask_questions(config.yes_to_all);

let cargo_toml_updated = get_cargo_toml_updated(&cargo_toml, &new_version);
let cargo_lock_updated =
Expand All @@ -64,40 +37,3 @@ fn main() {
try_to_git_commit(&new_version);
try_to_git_tag(&new_version);
}

fn should_show_questions(yes_to_all: bool) {
if yes_to_all {
return;
}

match questions() {
Ok(_) => (),
Err(e) => help::error_exit(&e.to_string()),
};
}

fn questions() -> Result<(), Box<dyn Error>> {
let mut answer1 = String::new();
let mut answer2 = String::new();

println!("Have you already run the command `cargo build` and your application has no erros? [Y]es [N]o:");
io::stdin().read_line(&mut answer1)?;
let answer1 = answer1.to_lowercase();

if answer1 != "y" || answer1 != "yes" {
println!("Answer 1: {}", answer1);
println!("WARN: run `cargo build` and make sure the project has no errors before using `cargo-v`");
return Ok(());
}

println!("All important files are \"git added\" and \"commited\" (specially `Cargo.toml` and `Cargo.lock`)? [Y]es [N]o:");
io::stdin().read_line(&mut answer2)?;
let answer2 = answer2.to_lowercase();
if answer2 != "y" || answer2 != "yes" {
println!("Answer 2: {}", answer2);
println!("WARN: please, commit all important files before run `cargo-v`");
return Ok(());
}

Ok(())
}
26 changes: 26 additions & 0 deletions src/parse_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[derive(Debug)]
pub struct CliConfig {
pub version: Option<String>,
pub help: bool,
pub yes_to_all: bool,
}

impl CliConfig {
pub fn from_iter(args: &mut impl Iterator<Item = String>) -> Self {
let mut config = Self {
version: None,
help: false,
yes_to_all: false,
};

for arg in args.by_ref() {
match arg.as_str() {
"-h" | "--help" => config.help = true,
"-y" | "--yes" => config.yes_to_all = true,
other => config.version = Some(String::from(other)),
};
}

config
}
}
50 changes: 50 additions & 0 deletions src/questions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::help;
use std::error::Error;
use std::io::{self, Write};

pub fn ask_questions(yes_to_all: bool) {
if yes_to_all {
return;
}

match questions() {
Ok(_) => (),
Err(e) => help::error_exit(&e.to_string()),
};
}

fn questions() -> Result<(), Box<dyn Error>> {
let question1 = create_question("Have you run the command `cargo build` already to make sure your application has no erros? [Y]es [N]o: ")?;
if not(question1.yes) {
return Err("run `cargo build` and make sure the project has no errors before using `cargo-v`")?;
}

let question2 = create_question("All important files are \"git added\" and \"commited\" (specially `Cargo.toml` and `Cargo.lock`)? [Y]es [N]o: ")?;
if not(question2.yes) {
return Err("please, commit all important files before run `cargo-v`")?;
}

Ok(())
}

struct Answer {
yes: bool,
}

fn create_question(question: &str) -> Result<Answer, Box<dyn Error>> {
let mut answer = String::new();
print!("{question}");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut answer)?;
let answer = answer.trim().to_lowercase();

if answer == "y" || answer == "yes" {
return Ok(Answer { yes: true });
}

Ok(Answer { yes: false })
}

fn not(value: bool) -> bool {
!value
}