Skip to content

Commit

Permalink
Single configuration file extensions
Browse files Browse the repository at this point in the history
The application configuration file loader supported multiple file types
with multiple file extensions, e.g. `yml` and `yaml` for YAML encoded
data. This could lead to priority problems when merging multiple loaded
configuration states when there are multiple YAML files with different
supported file extension in the same directory. There were no rules how
to decide which extension takes precedence over another extension
causing unexpected merged configurations.

To prevent such problems each file type now only supports a single
official file extension. The currently supported encodings are JSON (1)
and YAML (2) where the following file extension are only supported:

1. JSON
  - before: `*.json`
  - after: `*.json`
2. YAML
  - before: `*.yml, *.yaml`
  - after: `*.yml`

Note that this won't affect the precedences of different file types!
YAML files still take precedence over JSON files since YAML is a
superset of JSON and JSON is also valid YAML.

References:
  (1) https://www.json.org
  (2) https://yaml.org

Epic GH-33
Resolves GH-67
  • Loading branch information
arcticicestudio committed Jul 13, 2019
1 parent 8531f47 commit 008edbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
20 changes: 4 additions & 16 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,16 @@ func init() {
func genConfigPaths() []*file.File {
var files []*file.File

// Include user-level dot-file configurations from the user's home directory.
// Include the user-level dotfile configuration from user's home directory.
home, err := homedir.Dir()
if err == nil {
for _, ext := range encoder.ExtensionsJson {
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, ext))))
}
// Since YAML is a superset of JSON, YAML files take precedence over pure JSON based configurations.
for _, ext := range encoder.ExtensionsYaml {
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, ext))))
}
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, encoder.ExtensionsYaml))))
}

// Files placed in the current working directory take precedence over user-level configurations.
// A file placed in the current working directory takes precedence over the user-level configuration.
pwd, err := os.Getwd()
if err == nil {
for _, ext := range encoder.ExtensionsJson {
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, ext))))
}
// Since YAML is a superset of JSON, YAML files take precedence over pure JSON based configurations.
for _, ext := range encoder.ExtensionsYaml {
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, ext))))
}
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, encoder.ExtensionsYaml))))
}

return files
Expand Down
13 changes: 6 additions & 7 deletions pkg/config/encoder/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
var (
// ExtensionMapping maps supported file extensions to their compatible encoders.
ExtensionMapping = map[string]Encoder{
"json": json.NewJsonEncoder(),
"yaml": yaml.NewYamlEncoder(),
"yml": yaml.NewYamlEncoder(),
ExtensionsJson: json.NewJsonEncoder(),
ExtensionsYaml: yaml.NewYamlEncoder(),
}
// ExtensionsJson stores all supported extensions for files containing JSON data.
ExtensionsJson = []string{"json"}
// ExtensionsYaml stores all supported extensions for files containing YAML data.
ExtensionsYaml = []string{"yaml", "yml"}
// ExtensionsJson is the supported extension for files containing JSON data.
ExtensionsJson = "json"
// ExtensionsYaml is the supported extension for files containing YAML data.
ExtensionsYaml = "yml"
)

0 comments on commit 008edbc

Please sign in to comment.