This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
89 lines (70 loc) · 1.46 KB
/
types.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
package confirm
import (
"bufio"
"fmt"
"github.com/fatih/color"
"io"
"io/ioutil"
"os"
"strings"
)
type Confirmation struct {
Prompt string
Title string
Message string
Body interface{}
Color *color.Color
}
func (c *Confirmation) title() string {
if c.Title == "" {
return strings.ToUpper(DefaultTitle)
}
return strings.ToUpper(c.Title)
}
func (c *Confirmation) DoPrompt(out io.Writer) (bool, error) {
msg := c.Message
col := c.Color
if msg == "" {
msg = "This action is destructive!"
}
if col == nil {
col = color.New(color.FgRed)
}
if c.Prompt != "" {
fmt.Fprintln(color.Output, c.Prompt)
}
msgLines := strings.Split(msg, "\n")
col.Fprintln(out, c.title())
for _, l := range msgLines {
col.Fprintln(out, " "+l)
}
col.Fprintln(out, " Are you ABSOLUTELY sure you want to continue?")
fmt.Println("")
switch b := c.Body.(type) {
case string:
fmt.Println(b)
fmt.Println("")
case io.Reader:
s, err := ioutil.ReadAll(b)
if err != nil {
return false, err
}
fmt.Println(strings.TrimSpace(string(s)))
fmt.Println("")
case nil:
break
default:
return false, fmt.Errorf("unknown body type: %T", b)
}
for {
fmt.Fprint(color.Output, "Enter "+color.YellowString("y")+" or "+color.YellowString("n")+": ")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimRight(input, "\n")
if input == "y" {
return true, nil
} else if input == "n" {
return false, nil
}
}
}