execCommand executres the named program with the provided arguments, returning its output
(name string, arg ...string)
| 839 | |
| 840 | // execCommand executres the named program with the provided arguments, returning its output |
| 841 | func execCommand(name string, arg ...string) ([]byte, error) { |
| 842 | // Create a new command to execute. |
| 843 | cmd := exec.Command(name, arg...) |
| 844 | |
| 845 | // Initialize a buffer to capture the command output. |
| 846 | var buf bytes.Buffer |
| 847 | |
| 848 | // Redirect both standard output and standard error to the buffer. |
| 849 | cmd.Stdout = &buf |
| 850 | cmd.Stderr = &buf |
| 851 | |
| 852 | // Start the command execution. |
| 853 | if err := cmd.Start(); err != nil { |
| 854 | return buf.Bytes(), err |
| 855 | } |
| 856 | |
| 857 | // Wait for the command to finish executing. |
| 858 | if err := cmd.Wait(); err != nil { |
| 859 | return buf.Bytes(), err |
| 860 | } |
| 861 | |
| 862 | // Return the captured output and a nil error, indicating successful execution. |
| 863 | return buf.Bytes(), nil |
| 864 | } |