abra/config/env.go

50 lines
952 B
Go
Raw Normal View History

package config
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os/user"
"github.com/joho/godotenv"
)
// TODO: envvar
const ABRA_DIR = ".abra"
var ABRA_SERVER_FOLDER = fmt.Sprintf("/%s/%s", ABRA_DIR, "servers")
func getHomeDir() string {
// Future: Windows support?
user, err := user.Current()
if err != nil {
log.Fatalf(err.Error())
}
return user.HomeDir
}
func ReadServerNames() []string {
var serverNames []string
files, err := ioutil.ReadDir(getHomeDir() + ABRA_SERVER_FOLDER)
if err != nil {
log.Fatal(err.Error())
}
for _, file := range files {
// Check if file is directory or symlink to one
if file.IsDir() || file.Mode()&fs.ModeSymlink != 0 {
serverNames = append(serverNames, file.Name())
}
}
return serverNames
}
func ReadEnv(filePath string) map[string]string {
var envFile map[string]string
envFile, err := godotenv.Read(filePath)
if err != nil {
panic(err) // TODO: Better logging
}
return envFile
}