Skip to content

Commit

Permalink
Add support for ini files (#25)
Browse files Browse the repository at this point in the history
* Add support for ini files

* Fixes after review
	modified:   pkg/filetype/file_type.go
	renamed:    test/fixtures/subdir/bad.ini -> test/fixtures/subdir2/bad.ini
  • Loading branch information
g41797 authored May 31, 2023
1 parent b59479d commit 78a11b1
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
cmd/validator/validator

# Test binary, built with `go test -c`
*.test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ How many deployments have you done that needed to be rolled back due to a missin
* JSON
* YAML
* TOML
* INI

## Installing
There are several ways to install the config file validator tool
Expand Down
2 changes: 1 addition & 1 deletion cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Validator recusively scans a directory to search for configuration files and
validates them using the go package for each configuration type.
Currently json, yaml, toml, and xml configuration file types are supported.
Currently json, yaml, toml, xml and ini configuration file types are supported.
Usage:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand All @@ -21,6 +22,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ var TomlFileType = FileType{
validator.TomlValidator{},
}

// Instance of FileType object to
// represent a Ini file
var IniFileType = FileType{
"ini",
[]string{"ini"},
validator.IniValidator{},
}

// An array of files types that are supported
// by the validator
var FileTypes = []FileType{
JsonFileType,
YamlFileType,
XmlFileType,
TomlFileType,
IniFileType,
}
17 changes: 17 additions & 0 deletions pkg/validator/ini.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package validator

import (
"gopkg.in/ini.v1"
)

type IniValidator struct{}

// Validate implements the Validator interface by attempting to
// parse a byte array of ini
func (iv IniValidator) Validate(b []byte) (bool, error) {
_, err := ini.LoadSources(ini.LoadOptions{}, b)
if err != nil {
return false, err
}
return true, nil
}
2 changes: 2 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var testData = []struct {
{"invalidXml", []byte("<xml\n"), false, XmlValidator{}},
{"invalidToml", []byte("name = 123__456"), false, TomlValidator{}},
{"validToml", []byte("name = 123"), true, TomlValidator{}},
{"validIni", []byte(`{[Version]\nCatalog=hidden\n}`), true, IniValidator{}},
{"invalidIni", []byte(`\nCatalog hidden\n`), false, IniValidator{}},
}

func Test_ValidationInput(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/good.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Version]
First=name
second=value

[Empty]

[Last]
1=2
4 changes: 4 additions & 0 deletions test/fixtures/subdir2/bad.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


name value

0 comments on commit 78a11b1

Please sign in to comment.