提交 8f24d8e2 作者: keks 提交者: Jeromy

cleanup

License: MIT
Signed-off-by: 's avatarkeks <keks@cryptoscope.co>
上级 7b196390
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
corehttp "github.com/ipfs/go-ipfs/core/corehttp" corehttp "github.com/ipfs/go-ipfs/core/corehttp"
corerepo "github.com/ipfs/go-ipfs/core/corerepo" corerepo "github.com/ipfs/go-ipfs/core/corerepo"
nodeMount "github.com/ipfs/go-ipfs/fuse/node" nodeMount "github.com/ipfs/go-ipfs/fuse/node"
//config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations" migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
...@@ -200,7 +199,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -200,7 +199,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
} }
} }
ctx := env.(*oldcmds.Context) cctx := env.(*oldcmds.Context)
go func() { go func() {
<-req.Context.Done() <-req.Context.Done()
...@@ -219,10 +218,9 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -219,10 +218,9 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
// first, whether user has provided the initialization flag. we may be // first, whether user has provided the initialization flag. we may be
// running in an uninitialized state. // running in an uninitialized state.
initialize, _ := req.Options[initOptionKwd].(bool) initialize, _ := req.Options[initOptionKwd].(bool)
if initialize { if initialize {
cfg := ctx.ConfigRoot cfg := cctx.ConfigRoot
if !fsrepo.IsInitialized(cfg) { if !fsrepo.IsInitialized(cfg) {
err := initWithDefaults(os.Stdout, cfg) err := initWithDefaults(os.Stdout, cfg)
if err != nil { if err != nil {
...@@ -234,7 +232,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -234,7 +232,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
// acquire the repo lock _before_ constructing a node. we need to make // acquire the repo lock _before_ constructing a node. we need to make
// sure we are permitted to access the resources (datastore, etc.) // sure we are permitted to access the resources (datastore, etc.)
repo, err := fsrepo.Open(ctx.ConfigRoot) repo, err := fsrepo.Open(cctx.ConfigRoot)
switch err { switch err {
default: default:
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
...@@ -264,7 +262,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -264,7 +262,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
return return
} }
repo, err = fsrepo.Open(ctx.ConfigRoot) repo, err = fsrepo.Open(cctx.ConfigRoot)
if err != nil { if err != nil {
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
return return
...@@ -273,7 +271,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -273,7 +271,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
break break
} }
cfg, err := ctx.GetConfig() cfg, err := cctx.GetConfig()
if err != nil { if err != nil {
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
return return
...@@ -340,12 +338,12 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -340,12 +338,12 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
} }
}() }()
ctx.ConstructNode = func() (*core.IpfsNode, error) { cctx.ConstructNode = func() (*core.IpfsNode, error) {
return node, nil return node, nil
} }
// construct api endpoint - every time // construct api endpoint - every time
err, apiErrc := serveHTTPApi(req, ctx) err, apiErrc := serveHTTPApi(req, cctx)
if err != nil { if err != nil {
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
return return
...@@ -359,7 +357,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -359,7 +357,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
return return
} }
if mount { if mount {
if err := mountFuse(req, ctx); err != nil { if err := mountFuse(req, cctx); err != nil {
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
return return
} }
...@@ -376,7 +374,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) { ...@@ -376,7 +374,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env interface{}) {
var gwErrc <-chan error var gwErrc <-chan error
if len(cfg.Addresses.Gateway) > 0 { if len(cfg.Addresses.Gateway) > 0 {
var err error var err error
err, gwErrc = serveHTTPGateway(req, ctx) err, gwErrc = serveHTTPGateway(req, cctx)
if err != nil { if err != nil {
re.SetError(err, cmdkit.ErrNormal) re.SetError(err, cmdkit.ErrNormal)
return return
......
...@@ -108,11 +108,8 @@ func mainRet() int { ...@@ -108,11 +108,8 @@ func mainRet() int {
} }
log.Debugf("config path is %s", repoPath) log.Debugf("config path is %s", repoPath)
// this sets up the function that will initialize the config lazily.
// this sets up the function that will initialize the node // this sets up the function that will initialize the node
// this is so that we can construct the node lazily. // this is so that we can construct the node lazily.
return &oldcmds.Context{ return &oldcmds.Context{
ConfigRoot: repoPath, ConfigRoot: repoPath,
LoadConfig: loadConfig, LoadConfig: loadConfig,
......
...@@ -53,29 +53,3 @@ func OldContext(env interface{}) *oldcmds.Context { ...@@ -53,29 +53,3 @@ func OldContext(env interface{}) *oldcmds.Context {
return ctx return ctx
} }
/*
// OldReqLog returns an oldcmds.ReqLog from a ReqLog
func OldReqLog(newrl *ReqLog) *oldcmds.ReqLog {
if newrl == nil {
return nil
}
rl := &oldcmds.ReqLog{}
for _, rle := range newrl.Requests {
oldrle := &oldcmds.ReqLogEntry{
StartTime: rle.StartTime,
EndTime: rle.EndTime,
Active: rle.Active,
Command: rle.Command,
Options: rle.Options,
Args: rle.Args,
ID: rle.ID,
}
rl.AddEntry(oldrle)
}
return rl
}
*/
...@@ -184,32 +184,3 @@ func TestTeeEmitter(t *testing.T) { ...@@ -184,32 +184,3 @@ func TestTeeEmitter(t *testing.T) {
t.Fatal("expected %#v, got %#v", expect, buf2.String()) t.Fatal("expected %#v, got %#v", expect, buf2.String())
} }
} }
/*
type teeErrorTestCase struct {
err1, err2 error
bothNil bool
errString string
}
func TestTeeError(t *testing.T) {
tcs := []teeErrorTestCase{
teeErrorTestCase{nil, nil, true, ""},
teeErrorTestCase{fmt.Errorf("error!"), nil, false, "1: error!"},
teeErrorTestCase{nil, fmt.Errorf("error!"), false, "2: error!"},
teeErrorTestCase{fmt.Errorf("error!"), fmt.Errorf("error!"), false, `1: error!
2: error!`},
}
for i, tc := range tcs {
teeError := cmds.TeeError{tc.err1, tc.err2}
if teeError.BothNil() != tc.bothNil {
t.Fatalf("BothNil()/%d: expected %v but got %v", i, tc.bothNil, teeError.BothNil())
}
if teeError.Error() != tc.errString {
t.Fatalf("Error()/%d: expected %v but got %v", i, tc.errString, teeError.Error())
}
}
}
*/
...@@ -76,6 +76,9 @@ func (c *Context) RootContext() context.Context { ...@@ -76,6 +76,9 @@ func (c *Context) RootContext() context.Context {
return n.Context() return n.Context()
} }
// LogRequest adds the passed request to the request log and
// returns a function that should be called when the request
// lifetime is over.
func (c *Context) LogRequest(req *cmds.Request) func() { func (c *Context) LogRequest(req *cmds.Request) func() {
rle := &ReqLogEntry{ rle := &ReqLogEntry{
StartTime: time.Now(), StartTime: time.Now(),
...@@ -93,6 +96,7 @@ func (c *Context) LogRequest(req *cmds.Request) func() { ...@@ -93,6 +96,7 @@ func (c *Context) LogRequest(req *cmds.Request) func() {
} }
} }
// Close cleans up the application state.
func (c *Context) Close() { func (c *Context) Close() {
// let's not forget teardown. If a node was initialized, we must close it. // let's not forget teardown. If a node was initialized, we must close it.
// Note that this means the underlying req.Context().Node variable is exposed. // Note that this means the underlying req.Context().Node variable is exposed.
......
...@@ -145,6 +145,7 @@ You can now check what blocks have been created by: ...@@ -145,6 +145,7 @@ You can now check what blocks have been created by:
return nil return nil
} }
// HACK! Using context to pass the size to PostRun
sizeCh := make(chan int64, 1) sizeCh := make(chan int64, 1)
req.Context = context.WithValue(req.Context, "size", sizeCh) req.Context = context.WithValue(req.Context, "size", sizeCh)
...@@ -366,6 +367,7 @@ You can now check what blocks have been created by: ...@@ -366,6 +367,7 @@ You can now check what blocks have been created by:
bar.Start() bar.Start()
} }
// HACK! using context to pass size from PreRun
var sizeChan chan int64 var sizeChan chan int64
sizeChan, _ = req.Context.Value("size").(chan int64) sizeChan, _ = req.Context.Value("size").(chan int64)
...@@ -429,7 +431,7 @@ You can now check what blocks have been created by: ...@@ -429,7 +431,7 @@ You can now check what blocks have been created by:
bar.ShowTimeLeft = true bar.ShowTimeLeft = true
} }
case <-req.Context.Done(): case <-req.Context.Done():
//re.SetError(req.Context.Err(), cmdkit.ErrNormal) // don't set or print error here, that happens in the goroutine below
return return
} }
} }
......
...@@ -62,7 +62,7 @@ var CatCmd = &cmds.Command{ ...@@ -62,7 +62,7 @@ var CatCmd = &cmds.Command{
} }
err = req.ParseBodyArgs() err = req.ParseBodyArgs()
if err != nil && err.Error() != "all arguments covered by positional arguments" { if err != nil && !cmds.IsAllArgsAlreadyCovered(err) {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
} }
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"sort" "sort"
"strings" "strings"
// oldcmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e" e "github.com/ipfs/go-ipfs/core/commands/e"
"gx/ipfs/QmSRaAPPNxyhnXeDa5NXtZ2CWBYJ6BRWNQp6gKxhPcoqDM/go-ipfs-cmdkit" "gx/ipfs/QmSRaAPPNxyhnXeDa5NXtZ2CWBYJ6BRWNQp6gKxhPcoqDM/go-ipfs-cmdkit"
......
...@@ -45,12 +45,10 @@ func TestGetOutputPath(t *testing.T) { ...@@ -45,12 +45,10 @@ func TestGetOutputPath(t *testing.T) {
}, },
} }
/* _, err := GetCmd.GetOptions([]string{})
defOpts, err := GetCmd.GetOptions([]string{}) if err != nil {
if err != nil { t.Fatalf("error getting default command options: %v", err)
t.Fatalf("error getting default command options: %v", err) }
}
*/
for _, tc := range cases { for _, tc := range cases {
req, err := cmds.NewRequest(context.TODO(), []string{}, tc.opts, tc.args, nil, GetCmd) req, err := cmds.NewRequest(context.TODO(), []string{}, tc.opts, tc.args, nil, GetCmd)
......
...@@ -135,7 +135,7 @@ test_expect_success "get IPFS directory file through readonly API output looks g ...@@ -135,7 +135,7 @@ test_expect_success "get IPFS directory file through readonly API output looks g
test_cmp dir/test actual test_cmp dir/test actual
' '
test_expect_failure "refs IPFS directory file through readonly API succeeds" ' test_expect_success "refs IPFS directory file through readonly API succeeds" '
test_curl_gateway_api "refs?arg=$HASH2/test" test_curl_gateway_api "refs?arg=$HASH2/test"
' '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论