diff --git a/cmd/validator/validator_test.go b/cmd/validator/validator_test.go index e2a2330e..8eda87b6 100644 --- a/cmd/validator/validator_test.go +++ b/cmd/validator/validator_test.go @@ -23,6 +23,7 @@ func Test_flags(t *testing.T) { {"flags set, wrong reporter", []string{"--exclude-dirs=subdir", "--reporter=wrong", "."}, 1}, {"flags set, json reporter", []string{"--exclude-dirs=subdir", "--reporter=json", "."}, 0}, {"flags set, junit reported", []string{"--exclude-dirs=subdir", "--reporter=junit", "."}, 0}, + {"junit reported, wrong groupby", []string{"-groupby=directory", "--reporter=junit", "."}, 1}, {"bad path", []string{"/path/does/not/exit"}, 1}, {"exclude file types set", []string{"--exclude-file-types=json", "."}, 0}, {"multiple paths", []string{"../../test/fixtures/subdir/good.json", "../../test/fixtures/good.json"}, 0}, @@ -32,6 +33,7 @@ func Test_flags(t *testing.T) { {"wrong output set", []string{"--output", "/path/not/exist", "--reporter", "json", "."}, 1}, {"incorrect group", []string{"-groupby=badgroup", "."}, 1}, {"correct group", []string{"-groupby=directory", "."}, 0}, + {"duplicate groupby", []string{"-groupby=directory -groupby=directory", "."}, 1}, } for _, tc := range cases { // this call is required because otherwise flags panics, diff --git a/go.mod b/go.mod index 241f1450..af351e64 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/magiconair/properties v1.8.7 github.com/pelletier/go-toml/v2 v2.0.6 github.com/stretchr/testify v1.8.1 + github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436 gopkg.in/ini.v1 v1.67.0 gopkg.in/yaml.v3 v3.0.1 howett.net/plist v1.0.0 diff --git a/go.sum b/go.sum index 475c246d..35b02b49 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436 h1:i9TLbw23bUawnhimf5SghqkLrDRdpa65vw0hUqYhCB0= +github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436/go.mod h1:4fTjBxMoWGOIVnGFSTS9GAZ0yMyiGzTdATQS0krQv18= github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0= github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -61,5 +63,7 @@ gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa 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= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM= howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g= diff --git a/pkg/filetype/file_type.go b/pkg/filetype/file_type.go index 128dc7fd..2fbeb2cb 100644 --- a/pkg/filetype/file_type.go +++ b/pkg/filetype/file_type.go @@ -11,6 +11,7 @@ import ( type FileType struct { Name string Extensions []string + FileMasks []string Validator validator.Validator } @@ -19,6 +20,7 @@ type FileType struct { var JsonFileType = FileType{ "json", []string{"json"}, + []string{}, validator.JsonValidator{}, } @@ -27,6 +29,7 @@ var JsonFileType = FileType{ var YamlFileType = FileType{ "yaml", []string{"yml", "yaml"}, + []string{}, validator.YamlValidator{}, } @@ -35,6 +38,7 @@ var YamlFileType = FileType{ var XmlFileType = FileType{ "xml", []string{"xml"}, + []string{}, validator.XmlValidator{}, } @@ -43,6 +47,7 @@ var XmlFileType = FileType{ var TomlFileType = FileType{ "toml", []string{"toml"}, + []string{}, validator.TomlValidator{}, } @@ -51,6 +56,7 @@ var TomlFileType = FileType{ var IniFileType = FileType{ "ini", []string{"ini"}, + []string{}, validator.IniValidator{}, } @@ -59,6 +65,7 @@ var IniFileType = FileType{ var PropFileType = FileType{ "properties", []string{"properties"}, + []string{}, validator.PropValidator{}, } @@ -67,6 +74,7 @@ var PropFileType = FileType{ var HclFileType = FileType{ "hcl", []string{"hcl"}, + []string{}, validator.HclValidator{}, } @@ -75,6 +83,7 @@ var HclFileType = FileType{ var PlistFileType = FileType{ "plist", []string{"plist"}, + []string{}, validator.PlistValidator{}, } @@ -83,14 +92,23 @@ var PlistFileType = FileType{ var CsvFileType = FileType{ "csv", []string{"csv"}, + []string{}, validator.CsvValidator{}, } +var NginxFileType = FileType{ + "nginx", + []string{}, + []string{"nginx*.conf"}, + validator.NginxValidator{}, +} + // Instance of the FileType object to // represent a HOCON file var HoconFileType = FileType{ "hocon", []string{"hocon"}, + []string{}, validator.HoconValidator{}, } @@ -106,5 +124,6 @@ var FileTypes = []FileType{ HclFileType, PlistFileType, CsvFileType, + NginxFileType, HoconFileType, } diff --git a/pkg/finder/fsfinder.go b/pkg/finder/fsfinder.go index 9aad718d..05a0a2d4 100644 --- a/pkg/finder/fsfinder.go +++ b/pkg/finder/fsfinder.go @@ -150,6 +150,19 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) { } } } + } else { + for _, fileType := range fsf.FileTypes { + for _, fileMask := range fileType.FileMasks { + pattern := filepath.Join(filepath.Dir(path), filepath.Base(path), fileMask) + foundFiles, _ := filepath.Glob(pattern) + for _, matchedFilePath := range foundFiles { + ss := strings.Split(matchedFilePath, string(os.PathSeparator)) + matchedFileName := ss[len(ss)-1] + fileMetadata := FileMetadata{matchedFileName, matchedFilePath, fileType} + matchingFiles = append(matchingFiles, fileMetadata) + } + } + } } return nil diff --git a/pkg/validator/nginx.go b/pkg/validator/nginx.go new file mode 100644 index 00000000..31819f5e --- /dev/null +++ b/pkg/validator/nginx.go @@ -0,0 +1,17 @@ +package validator + +import ( + "github.com/tufanbarisyildirim/gonginx/parser" +) + +type NginxValidator struct{} + +func (nv NginxValidator) Validate(b []byte) (result bool, errMsg error) { + p := parser.NewStringParser(string(b)) + _, err := p.Parse() + if err != nil { + return false, err + } + + return true, nil +} diff --git a/pkg/validator/validator_test.go b/pkg/validator/validator_test.go index e83a5eb2..08c832ff 100644 --- a/pkg/validator/validator_test.go +++ b/pkg/validator/validator_test.go @@ -62,6 +62,8 @@ var testData = []struct { {"invalidCSV", []byte(`This string has a \" in it`), false, CsvValidator{}}, {"validPlist", validPlistBytes, true, PlistValidator{}}, {"invalidPlist", invalidPlistBytes, false, PlistValidator{}}, + {"validNginx", []byte(`http { server { listen 80; server_name localhost; }}`), true, NginxValidator{}}, + {"invalidNginx", []byte(`invalid_key {}`), false, NginxValidator{}}, {"validHocon", []byte(`test = [1, 2, 3]`), true, HoconValidator{}}, {"invalidHocon", []byte(`test = [1, 2,, 3]`), false, HoconValidator{}}, }