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

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/dgraph-io/badger/v2"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  func (d *dbTool) newGetCommand() *cobra.Command {
    13  	return &cobra.Command{
    14  		Use:     "get <key>",
    15  		Short:   "Retrieves key value and dumps it to stdout",
    16  		RunE:    d.runGet,
    17  		Args:    cobra.MinimumNArgs(1),
    18  		PreRunE: d.openDB(true),
    19  	}
    20  }
    21  
    22  func (d *dbTool) runGet(_ *cobra.Command, args []string) error {
    23  	return d.db.View(func(txn *badger.Txn) error {
    24  		item, err := txn.Get([]byte(args[0]))
    25  		if err != nil {
    26  			return err
    27  		}
    28  		v, err := item.ValueCopy(nil)
    29  		if err != nil {
    30  			return err
    31  		}
    32  		_, _ = io.Copy(os.Stdout, bytes.NewBuffer(v))
    33  		return nil
    34  	})
    35  }