forked from toolshed/abra
136 lines
3.0 KiB
Go
136 lines
3.0 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// TODO: envvar
|
|
const ABRA_DIR = ".abra"
|
|
|
|
var ABRA_SERVER_FOLDER = path.Join(ABRA_DIR, "servers")
|
|
|
|
// Type aliases to make code hints easier to understand
|
|
type AppEnv = map[string]string
|
|
type AppName = string
|
|
|
|
type App struct {
|
|
Name AppName
|
|
Type string
|
|
Domain string
|
|
Env AppEnv
|
|
}
|
|
type AppFile struct {
|
|
Path string
|
|
}
|
|
|
|
type AppFiles = map[AppName]AppFile
|
|
|
|
// func LoadAppFiles() (AppFiles, error) {
|
|
|
|
// }
|
|
|
|
// GetApp loads an apps settings, reading it from file, in preparation to use it
|
|
//
|
|
// ONLY use when ready to use the env file to keep IO down and
|
|
// because this exits with code 1 if the file cannot be found or is malformed
|
|
func GetApp(apps AppFiles, name AppName) (App, error) {
|
|
appFile, exists := apps[name]
|
|
if !exists {
|
|
return App{}, fmt.Errorf("cannot find app file with name '%s'", name)
|
|
}
|
|
app, err := readAppFile(appFile, name)
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
return app, nil
|
|
}
|
|
|
|
func readAppFile(appFile AppFile, name AppName) (App, error) {
|
|
env := readEnv(appFile.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 readEnv(filePath string) AppEnv {
|
|
var envFile AppEnv
|
|
envFile, err := godotenv.Read(filePath)
|
|
if err != nil {
|
|
log.Fatalln(err.Error())
|
|
}
|
|
return envFile
|
|
}
|
|
|
|
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 getHomeDir() string {
|
|
// Future: Windows support?
|
|
user, err := user.Current()
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
return user.HomeDir
|
|
}
|
|
|
|
func ReadServerNames() []string {
|
|
serverDir := path.Join(getHomeDir(), ABRA_SERVER_FOLDER)
|
|
serverNames := getAllFoldersInDirectory(serverDir)
|
|
return serverNames
|
|
}
|
|
|
|
// getAllFoldersInDirectory returns both folder and symlink paths
|
|
func getAllFoldersInDirectory(directory string) []string {
|
|
var folders []string
|
|
files, err := ioutil.ReadDir(directory)
|
|
if err != nil {
|
|
logrus.Fatal(err.Error())
|
|
}
|
|
if len(files) == 0 {
|
|
logrus.Fatal("directory is empty: '%s'", directory)
|
|
}
|
|
for _, file := range files {
|
|
// Check if file is directory or symlink to one
|
|
if file.IsDir() || file.Mode()&fs.ModeSymlink != 0 {
|
|
filePath := path.Join(directory, file.Name())
|
|
realDir, err := filepath.EvalSymlinks(filePath)
|
|
if err != nil {
|
|
logrus.Warningf("broken symlink in your abra config folders: '%s'", filePath)
|
|
} else if stat, err := os.Stat(realDir); err == nil && stat.IsDir() {
|
|
// path is a directory
|
|
folders = append(folders, file.Name())
|
|
}
|
|
|
|
}
|
|
}
|
|
return folders
|
|
}
|