Skip to content

Commit

Permalink
Merge pull request #14 from sdttttt/develop
Browse files Browse the repository at this point in the history
Feature: grc configuration file parsing.
  • Loading branch information
sdttttt authored Oct 31, 2020
2 parents 019a6a9 + f3f696d commit 6d0a5d7
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Publish to crates.io
run: |
cargo login ${{ env.CARGO_LOGIN_TOKEN }}
cargo publish -v
make publish
env:
CARGO_LOGIN_TOKEN: ${{ secrets.CARGO_LOGIN_TOKEN }}

Expand Down Expand Up @@ -82,7 +82,7 @@ jobs:
run: |
cargo build --release --verbose
zip --junk-paths grc.zip ./target/release/grc
- name: Build on Windows
if: ${{ runner.os == 'Windows' }}
run: |
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust

on:
push:
branches: [ master, develop ]
branches: [ master ]
pull_request:
branches: "*"
branches: [ master ]

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -36,16 +36,16 @@ jobs:
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Lint Code Base
if: ${{ runner.os != 'Windows' }}
uses: github/super-linter@v3
env:
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# - name: Lint Code Base
# if: ${{ runner.os != 'Windows' }}
# uses: github/super-linter@v3
# env:
# VALIDATE_ALL_CODEBASE: false
# DEFAULT_BRANCH: master
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run tests
run: cargo test --verbose
run: make test

- name: Run cargo-tarpaulin
if: ${{ runner.os == 'Linux' }}
Expand All @@ -56,6 +56,6 @@ jobs:
- name: Upload to codecov.io
if: ${{ runner.os == 'Linux' }}
run: bash <(curl -s https://codecov.io/bash)

- name: Build
run: cargo build --release --verbose
run: make
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"editor.fontSize": 17,
"editor.fontFamily": "Consolas,Fira Code, Cascadia Code, 'Courier New', monospace"
"editor.fontFamily": "Consolas, 'Courier New', monospace"
}
76 changes: 71 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "grc"
version = "0.8.1"
version = "0.9.0-rc1"
authors = ["sdttttt <[email protected]>"]
description = "Similar to git-cz, gcr will help you to provide a better Git experience."
edition = "2018"
Expand All @@ -10,8 +10,13 @@ keywords = ["git-cz", "git", "cli"]
categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.release]
lto = "fat"

[dependencies]
clap = "2.33.3"
console = "0.13.0"
git2 = "0.13"
dialoguer = "0.6.2"
toml = "0.5.7"
serde = { version = "1.0.82", features = ["derive"] }
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ grc -a .
grc -a <filename>...
```

### GRC config file

> **TIP:**
> This feature is supported above version 0.9.0
You can append custom commit types in the `grc.toml` configuration file:

```toml
# A semicolon separates the type from the description of the type.
type = [
"type: this is new commit type."
]
```

## IDEA

If you have any new ideas, you are welcome to talk to me.

`grc` has taken over all git submissions from @sdttttt.
1 change: 1 addition & 0 deletions grc.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type = [ "123" ]
1 change: 1 addition & 0 deletions grc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type = [ "version: version is change." ]
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build-dev: fmt

.PHONY: run
run: fmt
$(c) run -a .
$(c) run

.PHONY: test
test: fmt
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use_small_heuristics = "Max"
merge_derives = false
6 changes: 3 additions & 3 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ mod tests {
}

#[test]
fn add_all_mode() {
fn add_all_mode() -> Result<(), &'static str> {
let args = quick_command_run(vec!["grc", "--add", "."]);
match args.command_mode() {
Mode::AddAll => {}
_ => panic!("NOT ADDALL MODE!"),
Mode::AddAll => Ok(()),
_ => Err("NOT ADDALL MODE!"),
}
}

Expand Down
102 changes: 102 additions & 0 deletions src/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::fs;
use std::io::{Error, ErrorKind};

use serde::Deserialize;

use crate::metadata::GRC_CONFIG_FILE_NAME;

#[derive(Deserialize)]
pub struct Extensions {
#[serde(rename = "type")]
typ: Vec<String>,
}

impl Extensions {
pub fn from_agreement() -> Result<Self, Error> {
let file_str = Self::read_config_file(GRC_CONFIG_FILE_NAME)?;
Ok(Self::deserialize(file_str)?)
}

pub fn from(filename: &str) -> Result<Self, Error> {
let file_str = Self::read_config_file(filename)?;
Ok(Self::deserialize(file_str)?)
}

pub fn types(&self) -> &Vec<String> {
&self.typ
}

fn deserialize(file_str: String) -> Result<Self, Error> {
if file_str.len() == 0 || file_str == "" {
return Ok(Self { typ: vec![] });
}

let config = toml::from_str::<Extensions>(file_str.as_str())?;
Ok(config)
}

fn read_config_file(filename: &str) -> Result<String, Error> {
match fs::read_to_string(filename) {
Ok(content) => Ok(content),
Err(e) => {
if e.kind() == ErrorKind::NotFound {
Ok(String::new())
} else {
Err(e)
}
}
}
}
}

#[cfg(test)]
mod tests {

use super::*;

const GRC_TEST_CONFIG_FILE_NAME: &str = "grc.test.toml";

const GRC_TOML_CONTENT: &str = r#"type = [ "version: version is change." ]"#;
const GRC_TOML_TYPE: &str = "version: version is change.";

const GRC_TEST_TOML_CONTENT: &str = r#"type = [ "123" ]"#;
const GRC_TEST_TOML_TYPE: &str = "123";

#[test]
fn it_read_config_file() {
let file_str = Extensions::read_config_file(GRC_TEST_CONFIG_FILE_NAME).unwrap();
assert_eq!(file_str.as_str(), GRC_TEST_TOML_CONTENT);

let file_str2 = Extensions::read_config_file("nullfile").unwrap();
assert_eq!(file_str2.len(), 0);
assert_eq!(file_str2.as_str(), "");

let config = Extensions::read_config_file(GRC_CONFIG_FILE_NAME).unwrap();
assert_eq!(config.as_str(), GRC_TOML_CONTENT);
}

#[test]
fn it_deserialize() {
let file_str = String::from(GRC_TOML_CONTENT);
let result = Extensions::deserialize(file_str).unwrap();

let types = result.typ;
assert_eq!(types[0], GRC_TOML_TYPE);
}

#[test]
fn it_from_agreement() {
let config = Extensions::from_agreement().unwrap();
let types = config.typ;

assert_eq!(types[0], GRC_TOML_TYPE);
}

#[test]
fn it_from() {
let config = Extensions::from(GRC_TEST_CONFIG_FILE_NAME).unwrap();
let types = config.typ;

assert_eq!(types[0], GRC_TEST_TOML_TYPE);
}
}
Loading

0 comments on commit 6d0a5d7

Please sign in to comment.