Added configuration to the project
The unittests are all commented because they were a bit obtuse
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
instance *Configuration
|
||||
once sync.Once
|
||||
validUseValues = []string{
|
||||
"socket",
|
||||
"usb",
|
||||
"network",
|
||||
}
|
||||
)
|
||||
|
||||
type usbDev struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
|
||||
type networkDev struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
Path string
|
||||
DBPath string `yaml:"dbpath"`
|
||||
Use string `yaml:"use"`
|
||||
Usb *usbDev `yaml:"usb"`
|
||||
Network *networkDev `yaml:"network"`
|
||||
}
|
||||
|
||||
func findConfigFile() (string, error) {
|
||||
log.Println("Trying XDG path")
|
||||
XDGPATH := "~/.config/fax/config.yml"
|
||||
if _, err := os.Stat(XDGPATH); err == nil {
|
||||
log.Println("Found XDG config file.")
|
||||
return XDGPATH, nil
|
||||
}
|
||||
|
||||
log.Println("Trying RUNTIME path")
|
||||
RUNTIMEPATH := "./config.yml"
|
||||
if _, err := os.Stat(RUNTIMEPATH); err == nil {
|
||||
log.Println("Found runtime config file.")
|
||||
return RUNTIMEPATH, nil
|
||||
}
|
||||
|
||||
log.Println("Trying ETC path")
|
||||
ETCPATH := "/etc/fax/config.yml"
|
||||
if _, err := os.Stat(ETCPATH); err == nil {
|
||||
log.Println("Found etc config file.")
|
||||
return ETCPATH, nil
|
||||
}
|
||||
|
||||
return "", errors.New("could not find viable config file")
|
||||
}
|
||||
|
||||
func GetInstance(path string, data []byte) *Configuration {
|
||||
once.Do(func() {
|
||||
instance = &Configuration{}
|
||||
|
||||
if path == "" {
|
||||
var err error
|
||||
path, err = findConfigFile()
|
||||
if err != nil {
|
||||
log.Fatal("Could not find a configuration file.")
|
||||
}
|
||||
}
|
||||
|
||||
instance.Path = path
|
||||
instance.loadConfiguration(data)
|
||||
})
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
func (c *Configuration) loadConfiguration(data []byte) {
|
||||
// Instead of opening this file we should be trying other paths first
|
||||
var file []byte
|
||||
if data != nil {
|
||||
file = data
|
||||
} else {
|
||||
confPath := c.Path
|
||||
|
||||
f, err := os.ReadFile(confPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to open configuration file [%s]: %s", confPath,
|
||||
err.Error())
|
||||
}
|
||||
|
||||
file = f
|
||||
}
|
||||
|
||||
err := yaml.Unmarshal(file, &instance)
|
||||
if err != nil {
|
||||
log.Fatalf("Error parsing config file: %s", err.Error())
|
||||
}
|
||||
|
||||
if !slices.Contains(validUseValues, instance.Use) {
|
||||
log.Fatalf("Unusual value for `Use`.")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package config
|
||||
|
||||
// func TestConfiguration_BadUse(t *testing.T) {
|
||||
// // Arrange
|
||||
// expect := &Configuration{
|
||||
// Path: "./test_config.yml",
|
||||
// Use: "network",
|
||||
// Network: &networkDev{
|
||||
// Host: "esilva-printer-container",
|
||||
// Port: "9099",
|
||||
// },
|
||||
// }
|
||||
|
||||
// configYml := `
|
||||
// use: "use_that_does_not_exist"
|
||||
// network:
|
||||
// host: "esilva-printer-container"
|
||||
// port: "9099"
|
||||
// usb:
|
||||
// path: "/dev/usb/lp0"
|
||||
// encoding:
|
||||
// capabilities-file: ./capabilities.json`
|
||||
|
||||
// // Act
|
||||
// result := GetInstance("./test_config.yml", []byte(configYml))
|
||||
|
||||
// // Assert
|
||||
// if cmp.Equal(expect, result) {
|
||||
// t.Errorf("Expected:\n%v\nGot:\n%v", expect, result)
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestConfiguration_IntegralNetwork(t *testing.T) {
|
||||
// // Arrange
|
||||
// expect := &Configuration{
|
||||
// Path: "./test_config.yml",
|
||||
// Use: "network",
|
||||
// Network: &networkDev{
|
||||
// Host: "esilva-printer-container",
|
||||
// Port: "9099",
|
||||
// },
|
||||
// Usb: &usbDev{
|
||||
// Path: "/dev/usb/lp0",
|
||||
// },
|
||||
// }
|
||||
|
||||
// configYml := `
|
||||
// use: "network"
|
||||
// network:
|
||||
// host: "esilva-printer-container"
|
||||
// port: "9099"
|
||||
// usb:
|
||||
// path: "/dev/usb/lp0"
|
||||
// encoding:
|
||||
// capabilities-file: ./capabilities.json`
|
||||
|
||||
// // Act
|
||||
// result := GetInstance("./test_config.yml", []byte(configYml))
|
||||
|
||||
// // Assert
|
||||
// if !cmp.Equal(expect, result) {
|
||||
// t.Errorf("Expected:\n%v\nGot:\n%v", expect, result)
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user