Skip to content

Commit

Permalink
Allow specifying data storage path
Browse files Browse the repository at this point in the history
Signed-off-by: dubo-dubon-duponey <dubodubonduponey+github@pm.me>
  • Loading branch information
dubo-dubon-duponey committed Aug 9, 2021
1 parent 8d1ccb2 commit 0da73ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
17 changes: 15 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func main() {
var delay int64
var configurationBaseDir string

flag.StringVar(&configurationBaseDir, "c", ".", "Configuration base directory (default to current directory)")
flag.StringVar(&configurationBaseDir, "c", "", "Configuration base directory (default to current directory)")
flag.StringVar(&config.Config.DataDirectory, "d", "", "Data base directory (defaults to configuration 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")
Expand Down
5 changes: 3 additions & 2 deletions pairing/controller.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0da73ef

Please sign in to comment.