github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/cmd/pyroscope/command/convert.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/pyroscope-io/pyroscope/pkg/cli"
    11  	"github.com/pyroscope-io/pyroscope/pkg/config"
    12  	"github.com/pyroscope-io/pyroscope/pkg/convert"
    13  	"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
    14  	"github.com/pyroscope-io/pyroscope/pkg/structs/transporttrie"
    15  )
    16  
    17  func newConvertCmd(cfg *config.Convert) *cobra.Command {
    18  	vpr := newViper()
    19  	convertCmd := &cobra.Command{
    20  		Use:    "convert [flags]",
    21  		Short:  "Convert between different profiling formats",
    22  		Hidden: true,
    23  
    24  		DisableFlagParsing: true,
    25  		RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error {
    26  			return parse(os.Stdin, cfg.Format)
    27  		}),
    28  	}
    29  
    30  	cli.PopulateFlagSet(cfg, convertCmd.Flags(), vpr)
    31  	return convertCmd
    32  }
    33  
    34  func parse(input io.Reader, format string) error {
    35  	parser := convert.ParseGroups
    36  	switch format {
    37  	case "tree":
    38  		t := tree.New()
    39  		parser(input, func(name []byte, val int) {
    40  			t.Insert(name, uint64(val))
    41  		})
    42  		return t.SerializeTruncateNoDict(4096, os.Stdout)
    43  	case "trie":
    44  		t := transporttrie.New()
    45  		parser(input, func(name []byte, val int) {
    46  			t.Insert(name, uint64(val), true)
    47  		})
    48  		return t.Serialize(os.Stdout)
    49  	default:
    50  		return fmt.Errorf("unknown format: %s", format)
    51  	}
    52  }