Commit d574d03a authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands: Cleanup Requests after command execution returns

parent f724440c
......@@ -97,6 +97,14 @@ func (c *Command) Call(req Request) Response {
return res
}
// clean up the request (close the readers, e.g. fileargs)
// NOTE: this means commands can't expect to keep reading after cmd.Run returns (in a goroutine)
err = req.Cleanup()
if err != nil {
res.SetError(err, ErrNormal)
return res
}
res.SetOutput(output)
return res
}
......
......@@ -3,6 +3,7 @@ package commands
import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
......@@ -28,6 +29,7 @@ type Request interface {
Context() *Context
SetContext(Context)
Command() *Command
Cleanup() error
ConvertOptions() error
}
......@@ -119,6 +121,20 @@ func (r *request) Command() *Command {
return r.cmd
}
func (r *request) Cleanup() error {
for _, arg := range r.arguments {
closer, ok := arg.(io.Closer)
if ok {
err := closer.Close()
if err != nil {
return err
}
}
}
return nil
}
type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment