123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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"`
- }
|