2019-06-17 09:48:05 +00:00
|
|
|
// Package file implements decoding between configuration in a file and a typed Configuration.
|
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2020-06-17 14:48:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/parser"
|
2020-06-17 14:48:04 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-06-17 09:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Decode decodes the given configuration file into the given element.
|
|
|
|
// The operation goes through three stages roughly summarized as:
|
|
|
|
// file contents -> tree of untyped nodes
|
|
|
|
// untyped nodes -> nodes augmented with metadata such as kind (inferred from element)
|
2020-05-11 10:06:07 +00:00
|
|
|
// "typed" nodes -> typed element.
|
2019-06-17 09:48:05 +00:00
|
|
|
func Decode(filePath string, element interface{}) error {
|
|
|
|
if element == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filters := getRootFieldNames(element)
|
|
|
|
|
|
|
|
root, err := decodeFileToNode(filePath, filters...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:48:04 +00:00
|
|
|
metaOpts := parser.MetadataOpts{TagName: parser.TagFile, AllowSliceAsStruct: false}
|
2019-11-28 20:56:04 +00:00
|
|
|
err = parser.AddMetadata(element, root, metaOpts)
|
2019-06-17 09:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:48:04 +00:00
|
|
|
return parser.Fill(element, root, parser.FillerOpts{AllowSliceAsStruct: false})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeContent decodes the given configuration file content into the given element.
|
|
|
|
// The operation goes through three stages roughly summarized as:
|
|
|
|
// file contents -> tree of untyped nodes
|
|
|
|
// untyped nodes -> nodes augmented with metadata such as kind (inferred from element)
|
|
|
|
// "typed" nodes -> typed element.
|
2020-07-10 08:46:11 +00:00
|
|
|
func DecodeContent(content, extension string, element interface{}) error {
|
2020-06-17 14:48:04 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
|
|
switch extension {
|
|
|
|
case ".toml":
|
|
|
|
_, err := toml.Decode(content, &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
var err error
|
|
|
|
err = yaml.Unmarshal([]byte(content), &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported file extension: %s", extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
filters := getRootFieldNames(element)
|
|
|
|
|
|
|
|
node, err := decodeRawToNode(data, parser.DefaultRootName, filters...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(node.Children) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
metaOpts := parser.MetadataOpts{TagName: parser.TagFile, AllowSliceAsStruct: false}
|
|
|
|
err = parser.AddMetadata(element, node, metaOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parser.Fill(element, node, parser.FillerOpts{AllowSliceAsStruct: false})
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|