update cnf/loader to infer parser (config format) from content

This commit is contained in:
xdrm-brackets 2018-11-12 20:01:07 +01:00
parent aa3c8e472d
commit 7ceaf81ee5
1 changed files with 26 additions and 5 deletions

View File

@ -42,11 +42,11 @@ func Load(path string) (ConfigurationFormat, error) {
// parse
_, err = confFormat.ReadFrom(file)
if err != nil {
return nil, fmt.Errorf("cannot parse file as '%s' | %s", extension, err)
if err == nil {
return confFormat, nil
}
return confFormat, nil
// return nil, fmt.Errorf("cannot parse file as '%s' | %s", extension, err)
}
@ -72,11 +72,11 @@ func loadFromExtension(ext string) ConfigurationFormat {
switch ext {
case ".json":
return new(json)
case ".ini":
case ".ini", ".conf":
return new(ini)
case ".yaml":
return new(yaml)
case ".conf":
case ".nginx":
return new(nginx)
default:
return nil
@ -85,5 +85,26 @@ func loadFromExtension(ext string) ConfigurationFormat {
}
func loadFromContent(file io.Reader) ConfigurationFormat {
// extensions ordered by unicity of the language's syntax
extensions := []string{".json", ".yaml", ".nginx", ".ini"}
// try to load each available extension
for _, ext := range extensions {
// load parser
c := loadFromExtension(ext)
if c == nil {
continue
}
// parse
_, err := c.ReadFrom(file)
if err == nil {
return c
}
}
return nil
}