forked from iximiuz/labctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliutil.go
182 lines (139 loc) · 3.41 KB
/
cliutil.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
package labcli
import (
"fmt"
"io"
"strings"
"github.com/charmbracelet/huh"
"github.com/docker/cli/cli/streams"
"github.com/iximiuz/labctl/internal/api"
"github.com/iximiuz/labctl/internal/config"
"github.com/sirupsen/logrus"
)
type Streams interface {
InputStream() *streams.In
OutputStream() *streams.Out
AuxStream() *streams.Out // ErrorStream unless quiet else io.Discard
ErrorStream() io.Writer
}
type CLI interface {
Streams
SetQuiet(bool)
// Regular print to stdout.
PrintOut(string, ...any)
// Regular print to stderr.
PrintErr(string, ...any)
// Print to stderr unless quiet else - discard.
PrintAux(string, ...any)
SetConfig(*config.Config)
Config() *config.Config
SetClient(*api.Client)
Client() *api.Client
Confirm(title, affirmative, negative string) bool
Input(title, prompt string, value *string, validate func(string) error) error
Version() string
}
type cli struct {
inputStream *streams.In
outputStream *streams.Out
auxStream *streams.Out
errorStream io.Writer
config *config.Config
client *api.Client
version string
}
var _ CLI = &cli{}
func NewCLI(cin io.ReadCloser, cout io.Writer, cerr io.Writer, version string) CLI {
return &cli{
inputStream: streams.NewIn(cin),
outputStream: streams.NewOut(cout),
auxStream: streams.NewOut(cerr),
errorStream: cerr,
version: version,
}
}
func (c *cli) SetConfig(cfg *config.Config) {
c.config = cfg
}
func (c *cli) Config() *config.Config {
return c.config
}
func (c *cli) SetClient(client *api.Client) {
c.client = client
}
func (c *cli) Client() *api.Client {
return c.client
}
func (c *cli) InputStream() *streams.In {
return c.inputStream
}
func (c *cli) OutputStream() *streams.Out {
return c.outputStream
}
func (c *cli) AuxStream() *streams.Out {
return c.auxStream
}
func (c *cli) ErrorStream() io.Writer {
return c.errorStream
}
func (c *cli) SetQuiet(v bool) {
if v {
c.auxStream = streams.NewOut(io.Discard)
} else {
c.auxStream = streams.NewOut(c.errorStream)
}
}
func (c *cli) PrintOut(format string, a ...any) {
fmt.Fprintf(c.OutputStream(), format, a...)
}
func (c *cli) PrintErr(format string, a ...any) {
fmt.Fprintf(c.ErrorStream(), format, a...)
}
func (c *cli) PrintAux(format string, a ...any) {
fmt.Fprintf(c.AuxStream(), format, a...)
}
func (c *cli) Confirm(title, affirmative, negative string) bool {
var confirm bool
if err := huh.NewConfirm().Title(title).Affirmative(affirmative).Negative(negative).Value(&confirm).Run(); err != nil {
logrus.WithError(err).Warn("Confirmation prompt failed")
return false
}
return confirm
}
func (c *cli) Input(
title string,
prompt string,
value *string,
validate func(string) error,
) error {
return huh.NewInput().Title(title).Prompt(prompt).Validate(validate).Value(value).Run()
}
func (c *cli) Version() string {
return c.version
}
type StatusError struct {
status string
code int
}
var _ error = StatusError{}
func NewStatusError(code int, format string, a ...any) StatusError {
status := fmt.Sprintf(format, a...)
if !strings.HasSuffix(status, ".") && !strings.HasSuffix(status, "!") {
status += "."
}
return StatusError{
code: code,
status: strings.ToUpper(status[:1]) + status[1:],
}
}
func WrapStatusError(err error) error {
if err == nil {
return nil
}
return NewStatusError(1, err.Error())
}
func (e StatusError) Error() string {
return e.status
}
func (e StatusError) Code() int {
return e.code
}