-
Notifications
You must be signed in to change notification settings - Fork 224
/
cmdcontext.go
235 lines (200 loc) · 5.47 KB
/
cmdcontext.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cmdctx
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/segmentio/textio"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/iostreams"
)
// CmdContext - context passed to commands being run
type CmdContext struct {
IO *iostreams.IOStreams
Client *client.Client
Config flyctl.Config
GlobalConfig flyctl.Config
NS string
Args []string
Command *cobra.Command
Out io.Writer
WorkingDir string
ConfigFile string
AppName string
AppConfig *flyctl.AppConfig
MachineConfig *api.MachineConfig
}
// PresenterOption - options for RenderEx, RenderView, render etc...
type PresenterOption struct {
Presentable presenters.Presentable
AsJSON bool
Vertical bool
HideHeader bool
Title string
}
// Thoughts on how we could illuminate output in flyctl. I'm thinking a set of tags - INFO (plain text),
// DETAIL, TITLE (Bold Plain Text), BEGIN (Green bold with arrow), DONE (Blue with arrow), ERROR (red bold)...
const (
SINFO = "info"
SWARN = "warning"
SDETAIL = "detail"
STITLE = "title"
SBEGIN = "begin"
SDONE = "done"
SERROR = "error"
)
func NewCmdContext(flyctlClient *client.Client, ns string, cmd *cobra.Command, args []string) (*CmdContext, error) {
ctx := &CmdContext{
IO: flyctlClient.IO,
Client: flyctlClient,
NS: ns,
Config: flyctl.ConfigNS(ns),
GlobalConfig: flyctl.FlyConfig,
Args: args,
Command: cmd,
Out: flyctlClient.IO.Out,
}
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "Error resolving working directory")
}
ctx.WorkingDir = cwd
return ctx, nil
}
// Render - Render a presentable structure via the context
func (commandContext *CmdContext) Render(presentable presenters.Presentable) error {
presenter := &presenters.Presenter{
Item: presentable,
Out: os.Stdout,
Opts: presenters.Options{
AsJSON: commandContext.OutputJSON(),
},
}
return presenter.Render()
}
func (commandContext *CmdContext) render(out io.Writer, views ...PresenterOption) error {
for _, v := range views {
presenter := &presenters.Presenter{
Item: v.Presentable,
Out: out,
Opts: presenters.Options{
Vertical: v.Vertical,
HideHeader: v.HideHeader,
Title: v.Title,
AsJSON: v.AsJSON,
},
}
if err := presenter.Render(); err != nil {
return err
}
}
return nil
}
// Frender - render a view to a Writer
func (commandContext *CmdContext) Frender(views ...PresenterOption) error {
// If JSON output wanted, set in all views
if commandContext.OutputJSON() {
for i := range views {
views[i].AsJSON = true
}
}
return commandContext.render(commandContext.IO.Out, views...)
}
// FrenderPrefix - render a view to a Writer
func (commandContext *CmdContext) FrenderPrefix(prefix string, views ...PresenterOption) error {
// If JSON output wanted, set in all views
p := textio.NewPrefixWriter(commandContext.IO.Out, " ")
if commandContext.OutputJSON() {
for i := range views {
views[i].AsJSON = true
}
}
return commandContext.render(p, views...)
}
type JSON struct {
TS string
Source string
Status string
Message string
}
func (commandContext *CmdContext) StatusLn() {
outputJSON := commandContext.OutputJSON()
if outputJSON {
// Do nothing for JSON
return
}
fmt.Fprintln(commandContext.IO.Out)
}
func (commandContext *CmdContext) Status(source string, status string, args ...interface{}) {
outputJSON := commandContext.OutputJSON()
var message strings.Builder
for i, v := range args {
message.WriteString(fmt.Sprintf("%s", v))
if i < len(args)-1 {
message.WriteString(" ")
}
}
if outputJSON {
outstruct := JSON{
TS: time.Now().Format(time.RFC3339),
Source: source,
Status: status,
Message: message.String(),
}
outbuf, _ := json.Marshal(outstruct)
fmt.Fprintln(commandContext.IO.Out, string(outbuf))
return
} else {
fmt.Fprintln(commandContext.IO.Out, statusToEffect(status, message.String()))
}
}
func statusToEffect(status string, message string) string {
switch status {
case SINFO:
return message
case SWARN:
return aurora.Yellow(message).String()
case SDETAIL:
return aurora.Faint(message).String()
case STITLE:
return aurora.Bold(message).String()
case SBEGIN:
return aurora.Green("==> " + message).String()
case SDONE:
return aurora.Gray(20, "--> "+message).String()
case SERROR:
return aurora.Red("***" + message).String()
}
return message
}
func (commandContext *CmdContext) Statusf(source string, status string, format string, args ...interface{}) {
outputJSON := commandContext.OutputJSON()
message := fmt.Sprintf(format, args...)
if outputJSON {
outbuf, _ := json.Marshal(JSON{
TS: time.Now().Format(time.RFC3339),
Source: source,
Status: status,
Message: message,
})
fmt.Fprintln(commandContext.IO.Out, string(outbuf))
return
} else {
fmt.Fprint(commandContext.IO.Out, statusToEffect(status, message))
}
}
func (commandContext *CmdContext) WriteJSON(myData interface{}) {
outBuf, _ := json.MarshalIndent(myData, "", " ")
fmt.Fprintln(commandContext.IO.Out, string(outBuf))
}
func (commandContext *CmdContext) OutputJSON() bool {
return commandContext.GlobalConfig.GetBool(flyctl.ConfigJSONOutput)
}