2023-07-28 11:04:21 +00:00
|
|
|
package fusenrender
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConfigServer struct {
|
2023-08-06 16:40:26 +00:00
|
|
|
ServerID uint64 `yaml:"serverid"`
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
Cluster []string `yaml:"cluster"`
|
2023-07-28 11:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *ConfigServer) Address() string {
|
|
|
|
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *ConfigServer) AddressWith(p int) string {
|
|
|
|
return fmt.Sprintf("%s:%d", cfg.Host, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(etcpath string) (*ConfigServer, error) {
|
|
|
|
f, err := os.Open(etcpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg := ConfigServer{}
|
|
|
|
err = yaml.NewDecoder(f).Decode(&cfg)
|
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadAllConfig(etcpath string) (result []*ConfigServer) {
|
|
|
|
|
|
|
|
filepath.WalkDir(etcpath, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if strings.HasSuffix(d.Name(), ".yaml") {
|
|
|
|
cfg, err := LoadConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
result = append(result, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|