From c5724d56f81a8b2e8bc8c6bba5d2258fcf7f4471 Mon Sep 17 00:00:00 2001 From: p4u1 Date: Wed, 10 Jul 2024 14:06:07 +0200 Subject: [PATCH] fix(config): Removes config file name from abra dir --- pkg/config/abra.go | 20 +++++++++++++------- pkg/config/abra_test.go | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/pkg/config/abra.go b/pkg/config/abra.go index 0b13b04d..2f8d41dc 100644 --- a/pkg/config/abra.go +++ b/pkg/config/abra.go @@ -34,7 +34,7 @@ func LoadAbraConfig() Abra { return Abra{} } log.Debugf("config file loaded from: %s", configFile) - config.configPath = configFile + config.configPath = filepath.Dir(configFile) return config } @@ -88,16 +88,22 @@ func (a Abra) GetAbraDir() string { return os.ExpandEnv("$HOME/.abra") } +func (a Abra) GetServersDir() string { return path.Join(a.GetAbraDir(), "servers") } +func (a Abra) GetRecipesDir() string { return path.Join(a.GetAbraDir(), "recipes") } +func (a Abra) GetVendorDir() string { return path.Join(a.GetAbraDir(), "vendor") } +func (a Abra) GetBackupDir() string { return path.Join(a.GetAbraDir(), "backups") } +func (a Abra) GetCatalogueDir() string { return path.Join(a.GetAbraDir(), "catalogue") } + var config = LoadAbraConfig() var ( ABRA_DIR = config.GetAbraDir() - SERVERS_DIR = path.Join(ABRA_DIR, "servers") - RECIPES_DIR = path.Join(ABRA_DIR, "recipes") - VENDOR_DIR = path.Join(ABRA_DIR, "vendor") - BACKUP_DIR = path.Join(ABRA_DIR, "backups") - CATALOGUE_DIR = path.Join(ABRA_DIR, "catalogue") - RECIPES_JSON = path.Join(ABRA_DIR, "catalogue", "recipes.json") + SERVERS_DIR = config.GetServersDir() + RECIPES_DIR = config.GetRecipesDir() + VENDOR_DIR = config.GetVendorDir() + BACKUP_DIR = config.GetBackupDir() + CATALOGUE_DIR = config.GetCatalogueDir() + RECIPES_JSON = path.Join(config.GetCatalogueDir(), "recipes.json") REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud" CATALOGUE_JSON_REPO_NAME = "recipes-catalogue-json" SSH_URL_TEMPLATE = "ssh://git@git.coopcloud.tech:2222/coop-cloud/%s.git" diff --git a/pkg/config/abra_test.go b/pkg/config/abra_test.go index f60ad6a9..77b1fb2a 100644 --- a/pkg/config/abra_test.go +++ b/pkg/config/abra_test.go @@ -71,8 +71,7 @@ func TestLoadAbraConfigGetAbraDir(t *testing.T) { } cfg := LoadAbraConfig() - log.Println(cfg.GetAbraDir()) - wantAbraDir := filepath.Join(wd, "testdata/abraconfig1/abra.yaml/foobar") + wantAbraDir := filepath.Join(wd, "testdata/abraconfig1/foobar") if cfg.GetAbraDir() != wantAbraDir { t.Errorf("\nwant: %s\ngot: %s", wantAbraDir, cfg.GetAbraDir()) } @@ -101,3 +100,34 @@ func TestLoadAbraConfigGetAbraDir(t *testing.T) { } }) } + +func TestLoadAbraConfigServersDir(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + t.Setenv("ABRA_DIR", "") + + t.Run("default", func(t *testing.T) { + cfg := LoadAbraConfig() + wantServersDir := os.ExpandEnv("$HOME/.abra/servers") + if cfg.GetServersDir() != wantServersDir { + t.Errorf("\nwant: %s\ngot: %s", wantServersDir, cfg.GetServersDir()) + } + }) + + t.Run("from config file", func(t *testing.T) { + t.Cleanup(func() { os.Chdir(wd) }) + err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1")) + if err != nil { + log.Fatal(err) + } + + cfg := LoadAbraConfig() + log.Println(cfg) + wantServersDir := filepath.Join(wd, "testdata/abraconfig1/foobar/servers") + if cfg.GetServersDir() != wantServersDir { + t.Errorf("\nwant: %s\ngot: %s", wantServersDir, cfg.GetServersDir()) + } + }) +}