mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
commands: Cleanup Requests after command execution returns
This commit is contained in:

committed by
Juan Batiz-Benet

parent
8c9ee52a93
commit
81dbb23602
@ -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
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -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 {
|
|||||||
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{
|
||||||
|
Reference in New Issue
Block a user