feat(parser): adds explicit DB parser for when we don't want a key - should probably reject keyinput on match
This commit is contained in:
parent
af049a6ade
commit
fa224d9b38
2 changed files with 21 additions and 3 deletions
|
|
@ -28,7 +28,7 @@ import (
|
||||||
|
|
||||||
// listCmd represents the set command
|
// listCmd represents the set command
|
||||||
var listCmd = &cobra.Command{
|
var listCmd = &cobra.Command{
|
||||||
Use: "list [@DB]",
|
Use: "list [DB]",
|
||||||
Short: "List the contents of a db.",
|
Short: "List the contents of a db.",
|
||||||
Args: cobra.MaximumNArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
RunE: list,
|
RunE: list,
|
||||||
|
|
@ -38,7 +38,11 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
store := &Store{}
|
store := &Store{}
|
||||||
targetDB := "@default"
|
targetDB := "@default"
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
targetDB = args[0]
|
dbName, err := store.parseDB(args[0], false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
targetDB = "@" + dbName
|
||||||
}
|
}
|
||||||
|
|
||||||
trans := TransactionArgs{
|
trans := TransactionArgs{
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ func (s *Store) AllStores() ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) FindStore(k string) (string, error) {
|
func (s *Store) FindStore(k string) (string, error) {
|
||||||
_, n, err := s.parse(k, false)
|
n, err := s.parseDB(k, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -176,6 +176,20 @@ func (s *Store) parse(k string, defaults bool) ([]byte, string, error) {
|
||||||
return []byte(key), db, nil
|
return []byte(key), db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) parseDB(v string, defaults bool) (string, error) {
|
||||||
|
db := strings.TrimSpace(v)
|
||||||
|
if strings.HasPrefix(db, "@") {
|
||||||
|
db = strings.TrimPrefix(db, "@")
|
||||||
|
}
|
||||||
|
if db == "" {
|
||||||
|
if defaults {
|
||||||
|
return "default", nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("bad db format, use DB or @DB")
|
||||||
|
}
|
||||||
|
return strings.ToLower(db), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) open(name string) (*badger.DB, error) {
|
func (s *Store) open(name string) (*badger.DB, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = "default"
|
name = "default"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue