2018-11-08 12:40:36 +00:00
|
|
|
package cnf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
// ErrUnknownExtension is raised when encountering an unknown extension
|
2018-11-08 12:40:36 +00:00
|
|
|
var ErrUnknownExtension = errors.New("unknown extension format")
|
2018-11-11 18:01:00 +00:00
|
|
|
|
|
|
|
// ErrUnknownFormat is raised when the format cannot be guessed from the content of the file
|
2018-11-10 18:04:16 +00:00
|
|
|
var ErrUnknownFormat = errors.New("cannot infer format from content")
|
2018-11-08 12:40:36 +00:00
|
|
|
|
2018-11-16 05:57:57 +00:00
|
|
|
// ErrFileNotExist is raised when required file does not exist
|
2018-11-15 16:26:11 +00:00
|
|
|
var ErrFileNotExist = errors.New("cannot find file")
|
|
|
|
|
2018-11-08 12:40:36 +00:00
|
|
|
// Load the current file and create the configuration format accordingly
|
|
|
|
func Load(path string) (ConfigurationFormat, error) {
|
|
|
|
|
|
|
|
// 1. check file
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
2018-11-15 16:26:11 +00:00
|
|
|
return nil, ErrFileNotExist
|
2018-11-08 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Try to load from extension
|
|
|
|
extension := filepath.Ext(path)
|
|
|
|
if len(extension) > 0 {
|
|
|
|
|
2018-11-15 16:26:11 +00:00
|
|
|
if confFormat := loadFromExtension(extension); confFormat != nil {
|
2018-11-08 12:40:36 +00:00
|
|
|
|
2018-11-15 16:26:11 +00:00
|
|
|
// open file
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-08 12:40:36 +00:00
|
|
|
|
2018-11-15 16:26:11 +00:00
|
|
|
// parse
|
|
|
|
_, err = confFormat.ReadFrom(file)
|
|
|
|
file.Close()
|
|
|
|
if err == nil {
|
|
|
|
return confFormat, nil
|
|
|
|
}
|
2018-11-08 12:40:36 +00:00
|
|
|
|
2018-11-15 16:26:11 +00:00
|
|
|
}
|
2018-11-08 12:40:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:48:02 +00:00
|
|
|
// 4. Try to guess from the content
|
|
|
|
confFormat, err := loadFromContent(path)
|
2018-11-08 12:40:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if confFormat == nil {
|
2018-11-10 18:04:16 +00:00
|
|
|
return nil, ErrUnknownFormat
|
2018-11-08 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return confFormat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromExtension(ext string) ConfigurationFormat {
|
|
|
|
|
|
|
|
// select configuration or fail if not known
|
|
|
|
switch ext {
|
2018-11-10 18:04:16 +00:00
|
|
|
case ".json":
|
2018-11-11 14:34:00 +00:00
|
|
|
return new(json)
|
2018-11-12 19:01:07 +00:00
|
|
|
case ".ini", ".conf":
|
2018-11-11 14:34:00 +00:00
|
|
|
return new(ini)
|
2018-11-11 17:07:29 +00:00
|
|
|
case ".yaml":
|
|
|
|
return new(yaml)
|
2018-11-12 19:01:07 +00:00
|
|
|
case ".nginx":
|
2018-11-12 16:43:01 +00:00
|
|
|
return new(nginx)
|
2018-11-18 18:08:18 +00:00
|
|
|
case ".sh":
|
|
|
|
return new(bash)
|
2018-11-08 12:40:36 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:48:02 +00:00
|
|
|
func loadFromContent(path string) (ConfigurationFormat, error) {
|
|
|
|
|
|
|
|
// 3. open file
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2018-11-12 19:01:07 +00:00
|
|
|
|
2018-11-15 13:48:02 +00:00
|
|
|
// extensions ordered by strictness of the language's syntax
|
2018-11-18 18:09:28 +00:00
|
|
|
extensions := []string{".json", ".nginx", ".ini", ".yaml", ".sh"}
|
2018-11-12 19:01:07 +00:00
|
|
|
|
|
|
|
// try to load each available extension
|
|
|
|
for _, ext := range extensions {
|
2018-11-15 13:48:02 +00:00
|
|
|
file.Seek(0, 0)
|
2018-11-12 19:01:07 +00:00
|
|
|
|
|
|
|
// load parser
|
|
|
|
c := loadFromExtension(ext)
|
|
|
|
if c == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse
|
|
|
|
_, err := c.ReadFrom(file)
|
|
|
|
if err == nil {
|
2018-11-15 13:48:02 +00:00
|
|
|
return c, nil
|
2018-11-12 19:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:48:02 +00:00
|
|
|
return nil, ErrUnknownFormat
|
2018-11-08 12:40:36 +00:00
|
|
|
}
|