github.com/MetalBlockchain/subnet-evm@v0.4.9/tests/utils/command.go (about)

     1  // Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package utils
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/ethereum/go-ethereum/log"
    12  	"github.com/go-cmd/cmd"
    13  )
    14  
    15  // RunCommand starts the command [bin] with the given [args] and returns the command to the caller
    16  // TODO cmd package mentions we can do this more efficiently with cmd.NewCmdOptions rather than looping
    17  // and calling Status().
    18  func RunCommand(bin string, args ...string) (*cmd.Cmd, error) {
    19  	log.Info("Executing", "cmd", fmt.Sprintf("%s %s", bin, strings.Join(args, " ")))
    20  
    21  	curCmd := cmd.NewCmd(bin, args...)
    22  	_ = curCmd.Start()
    23  
    24  	// to stream outputs
    25  	ticker := time.NewTicker(10 * time.Millisecond)
    26  	go func() {
    27  		prevLine := ""
    28  		for range ticker.C {
    29  			status := curCmd.Status()
    30  			n := len(status.Stdout)
    31  			if n == 0 {
    32  				continue
    33  			}
    34  
    35  			line := status.Stdout[n-1]
    36  			if prevLine != line && line != "" {
    37  				fmt.Println("[streaming output]", line)
    38  			}
    39  
    40  			prevLine = line
    41  		}
    42  	}()
    43  
    44  	return curCmd, nil
    45  }