make ExecSystem() verbosity togglable
This commit is contained in:
parent
0c0a50203f
commit
d9bfb6ff01
1 changed files with 52 additions and 24 deletions
50
main.go
50
main.go
|
@ -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,15 +14,49 @@ 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) {
|
||||||
|
|
||||||
|
if *verbosePtr{
|
||||||
cmd := exec.Command("/usr/bin/bash", "-c", command)
|
cmd := exec.Command("/usr/bin/bash", "-c", command)
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error obtaining stdout pipe:", err)
|
fmt.Println("Error obtaining stdout pipe:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
// Start the command
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
fmt.Println("Error starting command:", err)
|
fmt.Println("Error starting command:", err)
|
||||||
|
@ -29,19 +64,11 @@ func ExecSystem(command string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the output line by line
|
// Read the output line by line
|
||||||
scanner := bufio.NewScanner(stdout)
|
stderrScanner := bufio.NewScanner(stderr)
|
||||||
for scanner.Scan() {
|
|
||||||
fmt.Println(scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for any errors
|
for stderrScanner.Scan() {
|
||||||
if err := scanner.Err(); err != nil {
|
fmt.Println(stderrScanner.Text())
|
||||||
fmt.Println("Error reading command output:", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the command to finish
|
|
||||||
if err := cmd.Wait(); err != nil {
|
|
||||||
fmt.Println("Error waiting for command:", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +82,7 @@ func genKeys() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
checkRoot()
|
checkRoot()
|
||||||
checkSettings()
|
checkSettings()
|
||||||
instDeps()
|
instDeps()
|
||||||
|
|
Loading…
Reference in a new issue