decentral1se
612fc5a531
All checks were successful
continuous-integration/drone/push Build is passing
142 lines
3.5 KiB
Go
142 lines
3.5 KiB
Go
package config
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io/fs"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Autonomic-Cooperative/godotenv"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var ABRA_DIR = os.ExpandEnv("$HOME/.abra")
|
|
var ABRA_SERVER_FOLDER = path.Join(ABRA_DIR, "servers")
|
|
var APPS_JSON = path.Join(ABRA_DIR, "apps.json")
|
|
var APPS_DIR = path.Join(ABRA_DIR, "apps")
|
|
var REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud"
|
|
|
|
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
|
|
}
|
|
|
|
func ReadEnv(filePath string) (AppEnv, error) {
|
|
var envFile AppEnv
|
|
envFile, err := godotenv.Read(filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return envFile, nil
|
|
}
|
|
|
|
func ReadServerNames() ([]string, error) {
|
|
serverNames, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return serverNames, nil
|
|
}
|
|
|
|
// getAllFilesInDirectory returns filenames of all files in directory
|
|
func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
|
|
var realFiles []fs.FileInfo
|
|
files, err := ioutil.ReadDir(directory)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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 {
|
|
return nil, err
|
|
}
|
|
if !realFile.IsDir() {
|
|
realFiles = append(realFiles, file)
|
|
}
|
|
}
|
|
|
|
}
|
|
return realFiles, nil
|
|
}
|
|
|
|
// getAllFoldersInDirectory returns both folder and symlink paths
|
|
func getAllFoldersInDirectory(directory string) ([]string, error) {
|
|
var folders []string
|
|
files, err := ioutil.ReadDir(directory)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(files) == 0 {
|
|
return nil, fmt.Errorf("directory is empty: '%s'", directory)
|
|
}
|
|
for _, file := range files {
|
|
// Check if file is directory or symlink
|
|
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, nil
|
|
}
|
|
|
|
// EnsureAbraDirExists checks for the abra config folder and throws error if not
|
|
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
|
|
}
|
|
|
|
func ReadAbraShEnvVars(abraSh string) (map[string]string, error) {
|
|
envVars := make(map[string]string)
|
|
|
|
file, err := os.Open(abraSh)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return envVars, fmt.Errorf("'%s' does not exist?", abraSh)
|
|
}
|
|
return envVars, err
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if strings.Contains(line, "export") {
|
|
splitVals := strings.Split(line, "export ")
|
|
envVarDef := splitVals[len(splitVals)-1]
|
|
keyVal := strings.Split(envVarDef, "=")
|
|
if len(keyVal) != 2 {
|
|
return envVars, fmt.Errorf("couldn't parse %s", line)
|
|
}
|
|
envVars[keyVal[0]] = keyVal[1]
|
|
}
|
|
}
|
|
|
|
return envVars, nil
|
|
}
|