make ExecSystem() verbosity togglable

This commit is contained in:
Late Night Defender 2024-05-16 14:43:17 +07:00
parent 0c0a50203f
commit d9bfb6ff01

76
main.go
View file

@ -4,6 +4,7 @@ import (
"fmt"
"os/exec"
"bufio"
"flag"
)
var mongoBasePath = "/opt/my-mongo"
@ -13,36 +14,62 @@ var numberOfContainers int = 5
var maxMemAllocPercent int = 95
var mongoInitdbRootUsername = "sasha"
var mongoInitdbRootPassword = "12345"
var verbosePtr = flag.Bool("v", false, "boolean")
func ExecSystem(command string) {
cmd := exec.Command("/usr/bin/bash", "-c", command)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error obtaining stdout pipe:", err)
return
}
// Start the command
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
if *verbosePtr{
cmd := exec.Command("/usr/bin/bash", "-c", command)
// Read the output line by line
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error obtaining stdout pipe:", err)
return
}
// Check for any errors
if err := scanner.Err(); err != nil {
fmt.Println("Error reading command output:", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error obtaining stderr pipe:", err)
return
}
// Wait for the command to finish
if err := cmd.Wait(); err != nil {
fmt.Println("Error waiting for command:", err)
}
// Start the command
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
// Read the output line by line
stdoutScanner := bufio.NewScanner(stdout)
stderrScanner := bufio.NewScanner(stderr)
for stdoutScanner.Scan() {
fmt.Println(stdoutScanner.Text())
}
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
} else {
cmd := exec.Command("/usr/bin/bash", "-c", command)
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error obtaining stderr pipe:", err)
return
}
// Start the command
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
// Read the output line by line
stderrScanner := bufio.NewScanner(stderr)
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
}
}
func genKeys() {
@ -55,6 +82,7 @@ func genKeys() {
}
func main() {
flag.Parse()
checkRoot()
checkSettings()
instDeps()