From 8d1ccb25b64a5eaaa826f3a44016636cbf925c4d Mon Sep 17 00:00:00 2001 From: dubo-dubon-duponey Date: Mon, 9 Aug 2021 10:17:03 -0700 Subject: [PATCH 1/2] Flag to control configuration base directory Signed-off-by: dubo-dubon-duponey --- config/config.go | 36 ++++++++++++++++++++++++++++-------- main.go | 7 ++++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index ef9edff..fc5ea1c 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "path/filepath" "syscall" ) @@ -16,6 +17,7 @@ type Configuration struct { PulseSink string `json:"-"` DeviceName string `json:"-"` exitsSignals chan os.Signal + baseDir string } var Config = &Configuration{ @@ -24,28 +26,46 @@ var Config = &Configuration{ DeviceUUID: uuid.NewString(), } -func (c *Configuration) Load() { - data, err := ioutil.ReadFile(c.DeviceName + "/config.json") +func (c *Configuration) Load(baseDir string) error { + if baseDir == "" { + var err error + baseDir, err = os.Getwd() + if err != nil { + return err + } + } + + c.baseDir = baseDir + configFilePath := filepath.Join(c.baseDir, c.DeviceName, "/config.json") + data, err := ioutil.ReadFile(configFilePath) if err != nil || json.Unmarshal(data, &c) != nil { - log.Printf("%s is not valid - at new file will be created at program exit\n", c.DeviceName+"/config.json") + log.Printf("%s is not valid - a new file will be created at program exit\n", configFilePath) } c.exitsSignals = make(chan os.Signal, 1) signal.Notify(c.exitsSignals, syscall.SIGINT, syscall.SIGTERM) go func() { <-c.exitsSignals - c.Store() + err := c.Store() + if err != nil { + os.Exit(1) + } os.Exit(0) }() + + return nil } -func (c *Configuration) Store() { +func (c *Configuration) Store() error { data, err := json.Marshal(&c) if err != nil { - log.Printf("Warning: impossible to marshal configuration in json") + log.Printf("Warning: impossible to marshal configuration in json\n") + return err } - err = ioutil.WriteFile(c.DeviceName+"/config.json", data, 0660) + configFilePath := filepath.Join(c.baseDir, c.DeviceName, "/config.json") + err = ioutil.WriteFile(configFilePath, data, 0660) if err != nil { - log.Printf("Warning : impossible to store config file %s \n", c.DeviceName+"/config.json") + log.Printf("Warning : impossible to store config file %s \n", configFilePath) } + return err } diff --git a/main.go b/main.go index 9a72284..860da6b 100644 --- a/main.go +++ b/main.go @@ -21,14 +21,19 @@ import ( func main() { var ifName string var delay int64 + var configurationBaseDir string + flag.StringVar(&configurationBaseDir, "c", ".", "Configuration base directory (default to current directory)") flag.StringVar(&config.Config.DeviceName, "n", "goplay", "Specify device name") flag.StringVar(&ifName, "i", "eth0", "Specify interface") flag.Int64Var(&delay, "delay", 0, "Specify hardware delay in ms") flag.StringVar(&config.Config.PulseSink, "sink", config.Config.PulseSink, "Specify Pulse Audio Sink - Linux only") flag.Parse() // after declaring flags we need to call it - config.Config.Load() + err := config.Config.Load(configurationBaseDir) + if err != nil { + panic(err) + } defer config.Config.Store() globals.ErrLog = log.New(os.Stderr, "Error:", log.LstdFlags|log.Lshortfile|log.Lmsgprefix) From 1358c1dceb3e9799f51a0b6287f6b510be43b245 Mon Sep 17 00:00:00 2001 From: dubo-dubon-duponey Date: Mon, 9 Aug 2021 11:15:08 -0700 Subject: [PATCH 2/2] Allow specifying data storage path Signed-off-by: dubo-dubon-duponey --- config/config.go | 17 +++++++++++++++-- main.go | 21 +++++++++++++++++++-- pairing/controller.go | 5 +++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index fc5ea1c..08f3bc5 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type Configuration struct { DeviceUUID string `json:"device-uuid"` PulseSink string `json:"-"` DeviceName string `json:"-"` + DataDirectory string `json:"data-directory"` exitsSignals chan os.Signal baseDir string } @@ -36,11 +37,15 @@ func (c *Configuration) Load(baseDir string) error { } c.baseDir = baseDir - configFilePath := filepath.Join(c.baseDir, c.DeviceName, "/config.json") + configFilePath := filepath.Join(c.baseDir, c.DeviceName, "config.json") data, err := ioutil.ReadFile(configFilePath) if err != nil || json.Unmarshal(data, &c) != nil { log.Printf("%s is not valid - a new file will be created at program exit\n", configFilePath) } + if c.DataDirectory == "" { + c.DataDirectory = baseDir + } + c.exitsSignals = make(chan os.Signal, 1) signal.Notify(c.exitsSignals, syscall.SIGINT, syscall.SIGTERM) @@ -62,7 +67,15 @@ func (c *Configuration) Store() error { log.Printf("Warning: impossible to marshal configuration in json\n") return err } - configFilePath := filepath.Join(c.baseDir, c.DeviceName, "/config.json") + configFilePath := filepath.Join(c.baseDir, c.DeviceName, "config.json") + dirPath := filepath.Join(c.baseDir, c.DeviceName) + if _, err := os.Stat(dirPath); os.IsNotExist(err) { + err = os.MkdirAll(dirPath, 0755) + if err != nil{ + log.Printf("Warning : impossible to create config directory %s \n", filepath.Join(c.baseDir, c.DeviceName)) + return err + } + } err = ioutil.WriteFile(configFilePath, data, 0660) if err != nil { log.Printf("Warning : impossible to store config file %s \n", configFilePath) diff --git a/main.go b/main.go index 860da6b..58548fa 100644 --- a/main.go +++ b/main.go @@ -22,20 +22,37 @@ func main() { var ifName string var delay int64 var configurationBaseDir string + var flagsConfig config.Configuration - flag.StringVar(&configurationBaseDir, "c", ".", "Configuration base directory (default to current directory)") + // These two are being used to construct the config path + // XXX if the config has been manually update, it could very well have a devicename that does not align with the directory it's in + flag.StringVar(&configurationBaseDir, "c", "", "Configuration base directory (default to current directory)") flag.StringVar(&config.Config.DeviceName, "n", "goplay", "Specify device name") + + // These two should override whatever is in the currently store config + flag.StringVar(&flagsConfig.DataDirectory, "d", "", "Data base directory (defaults to configuration directory)") + flag.StringVar(&flagsConfig.PulseSink, "sink", config.Config.PulseSink, "Specify Pulse Audio Sink - Linux only") + + // These are not stored in permanent config flag.StringVar(&ifName, "i", "eth0", "Specify interface") flag.Int64Var(&delay, "delay", 0, "Specify hardware delay in ms") - flag.StringVar(&config.Config.PulseSink, "sink", config.Config.PulseSink, "Specify Pulse Audio Sink - Linux only") flag.Parse() // after declaring flags we need to call it + // Load the possibly existing config err := config.Config.Load(configurationBaseDir) if err != nil { panic(err) } defer config.Config.Store() + // Override config specifics with command-line flags + if flagsConfig.DataDirectory != "" { + config.Config.DataDirectory = flagsConfig.DataDirectory + } + if flagsConfig.PulseSink != "" { + config.Config.PulseSink = flagsConfig.PulseSink + } + globals.ErrLog = log.New(os.Stderr, "Error:", log.LstdFlags|log.Lshortfile|log.Lmsgprefix) iFace, err := net.InterfaceByName(ifName) diff --git a/pairing/controller.go b/pairing/controller.go index 431cc29..db4777c 100644 --- a/pairing/controller.go +++ b/pairing/controller.go @@ -1,10 +1,11 @@ package pairing import ( - "fmt" "github.com/brutella/hc/db" "github.com/brutella/hc/hap/pair" "github.com/brutella/hc/util" + "goplay2/config" + "path/filepath" ) type Controller struct { @@ -37,7 +38,7 @@ func (c Controller) Handle(cont util.Container) (util.Container, error) { } func NewController(deviceName string) (*Controller, error) { - storage, err := util.NewFileStorage(fmt.Sprintf("%s/db", deviceName)) + storage, err := util.NewFileStorage(filepath.Join(config.Config.DataDirectory, "db", deviceName)) if err != nil { return nil, err }