From 548ecb756a76957a7402c1482fd4d894a3fab49d Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 29 May 2024 14:44:12 +0100 Subject: [PATCH] Added configuration to the project The unittests are all commented because they were a bit obtuse --- src/config/config.go | 109 ++++++++++++++++++++++++++++++++++++++ src/config/config_test.go | 64 ++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/config/config.go create mode 100644 src/config/config_test.go diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 0000000..a1ad49e --- /dev/null +++ b/src/config/config.go @@ -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`.") + } +} diff --git a/src/config/config_test.go b/src/config/config_test.go new file mode 100644 index 0000000..0a667d1 --- /dev/null +++ b/src/config/config_test.go @@ -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) +// } +// }