-
Notifications
You must be signed in to change notification settings - Fork 221
/
cmdcontext.go
214 lines (179 loc) · 5.11 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
package cmdctx
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/segmentio/textio"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/terminal"
)
// CmdContext - context passed to commands being run
type CmdContext struct {
Client *client.Client
Config flyctl.Config
GlobalConfig flyctl.Config
NS string
Args []string
Out io.Writer
Terminal *terminal.Terminal
WorkingDir string
ConfigFile string
AppName string
AppConfig *flyctl.AppConfig
}
// 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"
const SDETAIL = "detail"
const STITLE = "title"
const SBEGIN = "begin"
const SDONE = "done"
const SERROR = "error"
func NewCmdContext(flyctlClient *client.Client, ns string, out io.Writer, args []string) (*CmdContext, error) {
ctx := &CmdContext{
Client: flyctlClient,
NS: ns,
Config: flyctl.ConfigNS(ns),
GlobalConfig: flyctl.FlyConfig,
Out: out,
Args: args,
Terminal: terminal.NewTerminal(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.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.Out, " ")
if commandContext.OutputJSON() {
for i := range views {
views[i].AsJSON = true
}
}
return commandContext.render(p, views...)
}
type JSON struct {
Source string
Status string
Message string
}
func (commandContext *CmdContext) StatusLn() {
outputJSON := commandContext.OutputJSON()
if outputJSON {
// Do nothing for JSON
return
}
fmt.Fprintln(commandContext.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{Source: source, Status: status, Message: message.String()}
outbuf, _ := json.Marshal(outstruct)
fmt.Fprintln(commandContext.Out, string(outbuf))
return
} else {
fmt.Fprintln(commandContext.Out, statusToEffect(status, message.String()))
}
}
func statusToEffect(status string, message string) string {
switch status {
case SINFO:
return message
case SDETAIL:
return aurora.Faint(message).String()
case STITLE:
return aurora.Bold(message).Black().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{Source: source, Status: status, Message: message})
fmt.Fprintln(commandContext.Out, string(outbuf))
return
} else {
fmt.Fprint(commandContext.Out, statusToEffect(status, message))
}
}
func (commandContext *CmdContext) WriteJSON(myData interface{}) error {
outBuf, _ := json.MarshalIndent(myData, "", " ")
fmt.Fprintln(commandContext.Out, string(outBuf))
return nil
}
func (commandContext *CmdContext) OutputJSON() bool {
return commandContext.GlobalConfig.GetBool(flyctl.ConfigJSONOutput)
}