Skip to content

Commit

Permalink
feat(fetch): fetch pulls logs via logsync
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 15, 2019
1 parent 585c42c commit 0f3f2af
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 26 deletions.
38 changes: 19 additions & 19 deletions actions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ import (

// DatasetLog fetches the history of changes to a dataset
func DatasetLog(ctx context.Context, node *p2p.QriNode, ref repo.DatasetRef, limit, offset int) (rlog []base.DatasetLogItem, err error) {
local, err := ResolveDatasetRef(ctx, node, nil, "", &ref)
_, err = ResolveDatasetRef(ctx, node, nil, "", &ref)
if err != nil {
return
}

if !local {
res, err := node.RequestDatasetLog(ctx, ref, limit, offset)
if err != nil {
return nil, err
}
rlog = make([]base.DatasetLogItem, len(res))
for i, v := range res {
rlog[i] = base.DatasetLogItem{
Ref: repo.ConvertToDsref(v),
}
if v.Dataset != nil && v.Dataset.Commit != nil {
rlog[i].Timestamp = v.Dataset.Commit.Timestamp
rlog[i].CommitTitle = v.Dataset.Commit.Title
rlog[i].CommitMessage = v.Dataset.Commit.Message
}
}
return rlog, err
}
// if !local {
// res, err := node.RequestDatasetLog(ctx, ref, limit, offset)
// if err != nil {
// return nil, err
// }
// rlog = make([]base.DatasetLogItem, len(res))
// for i, v := range res {
// rlog[i] = base.DatasetLogItem{
// Ref: repo.ConvertToDsref(v),
// }
// if v.Dataset != nil && v.Dataset.Commit != nil {
// rlog[i].Timestamp = v.Dataset.Commit.Timestamp
// rlog[i].CommitTitle = v.Dataset.Commit.Title
// rlog[i].CommitMessage = v.Dataset.Commit.Message
// }
// }
// return rlog, err
// }

return base.DatasetLog(ctx, node.Repo, ref, limit, offset, true)
}
71 changes: 71 additions & 0 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"github.com/qri-io/ioes"
"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
)

// NewFetchCommand creates a `qri fetch` subcommand for working with configured registries
func NewFetchCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
o := &FetchOptions{IOStreams: ioStreams}
cmd := &cobra.Command{
Use: "fetch",
Short: "fetch logbook info for a dataset reference",
Long: ``,
Example: ``,
Annotations: map[string]string{
"group": "network",
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, args); err != nil {
return err
}
return o.Run()
},
}

// cmd.Flags().BoolVarP(&o.Unfetch, "unfetch", "", false, "unfetch a dataset")
// cmd.Flags().BoolVarP(&o.NoRegistry, "no-registry", "", false, "don't fetch to registry")
// cmd.Flags().BoolVarP(&o.NoPin, "no-pin", "", false, "don't pin dataset to registry")
cmd.Flags().StringVarP(&o.RemoteName, "remote", "", "", "name of remote to fetch to")

return cmd
}

// FetchOptions encapsulates state for the fetch command
type FetchOptions struct {
ioes.IOStreams

Refs []string
Unfetch bool
NoRegistry bool
NoPin bool
RemoteName string

RemoteMethods *lib.RemoteMethods
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *FetchOptions) Complete(f Factory, args []string) (err error) {
o.Refs = args
o.RemoteMethods, err = f.RemoteMethods()
return
}

// Run executes the fetch command
func (o *FetchOptions) Run() error {
var res repo.DatasetRef
for _, ref := range o.Refs {
p := lib.FetchParams{
Ref: ref,
RemoteName: o.RemoteName,
}
if err := o.RemoteMethods.Fetch(&p, &res); err != nil {
return err
}
printInfo(o.Out, "fetched dataset %s", res)
}
return nil
}
1 change: 1 addition & 0 deletions cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ https://github.com/qri-io/qri/issues`,
NewDAGCommand(opt, ioStreams),
NewDiffCommand(opt, ioStreams),
NewExportCommand(opt, ioStreams),
NewFetchCommand(opt, ioStreams),
NewFSICommand(opt, ioStreams),
NewGetCommand(opt, ioStreams),
NewInitCommand(opt, ioStreams),
Expand Down
6 changes: 5 additions & 1 deletion lib/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func (r *LogRequests) Log(params *LogParams, res *[]DatasetLogItem) (err error)
return fmt.Errorf("'%s' is not a valid dataset reference", params.Ref)
}
if err = repo.CanonicalizeDatasetRef(r.node.Repo, &ref); err != nil {
return
if err == repo.ErrNotFound {
err = nil
} else {
return
}
}

// ensure valid limit value
Expand Down
10 changes: 9 additions & 1 deletion lib/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ type FetchParams struct {

// Fetch pulls a logbook from a remote
func (r *RemoteMethods) Fetch(p *FetchParams, res *repo.DatasetRef) error {
if r.inst.rpc != nil {
return r.inst.rpc.Call("RemoteMethods.Fetch", p, res)
}

ref, err := repo.ParseDatasetRef(p.Ref)
if err != nil {
return err
}
if err = repo.CanonicalizeDatasetRef(r.inst.Repo(), &ref); err != nil {
return err
if err == repo.ErrNotFound {
err = nil
} else {
return err
}
}
*res = ref

Expand Down
10 changes: 5 additions & 5 deletions logbook/logbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,12 @@ func newTestRunner(t *testing.T) (tr *testRunner, cleanup func()) {
authorName := "test_author"
pk := testPrivKey(t)
fs := qfs.NewMemFS()
prevTs := newTimestamp
prevTs := NewTimestamp
tr = &testRunner{
Ctx: ctx,
Username: authorName,
}
newTimestamp = tr.newTimestamp
NewTimestamp = tr.newTimestamp

var err error
tr.Book, err = NewBook(pk, authorName, fs, "/mem/logset")
Expand All @@ -613,15 +613,15 @@ func newTestRunner(t *testing.T) (tr *testRunner, cleanup func()) {
}

cleanup = func() {
newTimestamp = prevTs
NewTimestamp = prevTs
}

return tr, cleanup
}

func (tr *testRunner) newTimestamp() time.Time {
func (tr *testRunner) newTimestamp() int64 {
defer func() { tr.Tick++ }()
return time.Unix(int64(94670280000+tr.Tick), 0)
return time.Unix(int64(94670280000+tr.Tick), 0).UnixNano()
}

func (tr *testRunner) WorldBankRef() dsref.Ref {
Expand Down

0 comments on commit 0f3f2af

Please sign in to comment.