forked from coop-cloud/abra
WIP: Enviroment file loading and config management
This commit is contained in:
parent
7d5db5fee1
commit
6caa176308
@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
@ -15,6 +16,54 @@ const ABRA_DIR = ".abra"
|
||||
|
||||
var ABRA_SERVER_FOLDER = fmt.Sprintf("/%s/%s", ABRA_DIR, "servers")
|
||||
|
||||
type AppEnv = map[string]string
|
||||
|
||||
type App struct {
|
||||
Name string
|
||||
Type string
|
||||
Domain string
|
||||
Env AppEnv
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func GetApp(name string) (App, error) {
|
||||
path, err := findAppEndFile(name)
|
||||
if err != nil {
|
||||
return App{}, fmt.Errorf("could not find env file for %s: %s", name, err.Error())
|
||||
}
|
||||
env := ReadEnv(path)
|
||||
app, err := makeApp(env, name)
|
||||
if err != nil {
|
||||
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
||||
}
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func makeApp(env AppEnv, name string) (App, error) {
|
||||
// Checking for domain and type as they are required - apps wont work without them
|
||||
domain, ok := env["DOMAIN"]
|
||||
if !ok {
|
||||
return App{}, errors.New("missing DOMAIN variable")
|
||||
}
|
||||
apptype, ok := env["TYPE"]
|
||||
if !ok {
|
||||
return App{}, errors.New("missing TYPE variable")
|
||||
}
|
||||
return App{
|
||||
Name: name,
|
||||
Domain: domain,
|
||||
Type: apptype,
|
||||
Env: env,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func findAppEndFile(name string) (string, error) {
|
||||
return "", nil // FIXME: Placeholder
|
||||
}
|
||||
|
||||
func getHomeDir() string {
|
||||
// Future: Windows support?
|
||||
user, err := user.Current()
|
||||
@ -39,8 +88,8 @@ func ReadServerNames() []string {
|
||||
return serverNames
|
||||
}
|
||||
|
||||
func ReadEnv(filePath string) map[string]string {
|
||||
var envFile map[string]string
|
||||
func ReadEnv(filePath string) AppEnv {
|
||||
var envFile AppEnv
|
||||
envFile, err := godotenv.Read(filePath)
|
||||
if err != nil {
|
||||
panic(err) // TODO: Better logging
|
||||
|
Loading…
Reference in New Issue
Block a user