diff --git a/cmd/set.go b/cmd/set.go index 26e6544..6efc4f9 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -69,7 +69,7 @@ func (c *setCmd) set(cmd *cobra.Command, args []string) error { id = konf.KonfID(args[0]) } - context, err := setContext(id, c.sm.Fs) + context, err := setContext(id, c.sm) if err != nil { return err } @@ -146,16 +146,16 @@ func idOfLatestKonf(sm *store.Storemanager) (konf.KonfID, error) { return konf.KonfID(b), nil } -func setContext(id konf.KonfID, f afero.Fs) (string, error) { - k, err := afero.ReadFile(f, id.StorePath()) +func setContext(id konf.KonfID, sm *store.Storemanager) (string, error) { + k, err := afero.ReadFile(sm.Fs, sm.StorePathFromID(id)) if err != nil { return "", err } ppid := os.Getppid() konfID := konf.IDFromProcessID(ppid) - activeKonf := konfID.ActivePath() - err = afero.WriteFile(f, activeKonf, k, utils.KonfPerm) + activeKonf := sm.ActivePathFromID(konfID) + err = afero.WriteFile(sm.Fs, activeKonf, k, utils.KonfPerm) if err != nil { return "", err } diff --git a/cmd/set_test.go b/cmd/set_test.go index 3e32f50..87c1f87 100644 --- a/cmd/set_test.go +++ b/cmd/set_test.go @@ -9,7 +9,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/manifoldco/promptui" - "github.com/simontheleg/konf-go/config" "github.com/simontheleg/konf-go/konf" "github.com/simontheleg/konf-go/prompt" "github.com/simontheleg/konf-go/store" @@ -127,9 +126,10 @@ func TestSaveLatestKonf(t *testing.T) { } func TestSetContext(t *testing.T) { - storeDir := config.StoreDir() + storeDir := "./konf/store" + activeDir := "./konf/active" ppid := os.Getppid() - sm := testhelper.SampleKonfManager{} + skm := testhelper.SampleKonfManager{} tt := map[string]struct { InID konf.KonfID @@ -141,7 +141,7 @@ func TestSetContext(t *testing.T) { "dev-eu_dev-eu", true, nil, - konf.IDFromProcessID(ppid).ActivePath(), + activeDir + "/" + string(konf.IDFromProcessID(ppid)) + ".yaml", }, "invalid id": { "i-am-invalid", @@ -155,12 +155,13 @@ func TestSetContext(t *testing.T) { t.Run(name, func(t *testing.T) { f := afero.NewMemMapFs() + sm := &store.Storemanager{Fs: f, Storedir: storeDir, Activedir: activeDir} if tc.StoreExists { - afero.WriteFile(f, storeDir+"/"+string(tc.InID)+".yaml", []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm) + afero.WriteFile(f, storeDir+"/"+string(tc.InID)+".yaml", []byte(skm.SingleClusterSingleContextEU()), utils.KonfPerm) } - resKonfPath, resError := setContext(tc.InID, f) + resKonfPath, resError := setContext(tc.InID, sm) if !errors.Is(resError, tc.ExpErr) { t.Errorf("Want error '%s', got '%s'", tc.ExpErr, resError) @@ -183,8 +184,8 @@ func TestSetContext(t *testing.T) { if err != nil { t.Errorf("Wanted to read file %q, but failed: %q", tc.ExpKonfPath, err) } - if string(res) != sm.SingleClusterSingleContextEU() { - t.Errorf("Exp content %q, got %q", res, sm.SingleClusterSingleContextEU()) + if string(res) != skm.SingleClusterSingleContextEU() { + t.Errorf("Exp content %q, got %q", res, skm.SingleClusterSingleContextEU()) } } }) diff --git a/konf/id.go b/konf/id.go index a54724a..f9e99a4 100644 --- a/konf/id.go +++ b/konf/id.go @@ -5,8 +5,6 @@ import ( "io/fs" "path/filepath" "strings" - - "github.com/simontheleg/konf-go/config" ) // ID unifies ID management that konf uses @@ -38,17 +36,3 @@ func IDFromProcessID(pid int) KonfID { func IDFromFileInfo(fi fs.FileInfo) KonfID { return KonfID(strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))) } - -// StorePathForID creates a valid filepath inside the configured storeDir -func (id KonfID) StorePath() string { - return genIDPath(config.StoreDir(), string(id)) -} - -// ActivePath creates a valid filepath inside the configured activeDir -func (id KonfID) ActivePath() string { - return genIDPath(config.ActiveDir(), string(id)) -} - -func genIDPath(path, id string) string { - return path + "/" + id + ".yaml" -}