-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
206 lines (193 loc) · 5.08 KB
/
prompt.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const ANSI_ESCAPE_CLEAR = "\033[H\033[2J"
type PromptSecrets struct {
secrets []PromptSecretInput
}
type PromptSecretInput struct {
key string
kind PromptSecretInput_Kind
value string
valueFromFile string
}
type PromptSecretInput_Kind string
const PromptSecretInput_Kind_File PromptSecretInput_Kind = "file"
const PromptSecretInput_Kind_String PromptSecretInput_Kind = "string"
const PromptSecretInput_Kind_None PromptSecretInput_Kind = "none"
func (s *PromptSecrets) InitKey(key string) {
if s.secrets == nil {
s.secrets = []PromptSecretInput{}
}
s.secrets = append(s.secrets, PromptSecretInput{
key: key,
})
}
func (s PromptSecrets) ToValues() map[string]string {
values := map[string]string{}
for _, s := range s.secrets {
if s.kind == PromptSecretInput_Kind_None {
continue
}
if s.kind == PromptSecretInput_Kind_File {
values[s.key] = s.valueFromFile
} else {
values[s.key] = s.value
}
}
return values
}
func (s *PromptSecrets) Namespace(input io.Reader, output io.Writer) (namespace string, err error) {
fmt.Fprintf(
output,
"%s%s\n\nWhat namespace will this Sealed Secret be scoped to?:\n",
ANSI_ESCAPE_CLEAR,
strings.Repeat(`-`, 80),
)
reader := bufio.NewReader(input)
for {
fmt.Fprintf(output, "namespace=")
var line string
line, err = reader.ReadString('\n')
if err != nil {
return
}
line = strings.TrimSuffix(line, "\n")
if line == "" {
fmt.Fprintf(output, "WARNING: Invalid namespace values are ignored, please re-enter a namespace.\n")
continue
}
return line, nil
}
}
func (s *PromptSecrets) Enter(input io.Reader, output io.Writer) (err error) {
fmt.Fprintf(
output,
"%s%s\n\nEnter a key and value separated by =, leave blank and press enter when finished:\n",
ANSI_ESCAPE_CLEAR,
strings.Repeat(`-`, 80),
)
reader := bufio.NewReader(input)
i := 0
for {
var line string
line, err = reader.ReadString('\n')
if err != nil {
return
}
line = strings.TrimSuffix(line, "\n")
if line == "" {
break
}
lineSplit := strings.SplitN(line, "=", 2)
if len(lineSplit) != 2 || strings.TrimSpace(lineSplit[0]) == "" || strings.TrimSpace(lineSplit[1]) == "" {
fmt.Fprintf(output, "WARNING: Lines not containing key and value separated by '=' are ignored\n")
continue
}
key := strings.TrimSpace(lineSplit[0])
value := lineSplit[1]
s.secrets = append(s.secrets, PromptSecretInput{
key: key,
value: strings.TrimSpace(value),
})
valueFromFile, readFileErr := os.ReadFile(value)
if value == "" {
s.secrets[i].kind = PromptSecretInput_Kind_None
} else if readFileErr == nil {
s.secrets[i].kind = PromptSecretInput_Kind_File
s.secrets[i].valueFromFile = string(valueFromFile)
} else {
s.secrets[i].kind = PromptSecretInput_Kind_String
}
i++
}
return
}
func (s *PromptSecrets) Update(redo int, input io.Reader, output io.Writer) (err error) {
fmt.Fprintf(
output,
"%s%s\n\nPlease enter your secrets for each key then press enter:\n",
ANSI_ESCAPE_CLEAR,
strings.Repeat(`-`, 80),
)
reader := bufio.NewReader(input)
for i, secret := range s.secrets {
if redo > 0 && redo != (i+1) {
continue
}
fmt.Fprintf(output, "%s=", secret.key)
var value string
value, err = reader.ReadString('\n')
if err != nil {
return
}
value = strings.TrimSuffix(value, "\n")
s.secrets[i] = PromptSecretInput{
key: secret.key,
value: strings.TrimSpace(value),
}
valueFromFile, readFileErr := os.ReadFile(value)
if value == "" {
s.secrets[i].kind = PromptSecretInput_Kind_None
} else if readFileErr == nil {
s.secrets[i].kind = PromptSecretInput_Kind_File
s.secrets[i].valueFromFile = string(valueFromFile)
} else {
s.secrets[i].kind = PromptSecretInput_Kind_String
}
}
return
}
func (s *PromptSecrets) Confirm(input io.Reader, output io.Writer) (redo int, err error) {
reader := bufio.NewReader(input)
fmt.Fprintf(
output,
"%s%s\n\nPlease review each secret is correct:\n",
ANSI_ESCAPE_CLEAR,
strings.Repeat(`-`, 80),
)
for i, s := range s.secrets {
switch s.kind {
case PromptSecretInput_Kind_File:
fmt.Fprintf(output, "%d. %s will contain the contents of file %s\n", i+1, s.key, s.value)
case PromptSecretInput_Kind_None:
fmt.Fprintf(output, "%d. %s will remain unchanged\n", i+1, s.key)
case PromptSecretInput_Kind_String:
fallthrough
default:
fmt.Fprintf(output, "%d. %s=%s\n", i+1, s.key, s.value)
}
}
for {
numString := fmt.Sprintf("1-%d", len(s.secrets))
if numString == "1-1" {
numString = "1"
}
fmt.Fprintf(output, "\nEnter the secret number to change the value, or Y to confirm\n%s or Y: ", numString)
in, err := reader.ReadString('\n')
if err != nil {
break
}
in = strings.TrimSpace(in)
if in == "Y" {
break
}
redo, err = strconv.Atoi(in)
if err == nil && redo >= 1 && redo <= len(s.secrets) {
break
}
redo = 0
fmt.Fprintf(output, "ERROR: Input invalid, please retry.\n")
}
fmt.Fprint(output, ANSI_ESCAPE_CLEAR)
return
}
func PromptClear(output io.Writer) {
fmt.Fprint(output, ANSI_ESCAPE_CLEAR)
}