-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI and API command output encoding #215
base: master
Are you sure you want to change the base?
Changes from 8 commits
957684f
16f14e9
11083f0
9bff5f3
ed1225f
f47dce6
d3455df
bce8c65
11a3754
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,21 @@ var _ ResponseEmitter = &responseEmitter{} | |
// NewResponseEmitter constructs a new response emitter that writes results to | ||
// the console. | ||
func NewResponseEmitter(stdout, stderr io.Writer, req *cmds.Request) (ResponseEmitter, error) { | ||
|
||
if req.Command != nil && req.Command.DisplayCLI != nil && cmds.GetEncoding(req, "json") == "text" { | ||
re, res := cmds.NewChanResponsePair(req) | ||
dre := &displayResponseEmitter{re: re} | ||
|
||
go func() { | ||
err := req.Command.DisplayCLI(res, os.Stdout, os.Stderr) | ||
if err != nil { | ||
dre.CloseWithError(err) | ||
} | ||
}() | ||
|
||
return dre, nil | ||
} | ||
|
||
encType, enc, err := cmds.GetEncoder(req, stdout, cmds.TextNewline) | ||
|
||
return &responseEmitter{ | ||
|
@@ -25,6 +40,54 @@ func NewResponseEmitter(stdout, stderr io.Writer, req *cmds.Request) (ResponseEm | |
}, err | ||
} | ||
|
||
// displayResponseEmitter implements cli.ResponseEmitter, for | ||
// delegating to a cmd.ResponseEmitter instance when | ||
// Command.DisplayCLI is defined | ||
type displayResponseEmitter struct { | ||
l sync.Mutex | ||
stdout io.Writer | ||
stderr io.Writer | ||
|
||
re cmds.ResponseEmitter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can embed this instead of manually forwarding. |
||
exit int | ||
} | ||
|
||
func (dre *displayResponseEmitter) Close() error { | ||
return dre.re.Close() | ||
} | ||
|
||
func (dre *displayResponseEmitter) CloseWithError(err error) error { | ||
return dre.re.CloseWithError(err) | ||
} | ||
|
||
func (dre *displayResponseEmitter) Emit(v interface{}) error { | ||
return dre.re.Emit(v) | ||
} | ||
|
||
func (dre *displayResponseEmitter) SetLength(l uint64) { | ||
dre.re.SetLength(l) | ||
} | ||
|
||
func (dre *displayResponseEmitter) SetStatus(code int) { | ||
dre.l.Lock() | ||
defer dre.l.Unlock() | ||
dre.exit = code | ||
} | ||
|
||
func (dre *displayResponseEmitter) Status() int { | ||
dre.l.Lock() | ||
defer dre.l.Unlock() | ||
return dre.exit | ||
} | ||
|
||
func (dre *displayResponseEmitter) Stderr() io.Writer { | ||
return dre.stderr | ||
} | ||
|
||
func (dre *displayResponseEmitter) Stdout() io.Writer { | ||
return dre.stdout | ||
} | ||
|
||
Comment on lines
+67
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can actually just drop these methods from the interface. But it'll require some investigation. |
||
// ResponseEmitter extends cmds.ResponseEmitter to give better control over the command line | ||
type ResponseEmitter interface { | ||
cmds.ResponseEmitter | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. This isn't going to work:
dre
viaCloseWithError
.DisplayCLI
will get this error fromres
, and return it.dre
here.Really, we need to expose this error via some method and/or a channel. I'll try to think of the right approach.