Saturday, May 30, 2020

golang: execCommand example

// execCommand executes a command from cmdName with args
// provided from cmdArgs. Returns the stdout in []byte and error or nil
func execCommand(cmdName string, cmdArgs ...string) ([]byte, error) {
        logrus.Debugf("Executing: %s %s ", cmdName, cmdArgs)
        cmd := exec.Command(cmdName, cmdArgs...)
        stdout, err := cmd.Output()
        if err != nil {
                return nil, errors.Wrapf(err, "Error executing the command")

        return stdout, nil

No comments:

Post a Comment