realize/settings/settings.go

64 lines
1.6 KiB
Go

package settings
import (
"gopkg.in/yaml.v2"
"os"
)
// Settings defines a group of general settings
type Settings struct {
Colors `yaml:"-"`
Resources `yaml:"resources" json:"resources"`
Server `yaml:"server" json:"server"`
Config `yaml:"config" json:"config"`
}
// Config defines structural options
type Config struct {
Flimit uint64 `yaml:"flimit" json:"flimit"`
}
// Server settings, used for the web panel
type Server struct {
Enabled bool `yaml:"enable" json:"enable"`
Open bool `yaml:"open" json:"open"`
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
}
// Resources defines the files generated by realize
type Resources struct {
Config string `yaml:"-" json:"-"`
Streams string `yaml:"streams" json:"output"`
Logs string `yaml:"logs" json:"log"`
Errors string `yaml:"errors" json:"error"`
}
// Read from config file
func (s *Settings) Read(out interface{}) error {
localConfigPath := s.Resources.Config
if _, err := os.Stat(".realize/" + s.Resources.Config); err == nil {
localConfigPath = ".realize/" + s.Resources.Config
}
content, err := s.Stream(localConfigPath)
if err == nil {
err = yaml.Unmarshal(content, out)
return err
}
return err
}
// Record create and unmarshal the yaml config file
func (s *Settings) Record(out interface{}) error {
y, err := yaml.Marshal(out)
if err != nil {
return err
}
if _, err := os.Stat(".realize/"); os.IsNotExist(err) {
if err = os.Mkdir(".realize/", 0770); err != nil {
return s.Write(s.Resources.Config, y)
}
}
return s.Write(".realize/"+s.Resources.Config, y)
}