From ceda0418919da3b7ce3c0cb8d01e2e84f9883033 Mon Sep 17 00:00:00 2001 From: Tran Hau Date: Tue, 12 Jan 2021 10:53:51 +0700 Subject: [PATCH] feat: add config loading --- 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..6581d49 --- /dev/null +++ b/config.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "os" + "path/filepath" +) + +type Config struct { + Apps map[string]App `json:"apps"` +} + +type App struct { + ConfigPath string `json:"config_path"` +} + +// Load config from file +func LoadConfig(path string) (Config, error) { + configPath := getConfigPath(path) + f, err := os.Open(configPath) + if err != nil { + // https://github.com/golang/go/blob/3e1e13ce6d1271f49f3d8ee359689145a6995bad/src/os/error.go#L90-L91 + if errors.Is(err, os.ErrNotExist) { + return Config{}, fmt.Errorf("file not exist %s: %w", configPath, err) + } + } + defer f.Close() + + bytes, err := ioutil.ReadAll(f) + if err != nil { + return Config{}, fmt.Errorf("failed to read %s: %w", configPath, err) + } + + var result Config + if err := json.Unmarshal(bytes, &result); err != nil { + return Config{}, fmt.Errorf("failed to unmarshal: %w,", err) + } + + return result, nil +} + +func getConfigPath(path string) string { + return filepath.Join(path, "configs/config.json") +}