From 8d5d5ca34a237e789f6c02470422dc4d54962738 Mon Sep 17 00:00:00 2001 From: rouggy Date: Mon, 1 Jul 2024 23:24:53 +0700 Subject: [PATCH] update --- config.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..7a4f4d0 --- /dev/null +++ b/config.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v2" +) + +type Config struct { + Gotify struct { + URL string `yaml:"url"` + Token string `yanl:"token"` + } `yaml:"gotify"` + + Cluster struct { + Host string `yaml:"host"` + Call string `yaml:"call"` + } `yaml:"cluster"` +} + +func NewConfig(configPath string) (*Config, error) { + config := &Config{} + file, err := os.Open(configPath) + if err != nil { + return nil, err + } + defer file.Close() + d := yaml.NewDecoder(file) + + if err := d.Decode(&config); err != nil { + return nil, err + } + + return config, nil +} + +func ValidateConfigPath(path string) error { + s, err := os.Stat(path) + if err != nil { + return err + } + if s.IsDir() { + return fmt.Errorf("'%s' is a directory, not a normal file", path) + } + return nil +}