Begin to overhaul config to couple initialization logic closer to the structs themselves.
This commit is contained in:
parent
d9babf5dc0
commit
1b374bccc6
15 changed files with 122 additions and 33 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
//
|
||||
// nb: there are methods defined on ConfigParser in other files in this package!
|
||||
|
||||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
|
@ -75,3 +75,59 @@ func (parser *ConfigParser) GetModes() []string {
|
|||
}
|
||||
return parser.config.Modes
|
||||
}
|
||||
|
||||
func ParseConfig(directory string) (*Config, error) {
|
||||
config := new(Config)
|
||||
|
||||
configFiles, err := getConfigFilePaths(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Open each yaml file and add its contents to the global config
|
||||
for _, filePath := range configFiles {
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
logger.LogError(err, "Error while opening config file")
|
||||
continue
|
||||
}
|
||||
|
||||
newConfig := Config{}
|
||||
err = yaml.Unmarshal(data, &newConfig)
|
||||
logger.LogIfError(err, "Error parsing YAML")
|
||||
config.Rules = append(config.Rules, newConfig.Rules...)
|
||||
config.Devices = append(config.Devices, newConfig.Devices...)
|
||||
config.Modes = append(config.Modes, newConfig.Modes...)
|
||||
}
|
||||
|
||||
if len(config.Devices) == 0 {
|
||||
return nil, errors.New("Found no devices in configuration. Please add configuration at " + directory)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func getConfigFilePaths(directory string) ([]string, error) {
|
||||
paths := make([]string, 0)
|
||||
|
||||
dirEntries, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
err = os.Mkdir(directory, 0755)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to create config directory at " + directory)
|
||||
} else {
|
||||
return nil, errors.New("no config files found at " + directory)
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range dirEntries {
|
||||
name := strings.ToLower(file.Name())
|
||||
if file.IsDir() || !(strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")) {
|
||||
continue
|
||||
}
|
||||
|
||||
paths = append(paths, filepath.Join(directory, file.Name()))
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import "github.com/holoplot/go-evdev"
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import "slices"
|
||||
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
// These types comprise the YAML schema for configuring Joyful.
|
||||
// The config files will be combined and then unmarshalled into this
|
||||
|
||||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package configparser
|
||||
|
||||
import (
|
||||
"github.com/holoplot/go-evdev"
|
||||
Loading…
Add table
Add a link
Reference in a new issue