-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgotic.go
115 lines (90 loc) · 2.65 KB
/
gotic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gotic
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
)
/*
Gotic package
Provides a proper interface to execute shell command lines.
License: MIT
*/
// the dependencies below are declared as package vars to enable dummies in unit tests
// BuildCommand builds up a exec.Command.CombinedOutput
var (
BuildCommand = buildCommand
logFatal = log.Fatal
ReadPrompt = readPrompt
)
func readPrompt(stdin io.Reader) (text string) {
reader := bufio.NewReader(stdin)
text, _ = reader.ReadString('\n')
return text
}
func buildCommand(command string) ([]byte, error) {
cmd := exec.Command("bash", "-c", command)
return cmd.CombinedOutput()
}
// ExecBashPipedCommand executes a simple command or a piped bash command
func ExecBashPipedCommand(command string, showOutput bool) (string, error) {
out, err := BuildCommand(command)
count := strings.Count(string(out), "\n")
if count == 1 {
out = []byte(strings.ReplaceAll(string(out), "\n", ""))
}
if out != nil && showOutput {
log.Printf("Done executing command: %s", command)
log.Printf("Output:%s", string(out))
}
if err != nil {
s := fmt.Sprintf("ExecBashPipedCommand failed with %s", err)
logFatal(s)
}
return string(out), err
}
// ExecBashPipedCommandIgnoreExitCode executes a simple command or a piped bash command
func ExecBashPipedCommandIgnoreExitCode(command string, showOutput bool) (string, error) {
out, err := BuildCommand(command)
count := strings.Count(string(out), "\n")
if count == 1 {
out = []byte(strings.ReplaceAll(string(out), "\n", ""))
}
if out != nil && showOutput {
log.Printf("Done executing command: %s", command)
log.Printf("Output:%s", string(out))
}
if err != nil && showOutput {
log.Printf("ExecBashPipedCommand failed with %v\n", err)
}
return string(out), err
}
// ExecShellScript executes a shell script file
func ExecShellScript(shFilePath string, showOutput bool) (string, string) {
out, err := BuildCommand(shFilePath)
if err != nil {
logFatal("ExecShellScript failed with %s\n", err)
}
if out != nil {
fmt.Printf("Done executing shellscript file %s\n", shFilePath)
fmt.Printf("Out \n%s\n", string(out))
}
return string(out), fmt.Sprintf("%s", err)
}
// Prompt asks to execute a commands
func Prompt(question, command string, showOutput bool) bool {
fmt.Printf("==> %s '%s'? (y/n)\n", question, command)
text := ReadPrompt(os.Stdin)
fmt.Println(text)
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
if strings.Compare("Y", text) == 0 || strings.Compare("y", text) == 0 {
_, _ = ExecBashPipedCommand(command, showOutput)
log.Printf("Command '%s' finished\n", command)
return true
}
return false
}