Skip to content

Commit

Permalink
refactor: move id into separate konf pkg
Browse files Browse the repository at this point in the history
With this change we reduced the amount of code that is in ambiguously
named packages like utils. Also this opens the door for introducing a
Konfig type in the konf pkg next.
  • Loading branch information
SimonTheLeg committed Oct 23, 2022
1 parent beaa9e0 commit f791f9a
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 88 deletions.
10 changes: 5 additions & 5 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/mitchellh/go-ps"
"github.com/simontheleg/konf-go/config"
"github.com/simontheleg/konf-go/konf"
log "github.com/simontheleg/konf-go/log"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ An active config is considered unused when no process points to it anymore`,
func selfClean(f afero.Fs) error {
pid := os.Getppid()

fpath := utils.IDFromProcessID(pid).ActivePath()
fpath := konf.IDFromProcessID(pid).ActivePath()
err := f.Remove(fpath)

if errors.Is(err, fs.ErrNotExist) {
Expand All @@ -71,12 +71,12 @@ func cleanLeftOvers(f afero.Fs) error {
return err
}

for _, konf := range konfs {
for _, k := range konfs {
// We need to trim of the .yaml file extension to get to the PID
konfID := utils.IDFromFileInfo(konf)
konfID := konf.IDFromFileInfo(k)
pid, err := strconv.Atoi(string(konfID))
if err != nil {
log.Warn("file '%s' could not be converted into an int, and therefore cannot be a valid process id. Skip for cleanup", konf.Name())
log.Warn("file '%s' could not be converted into an int, and therefore cannot be a valid process id. Skip for cleanup", k.Name())
continue
}

Expand Down
17 changes: 9 additions & 8 deletions cmd/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/simontheleg/konf-go/config"
"github.com/simontheleg/konf-go/konf"
"github.com/simontheleg/konf-go/testhelper"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
Expand All @@ -29,13 +30,13 @@ func TestSelfClean(t *testing.T) {
"PID FS": {
ppidFS(),
nil,
[]string{activeDir + "/abc", utils.KonfID("1234").ActivePath()},
[]string{activeDir + "/abc", konf.KonfID("1234").ActivePath()},
[]string{activeDir + "/" + fmt.Sprint(ppid) + ".yaml"},
},
"PID file deleted by external source": {
ppidFileMissing(),
nil,
[]string{activeDir + "/abc", utils.KonfID("1234").ActivePath()},
[]string{activeDir + "/abc", konf.KonfID("1234").ActivePath()},
[]string{},
},
// Unfortunately it was not possible with afero to test what happens if
Expand Down Expand Up @@ -77,15 +78,15 @@ func ppidFS() afero.Fs {
ppid := os.Getppid()
fs := ppidFileMissing()
sm := testhelper.SampleKonfManager{}
afero.WriteFile(fs, utils.IDFromProcessID(ppid).ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
afero.WriteFile(fs, konf.IDFromProcessID(ppid).ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
return fs
}

func ppidFileMissing() afero.Fs {
fs := afero.NewMemMapFs()
sm := testhelper.SampleKonfManager{}
afero.WriteFile(fs, config.ActiveDir()+"/abc", []byte("I am not even a kubeconfig, what am I doing here?"), utils.KonfPerm)
afero.WriteFile(fs, utils.KonfID("1234").ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
afero.WriteFile(fs, konf.KonfID("1234").ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
return fs
}

Expand Down Expand Up @@ -128,7 +129,7 @@ func TestCleanLeftOvers(t *testing.T) {
}

for _, cmd := range cmdsRunning {
fpath := utils.IDFromProcessID(cmd.Process.Pid).ActivePath()
fpath := konf.IDFromProcessID(cmd.Process.Pid).ActivePath()
_, err := f.Stat(fpath)

if err != nil {
Expand All @@ -141,7 +142,7 @@ func TestCleanLeftOvers(t *testing.T) {
}

for _, cmd := range cmdsStopped {
fpath := utils.IDFromProcessID(cmd.Process.Pid).ActivePath()
fpath := konf.IDFromProcessID(cmd.Process.Pid).ActivePath()
_, err := f.Stat(fpath)

if !errors.Is(err, fs.ErrNotExist) {
Expand Down Expand Up @@ -175,7 +176,7 @@ func mixedFSWithAllProcs(t *testing.T) (fs afero.Fs, cmdsRunning []*exec.Cmd, cm
}
pid := cmd.Process.Pid
cmdsRunning = append(cmdsRunning, cmd)
afero.WriteFile(fs, utils.IDFromProcessID(pid).ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
afero.WriteFile(fs, konf.IDFromProcessID(pid).ActivePath(), []byte(sm.SingleClusterSingleContextEU()), utils.KonfPerm)
}

return fs, cmdsRunning, nil
Expand Down Expand Up @@ -215,7 +216,7 @@ func mixedFSIncompleteProcs(t *testing.T) (fs afero.Fs, cmdsRunning []*exec.Cmd,
func mixedFSDirtyDir(t *testing.T) (fs afero.Fs, cmdsRunning []*exec.Cmd, cmdsStopped []*exec.Cmd) {
fs, cmdsRunning, cmdsStopped = mixedFSIncompleteProcs(t)

afero.WriteFile(fs, utils.KonfID("/not-a-valid-process-id").ActivePath(), []byte{}, utils.KonfPerm)
afero.WriteFile(fs, konf.KonfID("/not-a-valid-process-id").ActivePath(), []byte{}, utils.KonfPerm)

return fs, cmdsRunning, cmdsStopped

Expand Down
24 changes: 12 additions & 12 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"github.com/simontheleg/konf-go/konf"
"github.com/simontheleg/konf-go/log"
"github.com/simontheleg/konf-go/prompt"
"github.com/simontheleg/konf-go/store"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand All @@ -13,9 +13,9 @@ type deleteCmd struct {
fs afero.Fs

fetchconfs func(afero.Fs) ([]*store.Metadata, error)
selectSingleKonf func(afero.Fs, prompt.RunFunc) (utils.KonfID, error)
deleteKonfWithID func(afero.Fs, utils.KonfID) error
idsForGlobs func(afero.Fs, []string) ([]utils.KonfID, error)
selectSingleKonf func(afero.Fs, prompt.RunFunc) (konf.KonfID, error)
deleteKonfWithID func(afero.Fs, konf.KonfID) error
idsForGlobs func(afero.Fs, []string) ([]konf.KonfID, error)
prompt prompt.RunFunc

cmd *cobra.Command
Expand Down Expand Up @@ -49,11 +49,11 @@ func newDeleteCommand() *deleteCmd {
}

func (c *deleteCmd) delete(cmd *cobra.Command, args []string) error {
var ids []utils.KonfID
var ids []konf.KonfID
var err error

if len(args) == 0 {
var id utils.KonfID
var id konf.KonfID
id, err = c.selectSingleKonf(c.fs, c.prompt)
if err != nil {
return err
Expand All @@ -76,7 +76,7 @@ func (c *deleteCmd) delete(cmd *cobra.Command, args []string) error {
return nil
}

func deleteKonfWithID(fs afero.Fs, id utils.KonfID) error {
func deleteKonfWithID(fs afero.Fs, id konf.KonfID) error {
if err := fs.Remove(id.StorePath()); err != nil {
return err
}
Expand All @@ -86,15 +86,15 @@ func deleteKonfWithID(fs afero.Fs, id utils.KonfID) error {

// idsForGlobs takes in a slice of patterns and returns corresponding IDs from
// the konfStore
func idsForGlobs(f afero.Fs, patterns []string) ([]utils.KonfID, error) {
var ids []utils.KonfID
func idsForGlobs(f afero.Fs, patterns []string) ([]konf.KonfID, error) {
var ids []konf.KonfID
for _, pattern := range patterns {
metadata, err := store.FetchKonfsForGlob(f, pattern) // resolve any globs among the arguments
if err != nil {
return nil, err
}
for _, f := range metadata {
id := utils.IDFromClusterAndContext(f.Cluster, f.Context)
id := konf.IDFromClusterAndContext(f.Cluster, f.Context)
ids = append(ids, id)
}
}
Expand All @@ -114,10 +114,10 @@ func (c *deleteCmd) completeDelete(cmd *cobra.Command, args []string, toComplete
}

sug := []string{}
for _, konf := range konfs {
for _, k := range konfs {
// with the current design of 'set', we need to return the ID here in the autocomplete as the first part of the completion
// as it is directly passed to set
sug = append(sug, string(utils.IDFromClusterAndContext(konf.Cluster, konf.Context)))
sug = append(sug, string(konf.IDFromClusterAndContext(k.Cluster, k.Context)))
}

return sug, cobra.ShellCompDirectiveNoFileComp
Expand Down
12 changes: 6 additions & 6 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/simontheleg/konf-go/config"
"github.com/simontheleg/konf-go/konf"
"github.com/simontheleg/konf-go/prompt"
"github.com/simontheleg/konf-go/store"
"github.com/simontheleg/konf-go/testhelper"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"
Expand All @@ -23,7 +23,7 @@ func TestDeleteKonfWithID(t *testing.T) {

tt := map[string]struct {
fsCreator func() afero.Fs
idToDelete utils.KonfID
idToDelete konf.KonfID
expError error
expFiles []string
notExpFiles []string
Expand Down Expand Up @@ -157,17 +157,17 @@ func TestDelete(t *testing.T) {
idsForGlobsCalled := 0
deleteKonfWithIDCalled := 0

var mockSelectSingleKonf = func(afero.Fs, prompt.RunFunc) (utils.KonfID, error) {
var mockSelectSingleKonf = func(afero.Fs, prompt.RunFunc) (konf.KonfID, error) {
selectSingleKonfCalled++
return "id1", nil
}

var mockIDsForGlobs = func(afero.Fs, []string) ([]utils.KonfID, error) {
var mockIDsForGlobs = func(afero.Fs, []string) ([]konf.KonfID, error) {
idsForGlobsCalled++
return []utils.KonfID{"id1", "id2", "id3"}, nil
return []konf.KonfID{"id1", "id2", "id3"}, nil
}

var mockDeleteKonfWithID = func(afero.Fs, utils.KonfID) error {
var mockDeleteKonfWithID = func(afero.Fs, konf.KonfID) error {
deleteKonfWithIDCalled++
return nil
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/simontheleg/konf-go/konf"
log "github.com/simontheleg/konf-go/log"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
Expand Down Expand Up @@ -127,20 +128,20 @@ func determineConfigs(f afero.Fs, fpath string) ([]*konfFile, error) {
}
}

var konf konfFile
var k konfFile
// TODO it might make sense to build in a duplicate detection here. This would ensure that the store is trustworthy, which in return makes it easy for
// TODO the set command as it does not need any verification
id := utils.IDFromClusterAndContext(cluster.Name, curCon.Name)
konf.FilePath = id.StorePath()
konf.Content.AuthInfos = append(konf.Content.AuthInfos, user)
konf.Content.Clusters = append(konf.Content.Clusters, cluster)
konf.Content.Contexts = append(konf.Content.Contexts, curCon)
id := konf.IDFromClusterAndContext(cluster.Name, curCon.Name)
k.FilePath = id.StorePath()
k.Content.AuthInfos = append(k.Content.AuthInfos, user)
k.Content.Clusters = append(k.Content.Clusters, cluster)
k.Content.Contexts = append(k.Content.Contexts, curCon)

konf.Content.APIVersion = origConf.APIVersion
konf.Content.Kind = origConf.Kind
konf.Content.CurrentContext = curCon.Name
k.Content.APIVersion = origConf.APIVersion
k.Content.Kind = origConf.Kind
k.Content.CurrentContext = curCon.Name

konfs = append(konfs, &konf)
konfs = append(konfs, &k)
}

return konfs, nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/simontheleg/konf-go/konf"
"github.com/simontheleg/konf-go/testhelper"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestImport(t *testing.T) {
}

var devEUControlGroup = &konfFile{
FilePath: utils.IDFromClusterAndContext("dev-eu-1", "dev-eu").StorePath(),
FilePath: konf.IDFromClusterAndContext("dev-eu-1", "dev-eu").StorePath(),
Content: k8s.Config{
APIVersion: "v1",
Kind: "Config",
Expand Down Expand Up @@ -135,7 +135,7 @@ var devEUControlGroup = &konfFile{
}

var devASIAControlGroup = &konfFile{
FilePath: utils.IDFromClusterAndContext("dev-asia-1", "dev-asia").StorePath(),
FilePath: konf.IDFromClusterAndContext("dev-asia-1", "dev-asia").StorePath(),
Content: k8s.Config{
APIVersion: "v1",
Kind: "Config",
Expand Down
27 changes: 14 additions & 13 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/manifoldco/promptui"
"github.com/simontheleg/konf-go/config"
"github.com/simontheleg/konf-go/konf"
log "github.com/simontheleg/konf-go/log"
"github.com/simontheleg/konf-go/prompt"
"github.com/simontheleg/konf-go/store"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (c *setCmd) set(cmd *cobra.Command, args []string) error {
// TODO if I stay with the mocking approach used in commands like
// namespace. This part should be refactored to allow for mocking
// the downstream funcs in order to test the if-else logic
var id utils.KonfID
var id konf.KonfID
var err error

if len(args) == 0 {
Expand All @@ -64,7 +65,7 @@ func (c *setCmd) set(cmd *cobra.Command, args []string) error {
return err
}
} else {
id = utils.KonfID(args[0])
id = konf.KonfID(args[0])
}

context, err := setContext(id, c.fs)
Expand Down Expand Up @@ -99,10 +100,10 @@ func (c *setCmd) completeSet(cmd *cobra.Command, args []string, toComplete strin
}

sug := []string{}
for _, konf := range konfs {
for _, k := range konfs {
// with the current design of 'set', we need to return the ID here in the autocomplete as the first part of the completion
// as it is directly passed to set
sug = append(sug, string(utils.IDFromClusterAndContext(konf.Cluster, konf.Context)))
sug = append(sug, string(konf.IDFromClusterAndContext(k.Cluster, k.Context)))
}

return sug, cobra.ShellCompDirectiveNoFileComp
Expand All @@ -114,7 +115,7 @@ func (c *setCmd) completeSet(cmd *cobra.Command, args []string, toComplete strin
// it is also being used by two commands: "set" and "delete". But because
// they are in the same package, we also cannot easily duplicate the code for
// each
func selectSingleKonf(f afero.Fs, pf prompt.RunFunc) (utils.KonfID, error) {
func selectSingleKonf(f afero.Fs, pf prompt.RunFunc) (konf.KonfID, error) {
k, err := store.FetchAllKonfs(f)
if err != nil {
return "", err
Expand All @@ -130,30 +131,30 @@ func selectSingleKonf(f afero.Fs, pf prompt.RunFunc) (utils.KonfID, error) {
}
sel := k[selPos]

return utils.IDFromClusterAndContext(sel.Cluster, sel.Context), nil
return konf.IDFromClusterAndContext(sel.Cluster, sel.Context), nil
}

func idOfLatestKonf(f afero.Fs) (utils.KonfID, error) {
func idOfLatestKonf(f afero.Fs) (konf.KonfID, error) {
b, err := afero.ReadFile(f, config.LatestKonfFilePath())
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("could not select latest konf, because no konf was yet set")
}
return "", err
}
return utils.KonfID(b), nil
return konf.KonfID(b), nil
}

func setContext(id utils.KonfID, f afero.Fs) (string, error) {
konf, err := afero.ReadFile(f, id.StorePath())
func setContext(id konf.KonfID, f afero.Fs) (string, error) {
k, err := afero.ReadFile(f, id.StorePath())
if err != nil {
return "", err
}

ppid := os.Getppid()
konfID := utils.IDFromProcessID(ppid)
konfID := konf.IDFromProcessID(ppid)
activeKonf := konfID.ActivePath()
err = afero.WriteFile(f, activeKonf, konf, utils.KonfPerm)
err = afero.WriteFile(f, activeKonf, k, utils.KonfPerm)
if err != nil {
return "", err
}
Expand All @@ -162,7 +163,7 @@ func setContext(id utils.KonfID, f afero.Fs) (string, error) {

}

func saveLatestKonf(f afero.Fs, id utils.KonfID) error {
func saveLatestKonf(f afero.Fs, id konf.KonfID) error {
return afero.WriteFile(f, config.LatestKonfFilePath(), []byte(id), utils.KonfPerm)
}

Expand Down
Loading

0 comments on commit f791f9a

Please sign in to comment.