realize/settings/settings.go

98 lines
2.3 KiB
Go
Raw Normal View History

2016-11-01 09:56:12 +00:00
package settings
import (
2017-07-27 18:48:16 +00:00
yaml "gopkg.in/yaml.v2"
2016-12-18 23:30:58 +00:00
"os"
2017-08-27 11:45:46 +00:00
"path/filepath"
2016-12-21 23:28:08 +00:00
"time"
2016-11-01 09:56:12 +00:00
)
2017-08-12 17:48:06 +00:00
// settings const
const (
2017-09-03 18:01:30 +00:00
Interval = 200
Permission = 0775
Directory = ".realize"
File = "realize.yaml"
FileOut = "outputs.log"
FileErr = "errors.log"
FileLog = "logs.log"
2017-08-12 17:48:06 +00:00
)
2017-04-16 10:02:47 +00:00
2017-09-03 18:01:30 +00:00
// Settings defines a group of general settings and options
2016-11-01 09:56:12 +00:00
type Settings struct {
2017-09-03 18:01:30 +00:00
File string `yaml:"-" json:"-"`
Make bool `yaml:"-" json:"-"`
2017-09-05 17:39:52 +00:00
Files `yaml:"files,omitempty" json:"files,omitempty"`
2017-09-03 18:01:30 +00:00
Legacy `yaml:"legacy,omitempty" json:"legacy,omitempty"`
Server `yaml:"server,omitempty" json:"server,omitempty"`
2017-09-03 18:01:30 +00:00
FileLimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
}
2017-04-01 18:50:06 +00:00
// Legacy configuration
type Legacy struct {
Interval time.Duration `yaml:"interval" json:"interval"`
2016-11-01 09:56:12 +00:00
}
2016-12-17 10:43:27 +00:00
// Server settings, used for the web panel
2016-11-01 09:56:12 +00:00
type Server struct {
Status bool `yaml:"status" json:"status"`
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
}
2017-09-03 18:01:30 +00:00
// Files defines the files generated by realize
type Files struct {
2017-08-31 07:43:11 +00:00
Outputs Resource `yaml:"outputs,omitempty" json:"outputs,omitempty"`
Logs Resource `yaml:"logs,omitempty" json:"log,omitempty"`
Errors Resource `yaml:"errors,omitempty" json:"error,omitempty"`
}
// Resource status and file name
type Resource struct {
Status bool
Name string
2016-11-01 09:56:12 +00:00
}
2016-12-16 22:58:16 +00:00
// Read from config file
2016-11-01 09:56:12 +00:00
func (s *Settings) Read(out interface{}) error {
2017-09-03 18:01:30 +00:00
localConfigPath := s.File
2017-08-12 17:48:06 +00:00
// backward compatibility
2017-09-03 18:01:30 +00:00
path := filepath.Join(Directory, s.File)
2017-08-27 11:45:46 +00:00
if _, err := os.Stat(path); err == nil {
localConfigPath = path
}
content, err := s.Stream(localConfigPath)
2016-11-01 09:56:12 +00:00
if err == nil {
err = yaml.Unmarshal(content, out)
return err
}
2017-08-12 17:48:06 +00:00
return err
2016-11-01 09:56:12 +00:00
}
// Record create and unmarshal the yaml config file
func (s *Settings) Record(out interface{}) error {
2017-09-03 18:01:30 +00:00
if s.Make {
y, err := yaml.Marshal(out)
if err != nil {
return err
}
2017-09-03 18:01:30 +00:00
if _, err := os.Stat(Directory); os.IsNotExist(err) {
if err = os.Mkdir(Directory, Permission); err != nil {
return s.Write(s.File, y)
}
}
2017-09-03 18:01:30 +00:00
return s.Write(filepath.Join(Directory, s.File), y)
}
return nil
}
// Remove realize folder
2017-08-12 17:48:06 +00:00
func (s *Settings) Remove(d string) error {
_, err := os.Stat(d)
if !os.IsNotExist(err) {
return os.RemoveAll(d)
}
2017-08-12 17:48:06 +00:00
return err
}