github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/nwo/command.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package nwo
     8  
     9  import (
    10  	"os"
    11  	"os/exec"
    12  )
    13  
    14  type Command interface {
    15  	Args() []string
    16  	SessionName() string
    17  }
    18  
    19  type Enver interface {
    20  	Env() []string
    21  }
    22  
    23  type WorkingDirer interface {
    24  	WorkingDir() string
    25  }
    26  
    27  func NewCommand(path string, command Command) *exec.Cmd {
    28  	cmd := exec.Command(path, command.Args()...)
    29  	cmd.Env = os.Environ()
    30  	if ce, ok := command.(Enver); ok {
    31  		cmd.Env = append(cmd.Env, ce.Env()...)
    32  	}
    33  	if wd, ok := command.(WorkingDirer); ok {
    34  		cmd.Dir = wd.WorkingDir()
    35  	}
    36  	return cmd
    37  }