realize/settings/settings.go

61 lines
1.4 KiB
Go
Raw Normal View History

2016-11-01 09:56:12 +00:00
package settings
import (
"gopkg.in/yaml.v2"
"syscall"
)
type Settings struct {
Colors `yaml:"-"`
2016-11-20 17:04:11 +00:00
Resources `yaml:"resources,omitempty" json:"resources"`
Server `yaml:"server,omitempty" json:"server"`
Config `yaml:"config,omitempty" json:"config"`
2016-11-01 09:56:12 +00:00
}
type Config struct {
2016-11-20 16:58:52 +00:00
Flimit uint64 `yaml:"flimit" json:"flimit"`
2016-11-01 09:56:12 +00:00
}
type Server struct {
2016-11-20 16:58:52 +00:00
Enabled bool `yaml:"enable" json:"enable"`
Open bool `yaml:"open" json:"open"`
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
2016-11-01 09:56:12 +00:00
}
type Resources struct {
2016-11-20 16:58:52 +00:00
Config string `yaml:"-" json:"-"`
Output string `yaml:"output" json:"output"`
Log string `yaml:"log" json:"log"`
2016-11-01 09:56:12 +00:00
}
// Flimit defines the max number of watched files
func (s *Settings) Flimit() {
var rLimit syscall.Rlimit
rLimit.Max = s.Config.Flimit
rLimit.Cur = s.Config.Flimit
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
s.Fatal("Error Setting Rlimit", err)
}
}
// Read from the configuration file
func (s *Settings) Read(out interface{}) error {
content, err := s.Stream(s.Resources.Config)
if err == nil {
err = yaml.Unmarshal(content, out)
return err
}
return err
}
// Record create and unmarshal the yaml config file
func (h *Settings) Record(out interface{}) error {
y, err := yaml.Marshal(out)
if err != nil {
return err
}
return h.Write(h.Resources.Config, y)
}