Added configuration

This commit is contained in:
2024-05-02 22:18:48 +01:00
parent a5e08510f0
commit c4054b3a23
5 changed files with 94 additions and 7 deletions
+78
View File
@@ -0,0 +1,78 @@
package config
import (
"log"
"os"
"slices"
"sync"
"thermalPrinter/device"
"gopkg.in/yaml.v2"
)
var (
instance *Configuration
once sync.Once
validUseValues = []string{
"socket",
"usb",
}
)
type usbDev struct {
Path string `yaml:"path"`
}
type socketDev struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
}
type Configuration struct {
Use string `yaml:"use"`
Usb *usbDev `yaml:"usb"`
Socket *socketDev `yaml:"socket"`
}
func GetInstance() *Configuration {
once.Do(func() {
instance = &Configuration{}
instance.loadConfiguration()
})
return instance
}
func (c *Configuration) loadConfiguration() {
confPath := "/home/esilva/Desktop/projetos/thermal_printer/config.yaml"
file, err := os.ReadFile(confPath)
if err != nil {
log.Fatalf("Unable to open configuration file [%s]: %s", confPath,
err.Error())
}
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`.")
}
}
func (c *Configuration) GetWriteableDevice() device.WriteableDevice {
if c.Use == "usb" {
return &device.PrinterUSB{
Path: c.Usb.Path,
}
} else if c.Use == "socket" {
return &device.PrinterSocket{
Host: c.Socket.Host,
Port: c.Socket.Port,
}
}
return nil
}
+3 -1
View File
@@ -21,7 +21,9 @@ func (p *PrinterSocket) Write(data []byte) (int, error) {
written, err := conn.Write(data)
if err != nil {
fmt.Println("ERROR: ", err)
errStr := fmt.Sprintf("Error: %s", err)
return written, errors.New(errStr)
}
return written, err
}
+1
View File
@@ -5,6 +5,7 @@ go 1.22.2
require (
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v2 v2.4.0
)
require (
+3
View File
@@ -8,5 +8,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+9 -6
View File
@@ -1,8 +1,10 @@
package thermalprinter
import (
"fmt"
"sync"
"thermalPrinter/config"
"thermalPrinter/device"
encodings "thermalPrinter/encoding"
)
@@ -19,7 +21,10 @@ type TP struct {
func (p *TP) Print(message string) {
processed := p.TextProcessor.Process(message)
p.Device.Write(processed)
_, err := p.Device.Write(processed)
if err != nil {
fmt.Println("Err: ", err)
}
}
func GetInstance(customDev device.WriteableDevice) *TP {
@@ -27,13 +32,11 @@ func GetInstance(customDev device.WriteableDevice) *TP {
var p encodings.Parser
p.Initialize()
devSocket := device.PrinterSocket{
Host: "",
Port: "",
}
cfg := config.GetInstance()
device := cfg.GetWriteableDevice()
instance = &TP{
Device: &devSocket,
Device: device,
TextProcessor: p,
}
})