From 1dc936957446059d0d63f8346246f92a085ea2a6 Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 3 Sep 2017 20:01:30 +0200 Subject: [PATCH] config fields moved to settings --- settings/flimit.go | 4 ++-- settings/flimit_test.go | 2 +- settings/io.go | 8 +++---- settings/settings.go | 45 +++++++++++++++++++-------------------- settings/settings_test.go | 16 ++++++++------ 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/settings/flimit.go b/settings/flimit.go index 6ba2df9..f22489e 100644 --- a/settings/flimit.go +++ b/settings/flimit.go @@ -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 diff --git a/settings/flimit_test.go b/settings/flimit_test.go index 4a50752..420738b 100644 --- a/settings/flimit_test.go +++ b/settings/flimit_test.go @@ -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) } diff --git a/settings/io.go b/settings/io.go index f59e6db..2124117 100644 --- a/settings/io.go +++ b/settings/io.go @@ -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 } diff --git a/settings/settings.go b/settings/settings.go index 5885a58..1eb70e6 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -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 } diff --git a/settings/settings_test.go b/settings/settings_test.go index c4d41a8..5387755 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -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)) }