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 // Flimit defines the max number of watched files
func (s *Settings) Flimit() error { func (s *Settings) Flimit() error {
var rLimit syscall.Rlimit var rLimit syscall.Rlimit
rLimit.Max = uint64(s.Config.Flimit) rLimit.Max = uint64(s.FileLimit)
rLimit.Cur = uint64(s.Config.Flimit) rLimit.Cur = uint64(s.FileLimit)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return err return err

View File

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

View File

@ -19,19 +19,19 @@ func (s Settings) Stream(file string) ([]byte, error) {
// Write a file // Write a file
func (s Settings) Write(name string, data []byte) error { 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) return s.Validate(err)
} }
// Create a new file and return its pointer // Create a new file and return its pointer
func (s Settings) Create(path string, name string) *os.File { func (s Settings) Create(path string, name string) *os.File {
var file string var file string
if _, err := os.Stat(directory); err == nil { if _, err := os.Stat(Directory); err == nil {
file = filepath.Join(path, directory, name) file = filepath.Join(path, Directory, name)
} else { } else {
file = filepath.Join(path, name) 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) s.Validate(err)
return out return out
} }

View File

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

View File

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