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"
|
2016-12-21 23:28:08 +00:00
|
|
|
"time"
|
2016-11-01 09:56:12 +00:00
|
|
|
)
|
|
|
|
|
2017-08-11 20:24:18 +00:00
|
|
|
// realize hidden dir
|
2017-04-16 10:02:47 +00:00
|
|
|
var Dir = ".realize/"
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Settings defines a group of general settings
|
2016-11-01 09:56:12 +00:00
|
|
|
type Settings struct {
|
2016-12-26 23:14:29 +00:00
|
|
|
Config `yaml:",inline" json:"config"`
|
2016-11-20 17:19:09 +00:00
|
|
|
Resources `yaml:"resources" json:"resources"`
|
2016-12-26 23:14:29 +00:00
|
|
|
Server `yaml:"server,omitempty" json:"server,omitempty"`
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Config defines structural options
|
2016-11-01 09:56:12 +00:00
|
|
|
type Config struct {
|
2017-03-12 22:46:58 +00:00
|
|
|
Create bool `yaml:"-" json:"-"`
|
|
|
|
Flimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
|
2016-12-26 23:14:29 +00:00
|
|
|
Legacy `yaml:"legacy,omitempty" json:"legacy,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:50:06 +00:00
|
|
|
// Legacy configuration
|
2016-12-26 23:14:29 +00:00
|
|
|
type Legacy struct {
|
|
|
|
Status bool `yaml:"status" json:"status"`
|
|
|
|
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 {
|
2016-12-26 23:14:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Resources defines the files generated by realize
|
2016-11-01 09:56:12 +00:00
|
|
|
type Resources struct {
|
2016-12-18 23:30:58 +00:00
|
|
|
Config string `yaml:"-" json:"-"`
|
2016-12-26 23:14:29 +00:00
|
|
|
Outputs string `yaml:"outputs" json:"outputs"`
|
2016-12-18 23:30:58 +00:00
|
|
|
Logs string `yaml:"logs" json:"log"`
|
|
|
|
Errors string `yaml:"errors" json:"error"`
|
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 {
|
2016-12-15 06:06:24 +00:00
|
|
|
localConfigPath := s.Resources.Config
|
2017-04-16 10:02:47 +00:00
|
|
|
if _, err := os.Stat(Dir + s.Resources.Config); err == nil {
|
|
|
|
localConfigPath = Dir + s.Resources.Config
|
2016-12-15 06:06:24 +00:00
|
|
|
}
|
|
|
|
content, err := s.Stream(localConfigPath)
|
2016-11-01 09:56:12 +00:00
|
|
|
if err == nil {
|
|
|
|
err = yaml.Unmarshal(content, out)
|
|
|
|
return err
|
|
|
|
}
|
2017-04-15 18:11:05 +00:00
|
|
|
return nil
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record create and unmarshal the yaml config file
|
2016-12-15 06:06:24 +00:00
|
|
|
func (s *Settings) Record(out interface{}) error {
|
2016-12-26 23:14:29 +00:00
|
|
|
if s.Config.Create {
|
|
|
|
y, err := yaml.Marshal(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-16 10:02:47 +00:00
|
|
|
if _, err := os.Stat(Dir); os.IsNotExist(err) {
|
|
|
|
if err = os.Mkdir(Dir, 0770); err != nil {
|
2016-12-26 23:14:29 +00:00
|
|
|
return s.Write(s.Resources.Config, y)
|
|
|
|
}
|
2016-12-17 00:48:04 +00:00
|
|
|
}
|
2017-04-16 10:02:47 +00:00
|
|
|
return s.Write(Dir+s.Resources.Config, y)
|
2016-12-26 23:14:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove realize folder
|
|
|
|
func (s *Settings) Remove() error {
|
2017-04-16 10:02:47 +00:00
|
|
|
if _, err := os.Stat(Dir); !os.IsNotExist(err) {
|
|
|
|
return os.RemoveAll(Dir)
|
2016-12-26 23:14:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|