-
Notifications
You must be signed in to change notification settings - Fork 204
/
utils.go
100 lines (84 loc) · 1.94 KB
/
utils.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
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"github.com/SpectoLabs/hoverfly/hoverctl/configuration"
"github.com/olekukonko/tablewriter"
"golang.org/x/crypto/ssh/terminal"
)
func handleIfError(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func checkArgAndExit(args []string, message, command string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, message)
fmt.Fprintln(os.Stderr, "\nTry hoverctl "+command+" --help for more information")
os.Exit(1)
}
}
func checkTargetAndExit(target *configuration.Target) {
if target == nil {
handleIfError(fmt.Errorf("%[1]s is not a target\n\nRun `hoverctl targets create %[1]s`", targetNameFlag))
}
}
func askForConfirmation(message string) bool {
if force {
return true
}
for {
response := askForInput(message+" [y/n]", false)
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}
func askForInput(value string, sensitive bool) string {
if force {
return ""
}
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf(value + ": ")
if sensitive {
responseBytes, err := terminal.ReadPassword(int(syscall.Stdin))
handleIfError(err)
fmt.Println("")
return strings.TrimSpace(string(responseBytes))
} else {
response, err := reader.ReadString('\n')
handleIfError(err)
return strings.TrimSpace(response)
}
}
}
func drawTable(data [][]string, header bool) {
table := tablewriter.NewWriter(os.Stdout)
if header {
table.SetHeader(data[0])
data = data[1:]
}
for _, v := range data {
table.Append(v)
}
fmt.Print("\n")
table.Render()
}
func getContentShorthand(content string) string {
if content != "" {
scriptArr := strings.Split(content, "\n")
if verbose || len(scriptArr) < 5 {
return content
} else {
return fmt.Sprintln(scriptArr[0], scriptArr[1], scriptArr[2], scriptArr[3], scriptArr[4], "...")
}
}
return ""
}