Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ini files #25

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
jd4235 marked this conversation as resolved.
Show resolved Hide resolved
"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
Copy link
Collaborator

Choose a reason for hiding this comment

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

The error output needs to include the line and column number. For example:

Error at line 1 column 3:

Since ini does not seem to output that by default you'll need to calculate it. Look at how that's being done in the json validator

Copy link
Collaborator

@kehoecj kehoecj May 26, 2023

Choose a reason for hiding this comment

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

Created #26 to help enforce this in the future

Copy link
Contributor Author

@g41797 g41797 May 27, 2023

Choose a reason for hiding this comment

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

I didn't find possibility to calculate line&column

Checked another go ini solutions e.g.

Two possibilities:

  1. accept limitation and use current implementation
  2. postpone till finding solution

meanwhile pr set to draft mode

Copy link
Collaborator

@kehoecj kehoecj May 31, 2023

Choose a reason for hiding this comment

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

I looked into it as well and you're right - there is no way to get the line/column number from the error output.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrote an enhancement request to go-init to add this output to the error message.

}
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