Skip to content

Commit

Permalink
Hide pogreb type inside kv.DB struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarajas committed Jan 10, 2022
1 parent 03df8e3 commit 85b099c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ kubeswap delete -n test --db /tmp/kubeswap.db
- Compress the db into a single file, to enable
- simplicity: the user has a single file with all the database, not a directory
- backup/restore: easier to backup, restore or move between machines
- Refine the TUI list to show only the important stuff (remove '?' option for example)
- Cleanup! like a lot...
- Performance (we can just copy files if we know the name, not creating a kubeconfig.Kubeconfig etc etc...)
2 changes: 1 addition & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func addFunc(cmd *cobra.Command, args []string) {
retcode = 1
return
}
defer db.CloseDB()
defer db.Close()

log.Debug().Str("action", "adding new kubeconfig to the database").Str("key", kc.Name).Str("value", kc.Content).Send()

Expand Down
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func deleteFunc(cmd *cobra.Command, args []string) {
retcode = 1
return
}
defer db.CloseDB()
defer db.Close()

log.Debug().Str("action", "delete kubeconfig from db").Str("key", name).Send()

Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func listFunc(cmd *cobra.Command, args []string) {
retcode = 1
return
}
defer db.CloseDB()
defer db.Close()

var list []pterm.BulletListItem

Expand Down
2 changes: 1 addition & 1 deletion cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func printFunc(cmd *cobra.Command, args []string) {
if err != nil {
log.Error().Str("error", err.Error()).Msg("error opening kv database")
}
defer db.CloseDB()
defer db.Close()

kc, err := db.GetKubeconfig(name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/printall.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func printallFunc(cmd *cobra.Command, args []string) {
if err != nil {
log.Error().Str("error", err.Error()).Msg("error opening kv database")
}
defer db.CloseDB()
defer db.Close()

items, err := db.GetAll()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func useFunc(cmd *cobra.Command, args []string) {
retcode = 1
return
}
defer db.CloseDB()
defer db.Close()

if db.IsEmpty() {
log.Info().Msg("there are no kubeconfigs in the database. Exiting...")
Expand Down
22 changes: 11 additions & 11 deletions pkg/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type DB struct {
*pogreb.DB
db *pogreb.DB
}

func Open(path string) (*DB, error) {
Expand All @@ -25,25 +25,25 @@ func Open(path string) (*DB, error) {
return &DB{db}, nil
}

func (kv *DB) CloseDB() {
func (kv *DB) Close() {
// this should:
// - compress the real database folder into a single file
err := kv.Close()
err := kv.db.Close()
if err != nil {
log.Fatalf("error closing db: %s", err)
}
}

func (kv *DB) GetKubeconfig(key string) (*kubeconfig.Kubeconfig, error) {
exist, err := kv.Has([]byte(key))
exist, err := kv.db.Has([]byte(key))
if err != nil {
return nil, err
}
if !exist {
return nil, fmt.Errorf("key does not exist in the db: %s", key)
}

val, err := kv.Get([]byte(key))
val, err := kv.db.Get([]byte(key))
if err != nil {
return nil, err
}
Expand All @@ -54,31 +54,31 @@ func (kv *DB) GetKubeconfig(key string) (*kubeconfig.Kubeconfig, error) {
}

func (kv *DB) PutKubeconfig(key string, value []byte) error {
exist, err := kv.Has([]byte(key))
exist, err := kv.db.Has([]byte(key))
if err != nil {
return err
}
if exist {
return fmt.Errorf("key already exists in the db: %s", key)
}

err = kv.Put([]byte(key), value)
err = kv.db.Put([]byte(key), value)
if err != nil {
return err
}
return nil
}

func (kv *DB) DeleteKubeconfig(key string) error {
exist, err := kv.Has([]byte(key))
exist, err := kv.db.Has([]byte(key))
if err != nil {
return err
}
if !exist {
return fmt.Errorf("key does not exist in the db: %s", key)
}

err = kv.Delete([]byte(key))
err = kv.db.Delete([]byte(key))
if err != nil {
return err
}
Expand All @@ -87,7 +87,7 @@ func (kv *DB) DeleteKubeconfig(key string) error {

func (kv *DB) GetAll() ([]kubeconfig.Kubeconfig, error) {
var items []kubeconfig.Kubeconfig
it := kv.Items()
it := kv.db.Items()
for {
key, val, err := it.Next()
if err == pogreb.ErrIterationDone {
Expand All @@ -106,5 +106,5 @@ func (kv *DB) GetAll() ([]kubeconfig.Kubeconfig, error) {
}

func (kv *DB) IsEmpty() bool {
return kv.Count() == 0
return kv.db.Count() == 0
}

0 comments on commit 85b099c

Please sign in to comment.