config fields moved to settings

This commit is contained in:
alessio 2017-09-03 20:01:30 +02:00
parent 0bf537424c
commit 1dc9369574
5 changed files with 39 additions and 36 deletions

View File

@ -7,8 +7,8 @@ import "syscall"
// Flimit defines the max number of watched files
func (s *Settings) Flimit() error {
var rLimit syscall.Rlimit
rLimit.Max = uint64(s.Config.Flimit)
rLimit.Cur = uint64(s.Config.Flimit)
rLimit.Max = uint64(s.FileLimit)
rLimit.Cur = uint64(s.FileLimit)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return err

View File

@ -6,7 +6,7 @@ import (
func TestSettings_Flimit(t *testing.T) {
s := Settings{}
s.Config.Flimit = 100
s.FileLimit = 100
if err := s.Flimit(); err != nil {
t.Fatal("Unable to increase limit", err)
}

View File

@ -19,19 +19,19 @@ func (s Settings) Stream(file string) ([]byte, error) {
// Write a file
func (s Settings) Write(name string, data []byte) error {
err := ioutil.WriteFile(name, data, permission)
err := ioutil.WriteFile(name, data, Permission)
return s.Validate(err)
}
// Create a new file and return its pointer
func (s Settings) Create(path string, name string) *os.File {
var file string
if _, err := os.Stat(directory); err == nil {
file = filepath.Join(path, directory, name)
if _, err := os.Stat(Directory); err == nil {
file = filepath.Join(path, Directory, name)
} else {
file = filepath.Join(path, name)
}
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, permission)
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, Permission)
s.Validate(err)
return out
}

View File

@ -9,27 +9,27 @@ import (
// settings const
const (
permission = 0775
directory = ".realize"
Interval = 200
Permission = 0775
Directory = ".realize"
File = "realize.yaml"
FileOut = "outputs.log"
FileErr = "errors.log"
FileLog = "logs.log"
)
// Settings defines a group of general settings
// Settings defines a group of general settings and options
type Settings struct {
Config `yaml:"config" json:"config"`
Resources `yaml:"resources,omitempty" json:"resources,omitempty"`
File string `yaml:"-" json:"-"`
Make bool `yaml:"-" json:"-"`
Files `yaml:"files" json:"files"`
Legacy `yaml:"legacy,omitempty" json:"legacy,omitempty"`
Server `yaml:"server,omitempty" json:"server,omitempty"`
}
// Config defines structural options
type Config struct {
Create bool `yaml:"-" json:"-"`
Flimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
Legacy `yaml:"legacy,omitempty" json:"legacy,omitempty"`
FileLimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
}
// Legacy configuration
type Legacy struct {
Status bool `yaml:"status" json:"status"`
Interval time.Duration `yaml:"interval" json:"interval"`
}
@ -41,9 +41,8 @@ type Server struct {
Port int `yaml:"port" json:"port"`
}
// Resources defines the files generated by realize
type Resources struct {
Config string `yaml:"-" json:"-"`
// Files defines the files generated by realize
type Files struct {
Outputs Resource `yaml:"outputs,omitempty" json:"outputs,omitempty"`
Logs Resource `yaml:"logs,omitempty" json:"log,omitempty"`
Errors Resource `yaml:"errors,omitempty" json:"error,omitempty"`
@ -57,9 +56,9 @@ type Resource struct {
// Read from config file
func (s *Settings) Read(out interface{}) error {
localConfigPath := s.Resources.Config
localConfigPath := s.File
// backward compatibility
path := filepath.Join(directory, s.Resources.Config)
path := filepath.Join(Directory, s.File)
if _, err := os.Stat(path); err == nil {
localConfigPath = path
}
@ -73,17 +72,17 @@ func (s *Settings) Read(out interface{}) error {
// Record create and unmarshal the yaml config file
func (s *Settings) Record(out interface{}) error {
if s.Config.Create {
if s.Make {
y, err := yaml.Marshal(out)
if err != nil {
return err
}
if _, err := os.Stat(directory); os.IsNotExist(err) {
if err = os.Mkdir(directory, permission); err != nil {
return s.Write(s.Resources.Config, y)
if _, err := os.Stat(Directory); os.IsNotExist(err) {
if err = os.Mkdir(Directory, Permission); err != nil {
return s.Write(s.File, y)
}
}
return s.Write(filepath.Join(directory, s.Resources.Config), y)
return s.Write(filepath.Join(Directory, s.File), y)
}
return nil
}

View File

@ -9,17 +9,21 @@ import (
func TestSettings_Read(t *testing.T) {
s := Settings{}
var a interface{}
s.Resources.Config = "settings_b"
s.File = "settings_b"
if err := s.Read(a); err == nil {
t.Fatal("Error unexpected", err)
}
s.Resources.Config = "settings_test.yaml"
d, err := ioutil.TempFile("", "settings_test.yaml")
s.File = "settings_test.yaml"
dir, err := ioutil.TempDir("", Directory)
if err != nil {
t.Fatal(err)
}
s.Resources.Config = d.Name()
d, err := ioutil.TempFile(dir, "settings_test.yaml")
if err != nil {
t.Fatal(err)
}
s.File = d.Name()
if err := s.Read(a); err != nil {
t.Fatal("Error unexpected", err)
}
@ -42,10 +46,10 @@ func TestSettings_Remove(t *testing.T) {
func TestSettings_Record(t *testing.T) {
s := Settings{}
s.Resources.Config = "settings_test.yaml"
s.File = "settings_test.yaml"
var a interface{}
if err := s.Record(a); err != nil {
t.Fatal(err)
}
s.Remove(filepath.Join(directory, s.Resources.Config))
s.Remove(filepath.Join(Directory, s.File))
}