github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/scripts/dbtool/cmd/list.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/dgraph-io/badger/v2"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func (d *dbTool) newListCommand() *cobra.Command {
    12  	cmd := cobra.Command{
    13  		Use:     "list",
    14  		Short:   "Lists db keys",
    15  		RunE:    d.runList,
    16  		PreRunE: d.openDB(true),
    17  	}
    18  	cmd.Flags().StringVarP(&d.prefix, "prefix", "p", "", "key prefix")
    19  	return &cmd
    20  }
    21  
    22  func (d *dbTool) runList(_ *cobra.Command, _ []string) error {
    23  	return d.db.View(func(txn *badger.Txn) error {
    24  		opts := badger.DefaultIteratorOptions
    25  		opts.PrefetchValues = false
    26  		opts.Prefix = []byte(d.prefix)
    27  		it := txn.NewIterator(opts)
    28  		defer it.Close()
    29  		for it.Rewind(); it.Valid(); it.Next() {
    30  			_, _ = fmt.Fprintf(os.Stdout, "%s\n", it.Item().Key())
    31  		}
    32  		return nil
    33  	})
    34  }