@cryptotaxi247 / kubo / commits / 81dbb2360

commands: Cleanup Requests after command execution returns

Matt Bell committed Nov 12, 2014 at 00:05 UTC 81dbb236025af553f61488542349158b70466f6f
2 files changed +24
commands/command.go
+8
@@ -97,6 +97,14 @@ func (c *Command) Call(req Request) Response {
97 return res
98 }
99
100 + // clean up the request (close the readers, e.g. fileargs)
101 + // NOTE: this means commands can't expect to keep reading after cmd.Run returns (in a goroutine)
102 + err = req.Cleanup()
103 + if err != nil {
104 + res.SetError(err, ErrNormal)
105 + return res
106 + }
107 +
108 res.SetOutput(output)
109 return res
110 }
commands/request.go
+16
@@ -3,6 +3,7 @@ package commands
3 import (
4 "errors"
5 "fmt"
6 + "io"
7 "reflect"
8 "strconv"
9
@@ -28,6 +29,7 @@ type Request interface {
29 Context() *Context
30 SetContext(Context)
31 Command() *Command
32 + Cleanup() error
33
34 ConvertOptions() error
35 }
@@ -119,6 +121,20 @@ func (r *request) Command() *Command {
121 return r.cmd
122 }
123
124 +func (r *request) Cleanup() error {
125 + for _, arg := range r.arguments {
126 + closer, ok := arg.(io.Closer)
127 + if ok {
128 + err := closer.Close()
129 + if err != nil {
130 + return err
131 + }
132 + }
133 + }
134 +
135 + return nil
136 +}
137 +
138 type converter func(string) (interface{}, error)
139
140 var converters = map[reflect.Kind]converter{