github.com/codingeasygo/util@v0.0.0-20231206062002-1ce2f004b7d9/xos/exec.go (about)

     1  package xos
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"runtime"
     7  )
     8  
     9  // RunCommand will exec command and get text from stdout
    10  func RunCommand(command string) (text string, err error) {
    11  	var bys []byte
    12  	switch runtime.GOOS {
    13  	case "windows":
    14  		bys, err = exec.Command("cmd", "/C", command).Output()
    15  	default:
    16  		bys, err = exec.Command("bash", "-c", command).Output()
    17  	}
    18  	text = string(bys)
    19  	return
    20  }
    21  
    22  // Run will exec command and get text from stdout
    23  func Run(foramt string, args ...interface{}) (text string, err error) {
    24  	return RunCommand(fmt.Sprintf(foramt, args...))
    25  }