config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package config
  2. import "os"
  3. import "io"
  4. import "github.com/BurntSushi/toml"
  5. import "github.com/go-yaml/yaml"
  6. import "github.com/alecthomas/hcl"
  7. func LoadTomlFileName(name string) (*Config, error) {
  8. conf := &Config{}
  9. fin, err := os.Open(name)
  10. if err != nil {
  11. return nil, err
  12. }
  13. defer fin.Close()
  14. dec := toml.NewDecoder(fin)
  15. _, err = dec.Decode(conf)
  16. return conf, err
  17. }
  18. func (c Config) SaveTomlFileName(name string) error {
  19. fin, err := os.Create(name)
  20. if err != nil {
  21. return err
  22. }
  23. defer fin.Close()
  24. dec := toml.NewEncoder(fin)
  25. err = dec.Encode(c)
  26. return err
  27. }
  28. func (c Config) SaveYamlFileName(name string) error {
  29. fin, err := os.Create(name)
  30. if err != nil {
  31. return err
  32. }
  33. defer fin.Close()
  34. dec := yaml.NewEncoder(fin)
  35. defer dec.Close()
  36. err = dec.Encode(c)
  37. return err
  38. }
  39. func LoadYamlFileName(name string) (*Config, error) {
  40. conf := &Config{}
  41. fin, err := os.Open(name)
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer fin.Close()
  46. dec := yaml.NewDecoder(fin)
  47. err = dec.Decode(conf)
  48. return conf, err
  49. }
  50. func (c *Config) SaveHclFileName(name string) error {
  51. fin, err := os.Create(name)
  52. if err != nil {
  53. return err
  54. }
  55. defer fin.Close()
  56. buf, err := hcl.Marshal(c, hcl.InferHCLTags(true))
  57. if err != nil {
  58. return err
  59. }
  60. _, err = fin.Write(buf)
  61. return err
  62. }
  63. func LoadHclFileName(name string) (*Config, error) {
  64. conf := &Config{}
  65. fin, err := os.Open(name)
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer fin.Close()
  70. buf, err := io.ReadAll(fin)
  71. if err != nil {
  72. return nil, err
  73. }
  74. err = hcl.Unmarshal(buf, conf, hcl.InferHCLTags(true))
  75. return conf, err
  76. }
  77. type Net struct {
  78. Id *int
  79. Listen string
  80. As string
  81. Command string
  82. }
  83. type Io struct {
  84. Name string
  85. Mime string
  86. }
  87. type Skill struct {
  88. Name string
  89. Inputs []Io
  90. Outputs []Io
  91. }
  92. type Peer struct {
  93. As string
  94. Skills []Skill
  95. }
  96. type Step struct {
  97. Type string
  98. Cmd string
  99. }
  100. type Job struct {
  101. Skill `toml:"skill"`
  102. Step []Step
  103. }
  104. type State struct {
  105. Name string
  106. Kind string
  107. Order int
  108. }
  109. type Config struct {
  110. Net `toml:"net"`
  111. Peers []Peer `toml:"peers"`
  112. Jobs []Job `toml:"jobs"`
  113. States []State `toml:"states"`
  114. }