Skip to content

Commit

Permalink
refactor: remove storepath funcs from id package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonTheLeg committed Nov 2, 2023
1 parent 08fdea2 commit 2187ca9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
10 changes: 5 additions & 5 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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())
}
}
})
Expand Down
16 changes: 0 additions & 16 deletions konf/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"io/fs"
"path/filepath"
"strings"

"github.com/simontheleg/konf-go/config"
)

// ID unifies ID management that konf uses
Expand Down Expand Up @@ -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"
}

0 comments on commit 2187ca9

Please sign in to comment.