diff --git a/config/env.go b/config/env.go index af8432b2..f3cef527 100644 --- a/config/env.go +++ b/config/env.go @@ -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 } diff --git a/go.mod b/go.mod index e34e5632..4d928529 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/morikuni/aec v1.0.0 // indirect github.com/olekukonko/tablewriter v0.0.5 github.com/schultz-is/passgen v1.0.1 + github.com/sirupsen/logrus v1.8.1 github.com/theupdateframework/notary v0.7.0 // indirect github.com/urfave/cli/v2 v2.3.0 //ct ) diff --git a/go.sum b/go.sum index a2a70d62..6a36eac6 100644 --- a/go.sum +++ b/go.sum @@ -596,8 +596,9 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=