Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for the go-ipfs-cmds refactor #5035

Merged
merged 11 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"

cmds "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
mprome "gx/ipfs/QmUHHsirrDtP6WEHhE8SZeG672CLqDJn6XGzAHnvBHUiA3/go-metrics-prometheus"
"gx/ipfs/QmV6FjemM1K8oXjrvuq3wuVWWoU2TLDPmNnKrxHzY3v6Ai/go-multiaddr-net"
Expand Down Expand Up @@ -184,7 +184,7 @@ func defaultMux(path string) corehttp.ServeOption {
}
}

func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) {
func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
// Inject metrics before we do anything
err := mprome.Inject()
if err != nil {
Expand Down Expand Up @@ -227,8 +227,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment

err := initWithDefaults(os.Stdout, cfg, profiles)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}
}
}
Expand All @@ -238,8 +237,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
repo, err := fsrepo.Open(cctx.ConfigRoot)
switch err {
default:
re.SetError(err, cmdkit.ErrNormal)
return
return err
case fsrepo.ErrNeedMigration:
domigrate, found := req.Options[migrateKwd].(bool)
fmt.Println("Found outdated fs-repo, migrations need to be run.")
Expand All @@ -251,8 +249,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
if !domigrate {
fmt.Println("Not running migrations of fs-repo now.")
fmt.Println("Please get fs-repo-migrations from https://dist.ipfs.io")
re.SetError(fmt.Errorf("fs-repo requires migration"), cmdkit.ErrNormal)
return
return fmt.Errorf("fs-repo requires migration")
}

err = migrate.RunMigration(fsrepo.RepoVersion)
Expand All @@ -261,23 +258,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
fmt.Printf(" %s\n", err)
fmt.Println("If you think this is a bug, please file an issue and include this whole log output.")
fmt.Println(" https://github.com/ipfs/fs-repo-migrations")
re.SetError(err, cmdkit.ErrNormal)
return
return err
}

repo, err = fsrepo.Open(cctx.ConfigRoot)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}
case nil:
break
}

cfg, err := cctx.GetConfig()
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}

offline, _ := req.Options[offlineKwd].(bool)
Expand All @@ -303,8 +297,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
if routingOption == routingOptionDefaultKwd {
cfg, err := repo.Config()
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}

routingOption = cfg.Routing.Type
Expand All @@ -314,24 +307,21 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
}
switch routingOption {
case routingOptionSupernodeKwd:
re.SetError(errors.New("supernode routing was never fully implemented and has been removed"), cmdkit.ErrNormal)
return
return errors.New("supernode routing was never fully implemented and has been removed")
case routingOptionDHTClientKwd:
ncfg.Routing = core.DHTClientOption
case routingOptionDHTKwd:
ncfg.Routing = core.DHTOption
case routingOptionNoneKwd:
ncfg.Routing = core.NilRouterOption
default:
re.SetError(fmt.Errorf("unrecognized routing option: %s", routingOption), cmdkit.ErrNormal)
return
return fmt.Errorf("unrecognized routing option: %s", routingOption)
}

node, err := core.NewNode(req.Context, ncfg)
if err != nil {
log.Error("error from node construction: ", err)
re.SetError(err, cmdkit.ErrNormal)
return
return err
}
node.SetLocal(false)

Expand Down Expand Up @@ -361,29 +351,24 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
// construct api endpoint - every time
apiErrc, err := serveHTTPApi(req, cctx)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}

// construct fuse mountpoints - if the user provided the --mount flag
mount, _ := req.Options[mountKwd].(bool)
if mount && offline {
re.SetError(errors.New("mount is not currently supported in offline mode"),
cmdkit.ErrClient)
return
return cmdkit.Errorf(cmdkit.ErrClient, "mount is not currently supported in offline mode")
}
if mount {
if err := mountFuse(req, cctx); err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}
}

// repo blockstore GC - if --enable-gc flag is present
gcErrc, err := maybeRunGC(req, node)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}

// construct http gateway - if it is set in the config
Expand All @@ -392,8 +377,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
var err error
gwErrc, err = serveHTTPGateway(req, cctx)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
return err
}
}

Expand All @@ -405,10 +389,11 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
// TODO(cryptix): our fuse currently doesnt follow this pattern for graceful shutdown
for err := range merge(apiErrc, gwErrc, gcErrc) {
if err != nil {
log.Error(err)
re.SetError(err, cmdkit.ErrNormal)
return err
}
}

