2016-11-01 09:56:12 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
2016-12-17 00:48:04 +00:00
|
|
|
"os"
|
2016-12-17 10:43:27 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2016-11-01 09:56:12 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Colors `yaml:"-"`
|
2016-11-20 17:19:09 +00:00
|
|
|
Resources `yaml:"resources" json:"resources"`
|
|
|
|
Server `yaml:"server" json:"server"`
|
|
|
|
Config `yaml:"config" json:"config"`
|
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 {
|
2016-11-20 16:58:52 +00:00
|
|
|
Flimit uint64 `yaml:"flimit" json:"flimit"`
|
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-11-20 16:58:52 +00:00
|
|
|
Enabled bool `yaml:"enable" json:"enable"`
|
|
|
|
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-11-20 16:58:52 +00:00
|
|
|
Config string `yaml:"-" json:"-"`
|
|
|
|
Output string `yaml:"output" json:"output"`
|
|
|
|
Log string `yaml:"log" json:"log"`
|
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
|
2016-12-15 06:11:20 +00:00
|
|
|
if _, err := os.Stat(".realize/" + s.Resources.Config); err == nil {
|
2016-12-15 06:06:24 +00:00
|
|
|
localConfigPath = ".realize/" + 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
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record create and unmarshal the yaml config file
|
2016-12-15 06:06:24 +00:00
|
|
|
func (s *Settings) Record(out interface{}) error {
|
2016-11-01 09:56:12 +00:00
|
|
|
y, err := yaml.Marshal(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-15 06:06:24 +00:00
|
|
|
if _, err := os.Stat(".realize/"); os.IsNotExist(err) {
|
2016-12-17 00:48:04 +00:00
|
|
|
if err = os.Mkdir(".realize/", 0770); err != nil {
|
|
|
|
return s.Write(s.Resources.Config, y)
|
|
|
|
}
|
2016-12-15 06:06:24 +00:00
|
|
|
}
|
|
|
|
return s.Write(".realize/"+s.Resources.Config, y)
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|