diff --git a/cmd/list-dbs.go b/cmd/list-dbs.go new file mode 100644 index 0000000..07fcd77 --- /dev/null +++ b/cmd/list-dbs.go @@ -0,0 +1,51 @@ +/* +Copyright © 2025 Lewis Wynne + +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) +} diff --git a/cmd/shared.go b/cmd/shared.go index 5796db6..8d80d13 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -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) +func (s *Store) AllStores() ([]string, error) { + path, err := s.path() if err != nil { - return err + return nil, err } - - db, err := s.open(dbName) + dirs, err := os.ReadDir(path) if err != nil { - return err + return nil, err } - defer db.Close() - - if args.sync { - err = db.Sync() - if err != nil { - return err + var stores []string + for _, e := range dirs { + if e.IsDir() { + stores = append(stores, e.Name()) } } - - tx := db.NewTransaction(!args.readonly) - if err := args.transact(tx, k); err != nil { - tx.Discard() - return err - } - return tx.Commit() + return stores, nil }