return nil
}

// serveHTTPApi collects options, creates listener, prints status message and starts serving requests
Expand Down
18 changes: 6 additions & 12 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
namesys "github.com/ipfs/go-ipfs/namesys"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
)
Expand Down Expand Up @@ -72,11 +72,10 @@ environment variable:

return nil
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cctx := env.(*oldcmds.Context)
if cctx.Online {
res.SetError(errors.New("init must be run offline only"), cmdkit.ErrNormal)
return
return cmdkit.Error{Message: "init must be run offline only"}
}

empty, _ := req.Options["empty-repo"].(bool)
Expand All @@ -88,14 +87,12 @@ environment variable:
if f != nil {
confFile, err := f.NextFile()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

conf = &config.Config{}
if err := json.NewDecoder(confFile).Decode(conf); err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
}

Expand All @@ -106,10 +103,7 @@ environment variable:
profiles = strings.Split(profile, ",")
}

if err := doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf); err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

commands "github.com/ipfs/go-ipfs/core/commands"

cmds "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
)

// This is the CLI root, used for executing commands accessible to CLI clients.
Expand Down
6 changes: 3 additions & 3 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
repo "github.com/ipfs/go-ipfs/repo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds/cli"
"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds/http"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds/cli"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds/http"
u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util"
logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
manet "gx/ipfs/QmV6FjemM1K8oXjrvuq3wuVWWoU2TLDPmNnKrxHzY3v6Ai/go-multiaddr-net"
Expand Down
12 changes: 5 additions & 7 deletions commands/legacy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package legacy
import (
"io"

"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"

oldcmds "github.com/ipfs/go-ipfs/commands"

"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
)

Expand All @@ -29,17 +29,15 @@ func NewCommand(oldcmd *oldcmds.Command) *cmds.Command {
}

if oldcmd.Run != nil {
cmd.Run = func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) {
cmd.Run = func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
oldReq := &requestWrapper{req, OldContext(env)}
res := &fakeResponse{req: oldReq, re: re, wait: make(chan struct{})}

errCh := make(chan error)
go res.Send(errCh)

oldcmd.Run(oldReq, res)
err := <-errCh
if err != nil {
log.Error(err)
}
return <-errCh
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io"
"runtime/debug"

"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"

oldcmds "github.com/ipfs/go-ipfs/commands"
)
Expand Down
62 changes: 59 additions & 3 deletions commands/legacy/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

oldcmds "github.com/ipfs/go-ipfs/commands"
cmds "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
)

Expand Down Expand Up @@ -80,7 +80,7 @@ func TestNewCommand(t *testing.T) {

root.Call(req, re, &env)

expected := `{"Value":"Test."}
expected := `"Test."
`

if buf.String() != expected {
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestNewCommand(t *testing.T) {
}

func TestPipePair(t *testing.T) {
cmd := &cmds.Command{Type: "string"}
cmd := NewCommand(&oldcmds.Command{Type: "string"})

req, err := cmds.NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
Expand All @@ -134,6 +134,11 @@ func TestPipePair(t *testing.T) {
t.Fatal(err)
}

err = re.Close()
if err != nil {
t.Fatal(err)
}

close(wait)
}()

Expand All @@ -149,6 +154,57 @@ func TestPipePair(t *testing.T) {
t.Fatalf("expected value %#v but got %#v", expect, v)
}

_, err = res.Next()
if err != io.EOF {
t.Fatal("expected io.EOF, got:", err)
}

<-wait
}

func TestChanPair(t *testing.T) {
cmd := NewCommand(&oldcmds.Command{Type: "string"})

req, err := cmds.NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
t.Fatal(err)
}

re, res := cmds.NewChanResponsePair(req)

wait := make(chan interface{})

expect := "abc"
go func() {
err := re.Emit(expect)
if err != nil {
t.Fatal(err)
}

err = re.Close()
if err != nil {
t.Fatal(err)
}

close(wait)
}()

v, err := res.Next()
if err != nil {
t.Fatal(err)
}
str, ok := v.(string)
if !ok {
t.Fatalf("expected type %T but got %T", expect, v)
}
if str != expect {
t.Fatalf("expected value %#v but got %#v", expect, v)
}

_, err = res.Next()
if err != io.EOF {
t.Fatal("expected io.EOF, got:", err)
}

<-wait
}
2 changes: 1 addition & 1 deletion commands/legacy/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"reflect"

"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"

Expand Down
Loading