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

commands: Cleanup Requests after command execution returns

parent 8c9ee52a
...@@ -97,6 +97,14 @@ func (c *Command) Call(req Request) Response { ...@@ -97,6 +97,14 @@ func (c *Command) Call(req Request) Response {
return res 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) res.SetOutput(output)
return res return res
} }
......
...@@ -3,6 +3,7 @@ package commands ...@@ -3,6 +3,7 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"reflect" "reflect"
"strconv" "strconv"
...@@ -28,6 +29,7 @@ type Request interface { ...@@ -28,6 +29,7 @@ type Request interface {
Context() *Context Context() *Context
SetContext(Context) SetContext(Context)
Command() *Command Command() *Command
Cleanup() error
ConvertOptions() error ConvertOptions() error
} }
...@@ -119,6 +121,20 @@ func (r *request) Command() *Command { ...@@ -119,6 +121,20 @@ func (r *request) Command() *Command {
return r.cmd 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) type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{ 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