realize/settings/settings.go

86 lines
2.1 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"
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 {
Config `yaml:",inline" json:"config"`
2016-11-20 17:19:09 +00:00
Resources `yaml:"resources" json:"resources"`
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 {
Create bool `yaml:"-" json:"-"`
Flimit int64 `yaml:"flimit,omitempty" json:"flimit,omitempty"`
Legacy `yaml:"legacy,omitempty" json:"legacy,omitempty"`
}
2017-04-01 18:50:06 +00:00
// Legacy configuration
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 {
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:"-"`
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 {
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
}
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
func (s *Settings) Record(out interface{}) error {
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 {
return s.Write(s.Resources.Config, y)
}
}
2017-04-16 10:02:47 +00:00
return s.Write(Dir+s.Resources.Config, y)
}
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)
}
return nil
}