refactor: moving logging to command functions
easier to unit test our util commands like this
This commit is contained in:
parent
bd9bc530d1
commit
cfe2f70151
|
@ -56,7 +56,10 @@ var appListCommand = &cli.Command{
|
||||||
// FIXME: Needs to use flags
|
// FIXME: Needs to use flags
|
||||||
// TODO: Sorting of output to make servers in alphabetical
|
// TODO: Sorting of output to make servers in alphabetical
|
||||||
// Looks like sorting a 2d slice of strings might be messy though
|
// 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"}
|
tableCol := []string{"Name", "Type", "Server"}
|
||||||
table := createTable(tableCol)
|
table := createTable(tableCol)
|
||||||
for name, appFile := range apps {
|
for name, appFile := range apps {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"coopcloud.tech/abra/client"
|
"coopcloud.tech/abra/client"
|
||||||
"coopcloud.tech/abra/config"
|
"coopcloud.tech/abra/config"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,13 +19,16 @@ var serverListCommand = &cli.Command{
|
||||||
dockerContextStore := client.NewDefaultDockerContextStore()
|
dockerContextStore := client.NewDefaultDockerContextStore()
|
||||||
contexts, err := dockerContextStore.Store.List()
|
contexts, err := dockerContextStore.Store.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
tableColumns := []string{"Name", "Connection"}
|
tableColumns := []string{"Name", "Connection"}
|
||||||
table := createTable(tableColumns)
|
table := createTable(tableColumns)
|
||||||
defer table.Render()
|
defer table.Render()
|
||||||
|
|
||||||
serverNames := config.ReadServerNames()
|
serverNames, err := config.ReadServerNames()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
for _, serverName := range serverNames {
|
for _, serverName := range serverNames {
|
||||||
|
|
||||||
var row []string
|
var row []string
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -35,12 +34,18 @@ type AppFile struct {
|
||||||
|
|
||||||
type AppFiles = map[AppName]AppFile
|
type AppFiles = map[AppName]AppFile
|
||||||
|
|
||||||
func LoadAppFiles() AppFiles {
|
func LoadAppFiles() (AppFiles, error) {
|
||||||
appFiles := make(AppFiles)
|
appFiles := make(AppFiles)
|
||||||
servers := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
servers, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
|
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
|
||||||
files := getAllFilesInDirectory(serverDir)
|
files, err := getAllFilesInDirectory(serverDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
appName := strings.TrimSuffix(file.Name(), ".env")
|
appName := strings.TrimSuffix(file.Name(), ".env")
|
||||||
appFilePath := path.Join(ABRA_SERVER_FOLDER, server, file.Name())
|
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
|
// 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)
|
return App{}, fmt.Errorf("cannot find app file with name '%s'", name)
|
||||||
}
|
}
|
||||||
app, err := readAppFile(appFile, name)
|
app, err := readAppFile(appFile, name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
return App{}, err
|
||||||
}
|
}
|
||||||
return app, nil
|
return app, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readAppFile(appFile AppFile, name AppName) (App, error) {
|
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)
|
app, err := makeApp(env, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
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
|
return app, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readEnv(filePath string) AppEnv {
|
func readEnv(filePath string) (AppEnv, error) {
|
||||||
var envFile AppEnv
|
var envFile AppEnv
|
||||||
envFile, err := godotenv.Read(filePath)
|
envFile, err := godotenv.Read(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
return envFile
|
return envFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeApp(env AppEnv, name string) (App, error) {
|
func makeApp(env AppEnv, name string) (App, error) {
|
||||||
|
@ -107,17 +114,20 @@ func makeApp(env AppEnv, name string) (App, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadServerNames() []string {
|
func ReadServerNames() ([]string, error) {
|
||||||
serverNames := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
serverNames, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
||||||
return serverNames
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return serverNames, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAllFilesInDirectory returns filenames of all files in directory
|
// 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
|
var realFiles []fs.FileInfo
|
||||||
files, err := ioutil.ReadDir(directory)
|
files, err := ioutil.ReadDir(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
// Follow any symlinks
|
// Follow any symlinks
|
||||||
|
@ -128,7 +138,7 @@ func getAllFilesInDirectory(directory string) []fs.FileInfo {
|
||||||
} else {
|
} else {
|
||||||
realFile, err := os.Stat(realPath)
|
realFile, err := os.Stat(realPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
if !realFile.IsDir() {
|
if !realFile.IsDir() {
|
||||||
realFiles = append(realFiles, file)
|
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
|
// getAllFoldersInDirectory returns both folder and symlink paths
|
||||||
func getAllFoldersInDirectory(directory string) []string {
|
func getAllFoldersInDirectory(directory string) ([]string, error) {
|
||||||
var folders []string
|
var folders []string
|
||||||
files, err := ioutil.ReadDir(directory)
|
files, err := ioutil.ReadDir(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
logrus.Fatal("directory is empty: '%s'", directory)
|
return nil, fmt.Errorf("directory is empty: '%s'", directory)
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
// Check if file is directory or symlink
|
// Check if file is directory or symlink
|
||||||
|
@ -162,5 +172,5 @@ func getAllFoldersInDirectory(directory string) []string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return folders
|
return folders, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue