WIP: Enviroment file loading and config management
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
7d5db5fee1
commit
6caa176308
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -15,6 +16,54 @@ const ABRA_DIR = ".abra"
|
||||||
|
|
||||||
var ABRA_SERVER_FOLDER = fmt.Sprintf("/%s/%s", ABRA_DIR, "servers")
|
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 {
|
func getHomeDir() string {
|
||||||
// Future: Windows support?
|
// Future: Windows support?
|
||||||
user, err := user.Current()
|
user, err := user.Current()
|
||||||
|
@ -39,8 +88,8 @@ func ReadServerNames() []string {
|
||||||
return serverNames
|
return serverNames
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadEnv(filePath string) map[string]string {
|
func ReadEnv(filePath string) AppEnv {
|
||||||
var envFile map[string]string
|
var envFile AppEnv
|
||||||
envFile, err := godotenv.Read(filePath)
|
envFile, err := godotenv.Read(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) // TODO: Better logging
|
panic(err) // TODO: Better logging
|
||||||
|
|
Loading…
Reference in New Issue