114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
/*
|
|
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 (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/dgraph-io/badger/v4"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// listCmd represents the set command
|
|
var listCmd = &cobra.Command{
|
|
Use: "list [DB]",
|
|
Short: "List the contents of a db.",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: list,
|
|
}
|
|
|
|
func list(cmd *cobra.Command, args []string) error {
|
|
store := &Store{}
|
|
targetDB := "@default"
|
|
if len(args) == 1 {
|
|
rawArg := args[0]
|
|
dbName, err := store.parseDB(rawArg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := store.FindStore(dbName); err != nil {
|
|
var notFound errNotFound
|
|
if errors.As(err, ¬Found) {
|
|
return fmt.Errorf("%q does not exist, %s", rawArg, err.Error())
|
|
}
|
|
return err
|
|
}
|
|
targetDB = "@" + dbName
|
|
}
|
|
|
|
delimiter, err := cmd.Flags().GetString("delimiter")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if delimiter == "" {
|
|
delimiter = "\t\t"
|
|
}
|
|
|
|
includeSecret, err := cmd.Flags().GetBool("secret")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
trans := TransactionArgs{
|
|
key: targetDB,
|
|
readonly: true,
|
|
sync: true,
|
|
transact: func(tx *badger.Txn, k []byte) error {
|
|
binary, err := cmd.Flags().GetBool("include-binary")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
format := fmt.Sprintf("%%s%s%%s\n", delimiter)
|
|
opts := badger.DefaultIteratorOptions
|
|
opts.PrefetchSize = 10
|
|
it := tx.NewIterator(opts)
|
|
defer it.Close()
|
|
for it.Rewind(); it.Valid(); it.Next() {
|
|
item := it.Item()
|
|
key := item.Key()
|
|
meta := item.UserMeta()
|
|
if err := item.Value(func(v []byte) error {
|
|
if meta&metaSecret != 0 && !includeSecret {
|
|
placeholder := []byte("[secret: pass --secret to view]")
|
|
store.Print(format, false, key, placeholder)
|
|
return nil
|
|
}
|
|
store.Print(format, binary, key, v)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return store.Transaction(trans)
|
|
}
|
|
|
|
func init() {
|
|
listCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
|
|
listCmd.Flags().StringP("delimiter", "d", "\t\t", "string written between key and value columns")
|
|
listCmd.Flags().Bool("secret", false, "include entries marked as secret")
|
|
rootCmd.AddCommand(listCmd)
|
|
}
|