// Execute runs the command with the given config and outputs.
Execute runs the command with the given config and outputs.
// Execute runs the command with the given config and outputs .
func (c Command) Execute(ctx context.Context, config *Config, args Args, outputs Outputs, streams genericclioptions.IOStreams) ([]byte, error) {
	data := TemplateData{
		LocalPort: config.LocalPort,
		Args:      args,
		Outputs:   outputs,
	}
	cmd, err := c.ToCmd(ctx, data)
	if err != nil {
		return nil, err
	}
	cmdStr, _ := c.Display(data)
	fmt.Fprintf(streams.ErrOut, "> %s\n", cmdStr)
	if c.Interactive {
		cmd.Stdout = streams.Out
		cmd.Stderr = streams.ErrOut
		cmd.Stdin = streams.In
		return []byte{}, cmd.Run()
	}
	outBuff := new(bytes.Buffer)
	errBuff := new(bytes.Buffer)
	ows := []io.Writer{outBuff}
	ews := []io.Writer{errBuff}
	if config.Verbose {
		ows = append(ows, streams.Out)
		ews = append(ews, streams.ErrOut)
	}
	cmd.Stdout = io.MultiWriter(ows...)
	cmd.Stderr = io.MultiWriter(ews...)
	if err := cmd.Run(); err != nil {
		args, _ := c.Args(data, TemplateOptions{
			ShowSensitive: false,
		})
		errStr := fmt.Sprintf("Error running command: %v\n%s\n", append([]string{c.Name()}, args...), errBuff)
		fmt.Fprint(streams.ErrOut, chalk.Red.Color(errStr))
		return nil, err
	}
	return outBuff.Bytes(), nil
}
func ( c Command ) Execute ( ctx context . Context , config * Config , args Args , outputs Outputs , streams genericclioptions . IOStreams ) ( [ ] byte , error ) { data := TemplateData { LocalPort : config . LocalPort , Args : args , Outputs : outputs , } cmd , err := c . ToCmd ( ctx , data ) if err != nil { return nil , err } cmdStr , _ := c . Display ( data ) fmt . Fprintf ( streams . ErrOut , " \n " , cmdStr ) if c . Interactive { cmd . Stdout = streams . Out cmd . Stderr = streams . ErrOut cmd . Stdin = streams . In return [ ] byte { } , cmd . Run ( ) } outBuff := new ( bytes . Buffer ) errBuff := new ( bytes . Buffer ) ows := [ ] io . Writer { outBuff } ews := [ ] io . Writer { errBuff } if config . Verbose { ows = append ( ows , streams . Out ) ews = append ( ews , streams . ErrOut ) } cmd . Stdout = io . MultiWriter ( ows ... ) cmd . Stderr = io . MultiWriter ( ews ... ) if err := cmd . Run ( ) ; err != nil { args , _ := c . Args ( data , TemplateOptions { ShowSensitive : false , } ) errStr := fmt . Sprintf ( " \n \n " , append ( [ ] string { c . Name ( ) } , args ... ) , errBuff ) fmt . Fprint ( streams . ErrOut , chalk . Red . Color ( errStr ) ) return nil , err } return outBuff . Bytes ( ) , nil }
Execute runs the command with the given config and outputs.
Execute runs the command with the given config and outputs .
// interactive commands cannot return