diff --git a/internal/cnf/loader.go b/internal/cnf/loader.go index 7a669ac..e13de07 100644 --- a/internal/cnf/loader.go +++ b/internal/cnf/loader.go @@ -3,7 +3,6 @@ package cnf import ( "errors" "fmt" - "io" "os" "path/filepath" ) @@ -50,15 +49,11 @@ func Load(path string) (ConfigurationFormat, error) { } - // 3. open file - file, err := os.Open(path) + // 4. Try to guess from the content + confFormat, err := loadFromContent(path) if err != nil { return nil, err } - defer file.Close() - - // 4. Try to guess from the content - confFormat = loadFromContent(file) if confFormat == nil { return nil, ErrUnknownFormat } @@ -84,13 +79,21 @@ func loadFromExtension(ext string) ConfigurationFormat { } -func loadFromContent(file io.Reader) ConfigurationFormat { +func loadFromContent(path string) (ConfigurationFormat, error) { - // extensions ordered by unicity of the language's syntax - extensions := []string{".json", ".yaml", ".nginx", ".ini"} + // 3. open file + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + // extensions ordered by strictness of the language's syntax + extensions := []string{".json", ".nginx", ".yaml", ".ini"} // try to load each available extension for _, ext := range extensions { + file.Seek(0, 0) // load parser c := loadFromExtension(ext) @@ -101,10 +104,10 @@ func loadFromContent(file io.Reader) ConfigurationFormat { // parse _, err := c.ReadFrom(file) if err == nil { - return c + return c, nil } } - return nil + return nil, ErrUnknownFormat } diff --git a/internal/cnf/parser/nginx/decoder.go b/internal/cnf/parser/nginx/decoder.go index 8cb9a3e..a6b34b7 100644 --- a/internal/cnf/parser/nginx/decoder.go +++ b/internal/cnf/parser/nginx/decoder.go @@ -153,5 +153,4 @@ func (d *Decoder) Decode(v interface{}) error { } return nil - }