From 85b099c94b79941d415c34947cc77d65aedefa8b Mon Sep 17 00:00:00 2001 From: ebarajas Date: Mon, 10 Jan 2022 08:13:22 +0100 Subject: [PATCH] Hide pogreb type inside kv.DB struct --- README.md | 1 + cmd/add.go | 2 +- cmd/delete.go | 2 +- cmd/list.go | 2 +- cmd/print.go | 2 +- cmd/printall.go | 2 +- cmd/use.go | 2 +- pkg/kv/kv.go | 22 +++++++++++----------- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5588dbd..8fee3b2 100644 --- a/README.md +++ b/README.md @@ -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...) diff --git a/cmd/add.go b/cmd/add.go index c728731..1b13374 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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() diff --git a/cmd/delete.go b/cmd/delete.go index d631a35..fb46711 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -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() diff --git a/cmd/list.go b/cmd/list.go index 1d5dd13..df4f75e 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -31,7 +31,7 @@ func listFunc(cmd *cobra.Command, args []string) { retcode = 1 return } - defer db.CloseDB() + defer db.Close() var list []pterm.BulletListItem diff --git a/cmd/print.go b/cmd/print.go index 067196d..ac84984 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -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 { diff --git a/cmd/printall.go b/cmd/printall.go index beb87f1..91008c2 100644 --- a/cmd/printall.go +++ b/cmd/printall.go @@ -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 { diff --git a/cmd/use.go b/cmd/use.go index cbef44c..4e9a3e8 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -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...") diff --git a/pkg/kv/kv.go b/pkg/kv/kv.go index 6dd1a9d..88624ad 100644 --- a/pkg/kv/kv.go +++ b/pkg/kv/kv.go @@ -11,7 +11,7 @@ import ( ) type DB struct { - *pogreb.DB + db *pogreb.DB } func Open(path string) (*DB, error) { @@ -25,17 +25,17 @@ 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 } @@ -43,7 +43,7 @@ func (kv *DB) GetKubeconfig(key string) (*kubeconfig.Kubeconfig, error) { 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 } @@ -54,7 +54,7 @@ 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 } @@ -62,7 +62,7 @@ func (kv *DB) PutKubeconfig(key string, value []byte) error { 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 } @@ -70,7 +70,7 @@ func (kv *DB) PutKubeconfig(key string, value []byte) error { } 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 } @@ -78,7 +78,7 @@ func (kv *DB) DeleteKubeconfig(key string) error { 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 } @@ -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 { @@ -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 }