Skip to content

Commit

Permalink
read config from file with viper (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison authored Aug 2, 2024
1 parent 4822df6 commit c222328
Show file tree
Hide file tree
Showing 35 changed files with 785 additions and 680 deletions.
4 changes: 4 additions & 0 deletions checker/colorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
ColorInvalid
)

func GetSupportedColorValues() []string {
return []string{"auto", "always", "never"}
}

func NewColorMode(color string) (ColorMode, error) {
switch color {
case "always":
Expand Down
14 changes: 11 additions & 3 deletions checker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func (config *Config) WithOptionalCheck(id string) *Config {
// WithOptionalChecks overrides the log level of the given checks to ERR so they will appear in `oasdiff breaking`
func (config *Config) WithOptionalChecks(ids []string) *Config {
for _, id := range ids {
config.LogLevels[id] = ERR
config.setLogLevel(id, ERR)
}
return config
}

func (config *Config) WithSeverityLevels(severityLevels map[string]Level) *Config {
for id, level := range severityLevels {
config.LogLevels[id] = level
config.setLogLevel(id, level)
}

return config
Expand Down Expand Up @@ -74,9 +74,17 @@ func (config *Config) getLogLevel(checkId string) Level {
level, ok := config.LogLevels[checkId]

if !ok {
log.Fatal("check id not found: ", checkId)
log.Fatal("failed to get log level with invalid check id: ", checkId)
}

return level

}

func (config *Config) setLogLevel(checkId string, level Level) {
if _, ok := config.LogLevels[checkId]; !ok {
log.Fatal("failed to set log level with invalid check id: ", checkId)
}

config.LogLevels[checkId] = level
}
10 changes: 6 additions & 4 deletions checker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ func TestNewConfigWithDeprecation(t *testing.T) {
}

func TestNewConfigWithOptionalCheck(t *testing.T) {
config := allChecksConfig().WithOptionalCheck("id")
require.Equal(t, checker.ERR, config.LogLevels["id"])
const id = checker.RequestPropertyDefaultValueChangedId
config := allChecksConfig().WithOptionalCheck(id)
require.Equal(t, checker.ERR, config.LogLevels[id])
}

func TestNewConfigWithSeverityLevels(t *testing.T) {
config := allChecksConfig().WithSeverityLevels(map[string]checker.Level{"id": checker.ERR})
require.Equal(t, checker.ERR, config.LogLevels["id"])
const id = checker.RequestPropertyDefaultValueChangedId
config := allChecksConfig().WithSeverityLevels(map[string]checker.Level{id: checker.ERR})
require.Equal(t, checker.ERR, config.LogLevels[id])
}
9 changes: 5 additions & 4 deletions checker/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
type Level int

const (
ERR Level = 3
WARN Level = 2
INFO Level = 1
ERR Level = 3
WARN Level = 2
INFO Level = 1
INVALID Level = 0
)

func NewLevel(level string) (Level, error) {
Expand All @@ -28,7 +29,7 @@ func NewLevel(level string) (Level, error) {
case "INFO", "info":
return INFO, nil
}
return INFO, fmt.Errorf("invalid level %s", level)
return INVALID, fmt.Errorf("invalid level %s", level)
}

func (level Level) StringCond(colorMode ColorMode) string {
Expand Down
21 changes: 9 additions & 12 deletions diff/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ const (
ExcludeExtensionsOption = "extensions"
)

var ExcludeDiffOptions = []string{
ExcludeExamplesOption,
ExcludeDescriptionOption,
ExcludeEndpointsOption,
ExcludeTitleOption,
ExcludeSummaryOption,
ExcludeExtensionsOption,
func GetExcludeDiffOptions() []string {
return []string{
ExcludeExamplesOption,
ExcludeDescriptionOption,
ExcludeEndpointsOption,
ExcludeTitleOption,
ExcludeSummaryOption,
ExcludeExtensionsOption,
}
}

// NewConfig returns a default configuration
Expand All @@ -46,11 +48,6 @@ func (config *Config) WithExcludeElements(excludeElements []string) *Config {
return config
}

func (config *Config) WithExcludeExtensions() *Config {
config.ExcludeElements.Add(ExcludeExtensionsOption)
return config
}

func (config *Config) IsExcludeExamples() bool {
return config.ExcludeElements.Contains(ExcludeExamplesOption)
}
Expand Down
2 changes: 1 addition & 1 deletion diff/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestConfig_Default(t *testing.T) {
}

func TestConfig_ExcludeElements(t *testing.T) {
c := diff.NewConfig().WithExcludeElements(diff.ExcludeDiffOptions)
c := diff.NewConfig().WithExcludeElements(diff.GetExcludeDiffOptions())
require.True(t, c.IsExcludeExamples())
require.True(t, c.IsExcludeDescription())
require.True(t, c.IsExcludeEndpoints())
Expand Down
4 changes: 2 additions & 2 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestModifiedExtension(t *testing.T) {
}

func TestExcludedExtension(t *testing.T) {
require.Nil(t, d(t, diff.NewConfig().WithExcludeExtensions(), 1, 3).ExtensionsDiff)
require.Nil(t, d(t, diff.NewConfig().WithExcludeElements([]string{diff.ExcludeExtensionsOption}), 1, 3).ExtensionsDiff)
}

func TestDiff_AddedGlobalTag(t *testing.T) {
Expand Down Expand Up @@ -851,7 +851,7 @@ func TestDiff_ExtensionsExcluded(t *testing.T) {
s2, err := load.NewSpecInfo(loader, load.NewSource("../data/extensions/revision.yaml"))
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig().WithExcludeExtensions(), s1, s2)
d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig().WithExcludeElements([]string{diff.ExcludeExtensionsOption}), s1, s2)
require.NoError(t, err)
require.Empty(t, d)
}
Expand Down
1 change: 1 addition & 0 deletions docs/BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ If you encounter a change that isn't reported, you may:
- [Case-insensitive header comparison](HEADER-DIFF.md)
- [Comparing multiple specs](COMPOSED.md)
- [Adding OpenAPI Extensions to the changelog output](ATTRIBUTES.md)
- [Customize with configuration files](CONFIG-FILES.md)
- [Running from docker](DOCKER.md)
- [Embedding in your go program](GO.md)

Expand Down
22 changes: 22 additions & 0 deletions docs/CONFIG-FILES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Configuration Files

Oasdiff can be customized through command-line flags, for example:
```
oasdiff changelog data/openapi-test1.yaml data/openapi-test3.yaml --format yaml
```

To see available flags, run `oasdiff help <cmd>`, for example:
```
oasdiff help changelog
```

The flags can also be provided through a configuration file.
The config file should be named oasdiff.{json,yaml,yml,toml,hcl} and placed in the directory where the command is run.
For example, see [oasdiff.yaml](oasdiff.yaml).

Note that some of the flags define paths to additional configuration files:
- --err-ignore string: configuration file for ignoring errors
- --severity-levels string: configuration file for custom severity levels
- --warn-ignore string: configuration file for ignoring warnings

Note that command-line flags take precedence over configuration file settings.
1 change: 1 addition & 0 deletions docs/DIFF.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ oasdiff diff data/openapi-test1.yaml data/openapi-test3.yaml --exclude-elements
- [Path parameter renaming](PATH-PARAM-RENAME.md)
- [Case-insensitive header comparison](HEADER-DIFF.md)
- [Comparing multiple specs](COMPOSED.md)
- [Customize with configuration files](CONFIG-FILES.md)
- [Running from docker](DOCKER.md)
- [Embedding in your go program](GO.md)
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ docker run --rm -t tufin/oasdiff changelog https://raw.githubusercontent.com/Tuf
- [Filtering endpoints](FILTERING-ENDPOINTS.md)
- [Extending breaking changes with custom checks](CUSTOMIZING-CHECKS.md)
- Localization: display breaking changes and changelog messages in English or Russian ([please contribute support for your language](https://github.com/Tufin/oasdiff/issues/383))
- [Customize with configuration files](CONFIG-FILES.md)
- [Run from Docker](DOCKER.md)
- [Integrate in GitHub](https://github.com/oasdiff/github-demo/tree/main)
- [GitHub Action](https://github.com/oasdiff/oasdiff-action)
Expand Down
13 changes: 13 additions & 0 deletions docs/oasdiff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
format: json
color: never
attributes:
- x-beta
- x-extension-test
composed: false
flatten-allof: true
err-ignore: ignore-err-example.txt
lang: en
fail-on: ERR
level: INFO
exclude-elements:
- endpoints
14 changes: 14 additions & 0 deletions formatters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const (
FormatSarif Format = "sarif"
)

func GetSupportedFormats() []string {
return []string{
string(FormatYAML),
string(FormatJSON),
string(FormatText),
string(FormatMarkup),
string(FormatSingleLine),
string(FormatHTML),
string(FormatGithubActions),
string(FormatJUnit),
string(FormatSarif),
}
}

// FormatterOpts can be used to pass properties to the formatter (e.g. colors)
type FormatterOpts struct {
Language string
Expand Down
12 changes: 12 additions & 0 deletions formatters/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package formatters_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/formatters"
)

func TestTypes(t *testing.T) {
require.Equal(t, formatters.GetSupportedFormats(), []string{"yaml", "json", "text", "markup", "singleline", "html", "githubactions", "junit", "sarif"})
}
22 changes: 19 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/TwiN/go-color v1.4.1
github.com/getkin/kin-openapi v0.126.0
github.com/oasdiff/telemetry v0.1.2
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/yargevad/filepathx v1.0.0
github.com/yuin/goldmark v1.7.4
Expand All @@ -16,25 +18,39 @@ require (

require (
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/spf13/cobra v1.8.1
github.com/wI2L/jsondiff v0.6.0
)
Loading

0 comments on commit c222328

Please sign in to comment.