/* Copyright 2020 Huawei Technologies Co., Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // runCommandWithShell executes the given command with "sh -c". // It returns an error if the command outputs anything on the stderr. package setup import ( "bytes" "fmt" "io" "os" "os/exec" "strings" "sync" ) //Command defines commands to be executed and captures std out and std error type Command struct { Cmd *exec.Cmd StdOut []byte StdErr []byte } func (cm Command) GetStdOutput() string { if len(cm.StdOut) != 0 { return strings.TrimRight(string(cm.StdOut), "\n") } return "" } //GetStdErr gets StdErr field func (cm Command) GetStdErr() string { if len(cm.StdErr) != 0 { return strings.TrimRight(string(cm.StdErr), "\n") } return "" } //It helps in the commands where it takes some time for execution. func (cm Command) ExecuteCmdShowOutput() error { var stdoutBuf, stderrBuf bytes.Buffer stdoutIn, _ := cm.Cmd.StdoutPipe() stderrIn, _ := cm.Cmd.StderrPipe() var errStdout, errStderr error stdout := io.MultiWriter(os.Stdout, &stdoutBuf) stderr := io.MultiWriter(os.Stderr, &stderrBuf) err := cm.Cmd.Start() if err != nil { return fmt.Errorf("failed to start '%s' because of error : %s", strings.Join(cm.Cmd.Args, " "), err.Error()) } var wg sync.WaitGroup wg.Add(1) go func() { _, errStdout = io.Copy(stdout, stdoutIn) wg.Done() }() _, errStderr = io.Copy(stderr, stderrIn) wg.Wait() err = cm.Cmd.Wait() if err != nil { return fmt.Errorf("failed to run '%s' because of error : %s", strings.Join(cm.Cmd.Args, " "), err.Error()) } if errStdout != nil || errStderr != nil { return fmt.Errorf("failed to capture stdout or stderr") } cm.StdOut, cm.StdErr = stdoutBuf.Bytes(), stderrBuf.Bytes() return nil } func runCommandAtShell(command string) (string, error) { cmd := &Command{Cmd: exec.Command("sh", "-c", command)} err := cmd.ExecuteCmdShowOutput() if err != nil { return "", err } errout := cmd.GetStdErr() if errout != "" { return "", fmt.Errorf("%s", errout) } return cmd.GetStdOutput(), nil }