feat: add config loading

main
Tran Hau 2021-01-12 10:53:51 +07:00
parent c78cd130c2
commit ceda041891
1 changed files with 47 additions and 0 deletions

47
config.go Normal file
View File

@ -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")
}