Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conf dir #21

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)

Expand All @@ -15,7 +16,9 @@ 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
}

var Config = &Configuration{
Expand All @@ -24,28 +27,58 @@ 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)
}
if c.DataDirectory == "" {
c.DataDirectory = baseDir
}

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
}
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(c.DeviceName+"/config.json", data, 0660)
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
}
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,38 @@ import (
func main() {
var ifName string
var delay int64
var configurationBaseDir string
var flagsConfig config.Configuration

// 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

config.Config.Load()
// Load the possibly existing config
err := config.Config.Load(configurationBaseDir)
if err != nil {
panic(err)
Copy link
Member

@AlbanSeurat AlbanSeurat Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use log.panicf to have a meaningful message

}
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It start to be a good case to create a new file with all config related code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to give it a go if you are interested and refactor this proper.

Question though: are you attached to the idea that the config path must contain the DeviceName? IMHO this is problematic for a number of reasons (filesystem reserved chars vs. mDNS type, duplication of DeviceName in the path, on the command-line, and inside the config file)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all attached, the whole focus for me now is to have good sound and audio sync.

I am happy with a better configuration/usability if you want to help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed what matters most is good sound and audio sync :) - keep it coming!

Happy to help with the rest (build, packaging, UX) and then just feel free to pick whatever makes sense for you and the project.

Thanks again for doing this project - very much appreciated :)


globals.ErrLog = log.New(os.Stderr, "Error:", log.LstdFlags|log.Lshortfile|log.Lmsgprefix)

iFace, err := net.InterfaceByName(ifName)
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