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

     1  package command
     2  
     3  import (
     4  	"os"
     5  	goexec "os/exec"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/pyroscope-io/pyroscope/pkg/cli"
    10  	"github.com/pyroscope-io/pyroscope/pkg/config"
    11  	"github.com/pyroscope-io/pyroscope/pkg/exec"
    12  )
    13  
    14  func newExecCmd(cfg *config.Exec) *cobra.Command {
    15  	vpr := newViper()
    16  	execCmd := &cobra.Command{
    17  		Use:   "exec [flags] <args>",
    18  		Short: "Start a new process from arguments and profile it",
    19  		Args:  cobra.MinimumNArgs(1),
    20  
    21  		DisableFlagParsing: true,
    22  		RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, args []string) error {
    23  			c, err := exec.NewExec(cfg, args)
    24  			if err != nil {
    25  				return err
    26  			}
    27  			err = c.Run()
    28  			// Normally, if the program ran, the call should return ExitError and
    29  			// the exit code must be preserved. Otherwise, the error originates from
    30  			// pyroscope and will be printed.
    31  			if e, ok := err.(*goexec.ExitError); ok {
    32  				// revive:disable-next-line:deep-exit for all effects this is main
    33  				os.Exit(e.ExitCode())
    34  			}
    35  			return err
    36  		}),
    37  	}
    38  
    39  	cli.PopulateFlagSet(cfg, execCmd.Flags(), vpr)
    40  	return execCmd
    41  }