Skip to content

Commit

Permalink
feat: add configuration file support
Browse files Browse the repository at this point in the history
- 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
fallion committed Jun 16, 2020
1 parent 1a79530 commit b6b89fa
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .commitsar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 1
verbose: true
commits:
strict: true
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ renovate.json
.goreleaser.yml
.kodiak.toml
CNAME
.github
.github
.commitsar.yaml
32 changes: 32 additions & 0 deletions config/commits.go
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,
}
}
33 changes: 33 additions & 0 deletions config/commits_test.go
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()

}
36 changes: 36 additions & 0 deletions config/config.go
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
}
20 changes: 20 additions & 0 deletions config/config_test.go
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()
}
5 changes: 5 additions & 0 deletions config/testdata/.commitsar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
verbose: true
commits:
strict: false
limit: 100
all: true
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ require (
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.6.1
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/aevea/git/v2 v2.2.0 h1:CUi+R1AQ/NJgywyJIgrFXvbrhpMessaOU51GpDqsgyI=
Expand Down Expand Up @@ -40,6 +41,7 @@ github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
Expand Down Expand Up @@ -80,6 +82,7 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down Expand Up @@ -107,6 +110,7 @@ github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRn
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE=
github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand All @@ -119,6 +123,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand All @@ -142,13 +147,17 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
68 changes: 44 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@ import (
"log"
"os"

"github.com/aevea/commitsar/config"
"github.com/aevea/commitsar/internal/version_runner"

"github.com/aevea/commitsar/internal/root_runner"
"github.com/aevea/integrations"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// Verbose is used to allow verbose/debug output for any given command
var Verbose bool

// Strict is used to enforce only standard categories
var Strict bool

// Dir is the location of repo to check
var Dir string

// AllCommits will iterate through all the commits on a branch
var AllCommits bool

// version is a global variable passed during build time
var version string

Expand All @@ -41,7 +31,8 @@ func runRoot(cmd *cobra.Command, args []string) error {
debugLogger.SetPrefix("[DEBUG] ")
debugLogger.SetOutput(os.Stdout)

if !Verbose {
if !viper.GetBool("verbose") {
log.Println("this works")
debugLogger.SetOutput(ioutil.Discard)
debugLogger.SetPrefix("")
}
Expand All @@ -50,18 +41,47 @@ func runRoot(cmd *cobra.Command, args []string) error {

runner := root_runner.New(logger, &debugLogger)

options := root_runner.RunnerOptions{
Path: ".",
UpstreamBranch: upstreamBranch,
Limit: 0,
AllCommits: AllCommits,
Strict: Strict,
commitConfig := config.CommitConfig()

commitConfig.UpstreamBranch = upstreamBranch

commitConfig.Path = "."

return runner.Run(commitConfig, args...)
}

func bindRootFlags(rootCmd *cobra.Command) error {
rootCmd.Flags().BoolP("verbose", "v", false, "verbose output")
err := viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose"))
if err != nil {
return err
}
rootCmd.Flags().BoolP("strict", "s", true, "strict mode")
err = viper.BindPFlag("commits.strict", rootCmd.Flags().Lookup("strict"))
if err != nil {
return err
}
rootCmd.Flags().BoolP("all", "a", false, "iterate through all the commits on the branch")
err = viper.BindPFlag("commits.all", rootCmd.Flags().Lookup("all"))
if err != nil {
return err
}

return runner.Run(options, args...)
// Not used. TODO: Documentation
rootCmd.Flags().StringP("path", "d", ".", "dir points to the path of the repository")
err = viper.BindPFlag("path", rootCmd.Flags().Lookup("path"))
if err != nil {
return err
}
return nil
}

func main() {
if err := config.LoadConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

var rootCmd = &cobra.Command{
Use: "commitsar <from?>...<to>",
Short: "Checks if commits comply",
Expand All @@ -72,10 +92,10 @@ func main() {
Args: cobra.MinimumNArgs(0),
}

rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&Strict, "strict", "s", true, "strict mode")
rootCmd.PersistentFlags().StringVarP(&Dir, "path", "d", ".", "dir points to the path of the repository")
rootCmd.PersistentFlags().BoolVarP(&AllCommits, "all", "a", false, "iterate through all the commits on the branch")
if err := bindRootFlags(rootCmd); err != nil {
fmt.Println(err)
os.Exit(1)
}

// Version returns undefined if not on a tag. This needs to reset it.
if version == "undefined" {
Expand Down
45 changes: 45 additions & 0 deletions www/docs/configuration/config-file.md
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** |
1 change: 1 addition & 0 deletions www/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
],
Configuration: [
"configuration/flags",
"configuration/config-file",
],
},
};

1 comment on commit b6b89fa

@vercel
Copy link

@vercel vercel bot commented on b6b89fa Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.