package config import "os" import "io" import "github.com/BurntSushi/toml" import "github.com/go-yaml/yaml" import "github.com/alecthomas/hcl" func LoadTomlFileName(name string) (*Config, error) { conf := &Config{} fin, err := os.Open(name) if err != nil { return nil, err } defer fin.Close() dec := toml.NewDecoder(fin) _, err = dec.Decode(conf) return conf, err } func (c Config) SaveTomlFileName(name string) error { fin, err := os.Create(name) if err != nil { return err } defer fin.Close() dec := toml.NewEncoder(fin) err = dec.Encode(c) return err } func (c Config) SaveYamlFileName(name string) error { fin, err := os.Create(name) if err != nil { return err } defer fin.Close() dec := yaml.NewEncoder(fin) defer dec.Close() err = dec.Encode(c) return err } func LoadYamlFileName(name string) (*Config, error) { conf := &Config{} fin, err := os.Open(name) if err != nil { return nil, err } defer fin.Close() dec := yaml.NewDecoder(fin) err = dec.Decode(conf) return conf, err } func (c *Config) SaveHclFileName(name string) error { fin, err := os.Create(name) if err != nil { return err } defer fin.Close() buf, err := hcl.Marshal(c, hcl.InferHCLTags(true)) if err != nil { return err } _, err = fin.Write(buf) return err } func LoadHclFileName(name string) (*Config, error) { conf := &Config{} fin, err := os.Open(name) if err != nil { return nil, err } defer fin.Close() buf, err := io.ReadAll(fin) if err != nil { return nil, err } err = hcl.Unmarshal(buf, conf, hcl.InferHCLTags(true)) return conf, err } type Net struct { Id *int Listen string As string Command string } type Io struct { Name string Mime string } type Skill struct { Name string Inputs []Io Outputs []Io } type Peer struct { As string Skills []Skill } type Step struct { Type string Cmd string } type Job struct { Skill `toml:"skill"` Step []Step } type State struct { Name string Kind string Order int } type Config struct { Net `toml:"net"` Peers []Peer `toml:"peers"` Jobs []Job `toml:"jobs"` States []State `toml:"states"` }