feat(list-dbs): initial listdbscommand
This commit is contained in:
parent
2da42de7ab
commit
23376c3515
2 changed files with 95 additions and 26 deletions
51
cmd/list-dbs.go
Normal file
51
cmd/list-dbs.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// delCmd represents the set command
|
||||
var listDbsCmd = &cobra.Command{
|
||||
Use: "list-dbs",
|
||||
Short: "List all dbs.",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: listDbs,
|
||||
}
|
||||
|
||||
func listDbs(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
dbs, err := store.AllStores()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, db := range dbs {
|
||||
fmt.Println("@" + db)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listDbsCmd)
|
||||
}
|
||||
|
|
@ -35,6 +35,40 @@ import (
|
|||
|
||||
type Store struct{}
|
||||
|
||||
type TransactionArgs struct {
|
||||
key string
|
||||
readonly bool
|
||||
sync bool
|
||||
transact func(tx *badger.Txn, key []byte) error
|
||||
}
|
||||
|
||||
func (s *Store) Transaction(args TransactionArgs) error {
|
||||
k, dbName, err := s.parse(args.key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := s.open(dbName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if args.sync {
|
||||
err = db.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tx := db.NewTransaction(!args.readonly)
|
||||
if err := args.transact(tx, k); err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) parse(k string) ([]byte, string, error) {
|
||||
var key, db string
|
||||
ps := strings.Split(k, "@")
|
||||
|
|
@ -90,36 +124,20 @@ func (s *Store) Print(pf string, vs ...[]byte) {
|
|||
}
|
||||
}
|
||||
|
||||
type TransactionArgs struct {
|
||||
key string
|
||||
readonly bool
|
||||
sync bool
|
||||
transact func(tx *badger.Txn, key []byte) error
|
||||
}
|
||||
|
||||
func (s *Store) Transaction(args TransactionArgs) error {
|
||||
k, dbName, err := s.parse(args.key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := s.open(dbName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if args.sync {
|
||||
err = db.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tx := db.NewTransaction(!args.readonly)
|
||||
if err := args.transact(tx, k); err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
func (s *Store) AllStores() ([]string, error) {
|
||||
path, err := s.path()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dirs, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var stores []string
|
||||
for _, e := range dirs {
|
||||
if e.IsDir() {
|
||||
stores = append(stores, e.Name())
|
||||
}
|
||||
}
|
||||
return stores, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue