2021-07-18 03:21:26 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
2021-07-19 06:04:37 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-31 10:47:09 +00:00
|
|
|
"github.com/Autonomic-Cooperative/godotenv"
|
2021-07-19 06:04:37 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-07-18 03:21:26 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 07:36:38 +00:00
|
|
|
var ABRA_DIR = os.ExpandEnv("$HOME/.abra")
|
2021-07-19 06:04:37 +00:00
|
|
|
var ABRA_SERVER_FOLDER = path.Join(ABRA_DIR, "servers")
|
2021-07-21 16:21:45 +00:00
|
|
|
var APPS_JSON = path.Join(ABRA_DIR, "apps.json")
|
2021-07-24 22:07:35 +00:00
|
|
|
var APPS_DIR = path.Join(ABRA_DIR, "apps")
|
|
|
|
var REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud"
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
func (a AppFiles) GetServers() []string {
|
|
|
|
var unique []string
|
|
|
|
servers := make(map[string]struct{})
|
|
|
|
for _, appFile := range a {
|
|
|
|
if _, ok := servers[appFile.Server]; !ok {
|
|
|
|
servers[appFile.Server] = struct{}{}
|
|
|
|
unique = append(unique, appFile.Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unique
|
|
|
|
}
|
2021-07-19 06:04:37 +00:00
|
|
|
|
2021-07-30 20:55:00 +00:00
|
|
|
func ReadEnv(filePath string) (AppEnv, error) {
|
2021-07-19 06:04:37 +00:00
|
|
|
var envFile AppEnv
|
|
|
|
envFile, err := godotenv.Read(filePath)
|
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return envFile, nil
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:47:46 +00:00
|
|
|
func ReadServerNames() ([]string, error) {
|
|
|
|
serverNames, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serverNames, nil
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAllFilesInDirectory returns filenames of all files in directory
|
2021-07-19 11:47:46 +00:00
|
|
|
func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
|
2021-07-19 07:36:38 +00:00
|
|
|
var realFiles []fs.FileInfo
|
|
|
|
files, err := ioutil.ReadDir(directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-19 07:36:38 +00:00
|
|
|
for _, file := range files {
|
|
|
|
// Follow any symlinks
|
|
|
|
filePath := path.Join(directory, file.Name())
|
|
|
|
realPath, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("broken symlink in your abra config folders: '%s'", filePath)
|
|
|
|
} else {
|
|
|
|
realFile, err := os.Stat(realPath)
|
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
|
|
|
if !realFile.IsDir() {
|
|
|
|
realFiles = append(realFiles, file)
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return realFiles, nil
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAllFoldersInDirectory returns both folder and symlink paths
|
2021-07-19 11:47:46 +00:00
|
|
|
func getAllFoldersInDirectory(directory string) ([]string, error) {
|
2021-07-19 06:04:37 +00:00
|
|
|
var folders []string
|
|
|
|
files, err := ioutil.ReadDir(directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
if len(files) == 0 {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, fmt.Errorf("directory is empty: '%s'", directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
2021-07-19 07:36:38 +00:00
|
|
|
// Check if file is directory or symlink
|
2021-07-18 03:21:26 +00:00
|
|
|
if file.IsDir() || file.Mode()&fs.ModeSymlink != 0 {
|
2021-07-19 06:04:37 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return folders, nil
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-28 20:10:42 +00:00
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// EnsureAbraDirExists checks for the abra config folder and throws error if not
|
2021-07-28 20:10:42 +00:00
|
|
|
func EnsureAbraDirExists() error {
|
|
|
|
if _, err := os.Stat(ABRA_DIR); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(ABRA_DIR, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|