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