refactor: filesystem io

This commit is contained in:
2021-07-19 07:04:37 +01:00
parent 8c5e25bd01
commit f7059dbe98
3 changed files with 68 additions and 29 deletions

View File

@ -6,35 +6,58 @@ import (
"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 = fmt.Sprintf("/%s/%s", ABRA_DIR, "servers")
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 string
Name AppName
Type string
Domain string
Env AppEnv
}
type Server struct {
Name string
type AppFile struct {
Path 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())
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)
}
env := ReadEnv(path)
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())
@ -42,6 +65,15 @@ func GetApp(name string) (App, 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"]
@ -60,10 +92,6 @@ func makeApp(env AppEnv, name string) (App, error) {
}, nil
}
func findAppEndFile(name string) (string, error) {
return "", nil // FIXME: Placeholder
}
func getHomeDir() string {
// Future: Windows support?
user, err := user.Current()
@ -74,25 +102,34 @@ func getHomeDir() string {
}
func ReadServerNames() []string {
var serverNames []string
files, err := ioutil.ReadDir(getHomeDir() + ABRA_SERVER_FOLDER)
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 {
log.Fatal(err.Error())
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 {
serverNames = append(serverNames, file.Name())
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 serverNames
}
func ReadEnv(filePath string) AppEnv {
var envFile AppEnv
envFile, err := godotenv.Read(filePath)
if err != nil {
panic(err) // TODO: Better logging
}
return envFile
return folders
}