generated from fallion/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configuration file support
- adds viper library for loading configuration file - all flags reworked to work with viper - added default config for commits - added documentation for configuration file - added default config file for this repo Refs #pace29
- Loading branch information
Showing
12 changed files
with
232 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: 1 | ||
verbose: true | ||
commits: | ||
strict: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ renovate.json | |
.goreleaser.yml | ||
.kodiak.toml | ||
CNAME | ||
.github | ||
.github | ||
.commitsar.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/aevea/commitsar/internal/root_runner" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// CommitConfig will return the RunnerOptions using defaults unless overriden in config or flags | ||
func CommitConfig() root_runner.RunnerOptions { | ||
// defaults | ||
strict := true | ||
limit := 0 | ||
all := false | ||
|
||
if viper.IsSet("commits.strict") { | ||
strict = viper.GetBool("commits.strict") | ||
} | ||
|
||
if viper.IsSet("commits.limit") { | ||
limit = viper.GetInt("commits.limit") | ||
} | ||
|
||
if viper.IsSet("commits.all") { | ||
all = viper.GetBool("commits.all") | ||
} | ||
|
||
return root_runner.RunnerOptions{ | ||
Strict: strict, | ||
Limit: limit, | ||
AllCommits: all, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCommitConfig(t *testing.T) { | ||
os.Clearenv() | ||
|
||
defaultConfig := CommitConfig() | ||
|
||
assert.Equal(t, true, defaultConfig.Strict, "expect strict to be true by default") | ||
assert.Equal(t, 0, defaultConfig.Limit, "expect the limit to be 0 by default") | ||
assert.Equal(t, false, defaultConfig.AllCommits, "expect AllCommits to be true by default") | ||
|
||
err := os.Setenv(CommitsarConfigPath, "./testdata") | ||
assert.NoError(t, err) | ||
|
||
err = LoadConfig() | ||
assert.NoError(t, err) | ||
|
||
commitConfig := CommitConfig() | ||
|
||
assert.Equal(t, false, commitConfig.Strict, "expect strict to be false as opposed to the default of true") | ||
assert.Equal(t, 100, commitConfig.Limit, "expect limit to be 100 as opposed to the default of 0") | ||
assert.Equal(t, true, commitConfig.AllCommits, "expect strict to be false as opposed to the default of false") | ||
|
||
os.Clearenv() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
// CommitsarConfigPath is used an env variable to override the default location of the config file. | ||
CommitsarConfigPath = "COMMITSAR_CONFIG_PATH" | ||
) | ||
|
||
// LoadConfig iterrates through possible config paths. No config will be loaded if no files are present. | ||
func LoadConfig() error { | ||
viper.AutomaticEnv() | ||
viper.SetConfigName(".commitsar") | ||
viper.SetConfigType("yaml") | ||
|
||
if viper.IsSet(CommitsarConfigPath) { | ||
viper.AddConfigPath(viper.GetString(CommitsarConfigPath)) | ||
} | ||
|
||
viper.AddConfigPath(".") | ||
|
||
if err := viper.ReadInConfig(); err != nil { | ||
if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
// Config file not found; ignore error if desired | ||
log.Println("config file not found, using defaults") | ||
} else { | ||
// Config file was found but another error was produced | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadConfigCustomPath(t *testing.T) { | ||
err := os.Setenv(CommitsarConfigPath, "./testdata") | ||
assert.NoError(t, err) | ||
|
||
err = LoadConfig() | ||
assert.NoError(t, err) | ||
assert.Equal(t, true, viper.GetBool("verbose"), "expected verbose to be true, but got false") | ||
|
||
os.Clearenv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
verbose: true | ||
commits: | ||
strict: false | ||
limit: 100 | ||
all: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
id: config-file | ||
title: Configuration File | ||
--- | ||
|
||
**The configuration file is still under development and is subject to changes** | ||
|
||
**Name:** `.commitsar.yml` | ||
|
||
In order to make configuration easier than through flags we provide configuration file support. Most up to date examples can be found in <https://github.com/aevea/commitsar/tree/master/config/testdata>. | ||
|
||
By default the current working directory is used to scan for the file. However this can be overriden by specifying `COMMITSAR_CONFIG_PATH` environment variable. Accepts relative or absolute paths. | ||
|
||
Example: `COMMITSAR_CONFIG_PATH=./testdata` will scan for `.commitsar.yaml` in the `testdata` folder. | ||
|
||
## Global configuration | ||
|
||
These are settings that get used across all runs of commitsar. | ||
|
||
```yaml | ||
version: 1 | ||
verbose: false | ||
``` | ||
| Name | Default Value | Description | | ||
| ------- | ------------- | ----------------------------------------------------------------------------------- | | ||
| version | 1 | Currently not in use. Might be used in the future in case of incompatible upgrades. | | ||
| verbose | false | Turns on debug logging of commitsar. Useful if you want to submit an issue. | | ||
## Commit style settings | ||
```yaml | ||
commits: | ||
disabled: false | ||
strict: true | ||
limit: 0 | ||
all: false | ||
``` | ||
| Name | Default Value | Description | | ||
| -------- | ------------- | --------------------------------------------------------------------------------------------- | | ||
| disabled | false | Disables checking commmits. Useful if you want to use commitsar only for PR titles. | | ||
| strict | true | Enforces strict category enforcement. | | ||
| limit | none | Makes commitsar check only the last x commits. Useful if you want to run commitsar on master. | | ||
| all | false | Makes commitsar check all the commits in history. **Overrides the `limit` flag** | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ module.exports = { | |
], | ||
Configuration: [ | ||
"configuration/flags", | ||
"configuration/config-file", | ||
], | ||
}, | ||
}; |
b6b89fa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: