From b26dec85763bc214e75029c6c4056973fb3cf87c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 15 Nov 2018 14:48:02 +0100 Subject: [PATCH] FIX: find config format from content : seek start of file before each --- internal/cnf/loader.go | 27 +++++++++++++++------------ internal/cnf/parser/nginx/decoder.go | 1 - 2 files changed, 15 insertions(+), 13 deletions(-) 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 - }