github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/utils/system.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"os/exec"
    24  	"strings"
    25  )
    26  
    27  // RunCommandWithSudo runs a command with sudo permissions
    28  func RunCommandWithSudo(cmd string) error {
    29  	return RunCommand("sudo " + cmd)
    30  }
    31  
    32  // ExecCommandWithSudo runs a command with sudo permissions and
    33  // return the output as a string
    34  func ExecCommandWithSudo(cmd string) (string, error) {
    35  	return ExecCommand("sudo " + cmd)
    36  }
    37  
    38  // RunCommand runs a command on the host
    39  func RunCommand(cmd string) error {
    40  	substring := strings.Fields(cmd)
    41  	name := substring[0]
    42  	args := substring[1:]
    43  	err := exec.Command(name, args...).Run() // #nosec G204
    44  	if err != nil {
    45  		return fmt.Errorf("run failed %s %v", cmd, err)
    46  	}
    47  	return err
    48  }
    49  
    50  // ExecCommand runs a command on the host and get the output
    51  func ExecCommand(cmd string) (string, error) {
    52  	substring := strings.Fields(cmd)
    53  	name := substring[0]
    54  	args := substring[1:]
    55  	out, err := exec.Command(name, args...).CombinedOutput() // #nosec G204
    56  	if err != nil {
    57  		return "", fmt.Errorf("exec failed %s %v", cmd, err)
    58  	}
    59  	return string(out), err
    60  }
    61  
    62  // ExecCommandWithPipe runs 2 commands, pipe the output of first command to second,
    63  // and returns output from the second
    64  func ExecCommandWithPipe(cmd1, cmd2 string) (string, error) {
    65  	parts1 := strings.Fields(cmd1)
    66  	parts2 := strings.Fields(cmd2)
    67  
    68  	c1 := exec.Command(parts1[0], parts1[1:]...) // #nosec G204
    69  	c2 := exec.Command(parts2[0], parts2[1:]...) // #nosec G204
    70  
    71  	reader, writer := io.Pipe()
    72  	c1.Stdout = writer
    73  	c2.Stdin = reader
    74  
    75  	var buffer bytes.Buffer
    76  	c2.Stdout = &buffer
    77  
    78  	err := c1.Start()
    79  	if err != nil {
    80  		return "", fmt.Errorf("error starting command: %q. Error: %v", cmd1, err)
    81  	}
    82  	err = c2.Start()
    83  	if err != nil {
    84  		return "", fmt.Errorf("error starting command: %q. Error: %v", cmd2, err)
    85  	}
    86  	err = c1.Wait()
    87  	if err != nil {
    88  		return "", fmt.Errorf("error while waiting for command: %q to exit. Error: %v", cmd1, err)
    89  	}
    90  	err = writer.Close()
    91  	if err != nil {
    92  		return "", fmt.Errorf("error while closing the pipe writer. Error: %v", err)
    93  	}
    94  	err = c2.Wait()
    95  	if err != nil {
    96  		return "", fmt.Errorf("error while waiting for command: %q to exit. Error: %v", cmd2, err)
    97  	}
    98  
    99  	return buffer.String(), nil
   100  }