refactor: moving logging to command functions

easier to unit test our util commands like this
This commit is contained in:
Roxie Gibson 2021-07-19 12:47:46 +01:00
parent bd9bc530d1
commit cfe2f70151
Signed by untrusted user: roxxers
GPG Key ID: 5D0140EDEE123F4D
3 changed files with 42 additions and 25 deletions

View File

@ -56,7 +56,10 @@ var appListCommand = &cli.Command{
// FIXME: Needs to use flags
// TODO: Sorting of output to make servers in alphabetical
// Looks like sorting a 2d slice of strings might be messy though
apps := config.LoadAppFiles()
apps, err := config.LoadAppFiles()
if err != nil {
logrus.Fatal(err)
}
tableCol := []string{"Name", "Type", "Server"}
table := createTable(tableCol)
for name, appFile := range apps {

View File

@ -5,6 +5,7 @@ import (
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -18,13 +19,16 @@ var serverListCommand = &cli.Command{
dockerContextStore := client.NewDefaultDockerContextStore()
contexts, err := dockerContextStore.Store.List()
if err != nil {
panic(err)
logrus.Fatal(err)
}
tableColumns := []string{"Name", "Connection"}
table := createTable(tableColumns)
defer table.Render()
serverNames := config.ReadServerNames()
serverNames, err := config.ReadServerNames()
if err != nil {
logrus.Fatal(err)
}
for _, serverName := range serverNames {
var row []string

View File

@ -5,7 +5,6 @@ import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
@ -35,12 +34,18 @@ type AppFile struct {
type AppFiles = map[AppName]AppFile
func LoadAppFiles() AppFiles {
func LoadAppFiles() (AppFiles, error) {
appFiles := make(AppFiles)
servers := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
servers, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
if err != nil {
return nil, err
}
for _, server := range servers {
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
files := getAllFilesInDirectory(serverDir)
files, err := getAllFilesInDirectory(serverDir)
if err != nil {
return nil, err
}
for _, file := range files {
appName := strings.TrimSuffix(file.Name(), ".env")
appFilePath := path.Join(ABRA_SERVER_FOLDER, server, file.Name())
@ -50,7 +55,7 @@ func LoadAppFiles() AppFiles {
}
}
}
return appFiles
return appFiles, nil
}
// GetApp loads an apps settings, reading it from file, in preparation to use it
@ -64,15 +69,17 @@ func GetApp(apps AppFiles, name AppName) (App, error) {
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{}, err
}
return app, nil
}
func readAppFile(appFile AppFile, name AppName) (App, error) {
env := readEnv(appFile.Path)
env, err := readEnv(appFile.Path)
if err != nil {
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
}
app, err := makeApp(env, name)
if err != nil {
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
@ -80,13 +87,13 @@ func readAppFile(appFile AppFile, name AppName) (App, error) {
return app, nil
}
func readEnv(filePath string) AppEnv {
func readEnv(filePath string) (AppEnv, error) {
var envFile AppEnv
envFile, err := godotenv.Read(filePath)
if err != nil {
log.Fatalln(err.Error())
return nil, err
}
return envFile
return envFile, nil
}
func makeApp(env AppEnv, name string) (App, error) {
@ -107,17 +114,20 @@ func makeApp(env AppEnv, name string) (App, error) {
}, nil
}
func ReadServerNames() []string {
serverNames := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
return serverNames
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 {
func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
var realFiles []fs.FileInfo
files, err := ioutil.ReadDir(directory)
if err != nil {
logrus.Fatal(err.Error())
return nil, err
}
for _, file := range files {
// Follow any symlinks
@ -128,7 +138,7 @@ func getAllFilesInDirectory(directory string) []fs.FileInfo {
} else {
realFile, err := os.Stat(realPath)
if err != nil {
logrus.Fatal(err.Error())
return nil, err
}
if !realFile.IsDir() {
realFiles = append(realFiles, file)
@ -136,18 +146,18 @@ func getAllFilesInDirectory(directory string) []fs.FileInfo {
}
}
return realFiles
return realFiles, nil
}
// getAllFoldersInDirectory returns both folder and symlink paths
func getAllFoldersInDirectory(directory string) []string {
func getAllFoldersInDirectory(directory string) ([]string, error) {
var folders []string
files, err := ioutil.ReadDir(directory)
if err != nil {
logrus.Fatal(err.Error())
return nil, err
}
if len(files) == 0 {
logrus.Fatal("directory is empty: '%s'", directory)
return nil, fmt.Errorf("directory is empty: '%s'", directory)
}
for _, file := range files {
// Check if file is directory or symlink
@ -162,5 +172,5 @@ func getAllFoldersInDirectory(directory string) []string {
}
}
}
return folders
return folders, nil
}