Skip to content

Commit

Permalink
fix(publish): Use Refselect for publish, so FSI works. Use dsref, add…
Browse files Browse the repository at this point in the history
… tests.
  • Loading branch information
dustmop committed Feb 7, 2020
1 parent 1c7eece commit 68dfff3
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 36 deletions.
3 changes: 1 addition & 2 deletions api/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
util "github.com/qri-io/apiutil"
"github.com/qri-io/qri/dsref"
"github.com/qri-io/qri/lib"
reporef "github.com/qri-io/qri/repo/ref"
)

// RemoteClientHandlers provides HTTP handlers for issuing requests to remotes
Expand Down Expand Up @@ -80,8 +79,8 @@ func (h *RemoteClientHandlers) PublishHandler(w http.ResponseWriter, r *http.Req
Ref: ref.String(),
RemoteName: r.FormValue("remote"),
}
var res reporef.DatasetRef

var res dsref.Ref
switch r.Method {
case "POST":
if err := h.Publish(p, &res); err != nil {
Expand Down
41 changes: 23 additions & 18 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

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

Expand Down Expand Up @@ -52,7 +52,7 @@ to a published dataset will be immediately visible to connected peers.
type PublishOptions struct {
ioes.IOStreams

Refs []string
Refs *RefSelect
Unpublish bool
NoRegistry bool
NoPin bool
Expand All @@ -64,33 +64,38 @@ type PublishOptions struct {

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

if o.DatasetRequests, err = f.DatasetRequests(); err != nil {
return err
}

if o.Refs, err = GetCurrentRefSelect(f, args, 1, nil); err != nil {
return err
}

o.RemoteMethods, err = f.RemoteMethods()
return
}

// Run executes the publish command
func (o *PublishOptions) Run() error {
var res reporef.DatasetRef
for _, ref := range o.Refs {
p := lib.PublicationParams{
Ref: ref,
RemoteName: o.RemoteName,
printRefSelect(o.Out, o.Refs)

p := lib.PublicationParams{
Ref: o.Refs.Ref(),
RemoteName: o.RemoteName,
}
var res dsref.Ref
if o.Unpublish {
if err := o.RemoteMethods.Unpublish(&p, &res); err != nil {
return err
}
if o.Unpublish {
if err := o.RemoteMethods.Unpublish(&p, &res); err != nil {
return err
}
printInfo(o.Out, "unpublished dataset %s", res)
} else {
if err := o.RemoteMethods.Publish(&p, &res); err != nil {
return err
}
printInfo(o.Out, "published dataset %s", res)
printInfo(o.Out, "unpublished dataset %s", res)
} else {
if err := o.RemoteMethods.Publish(&p, &res); err != nil {
return err
}
printInfo(o.Out, "published dataset %s", res)
}
return nil
}
51 changes: 51 additions & 0 deletions cmd/publish_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"testing"

"github.com/qri-io/qri/registry"
"github.com/qri-io/qri/registry/regserver"
)

// Test publishing to a mock registry
func TestPublish(t *testing.T) {
run := NewTestRunner(t, "test_peer", "qri_test_registry_publish")
defer run.Delete()

reg, cleanup, err := regserver.NewTempRegistry("temp_registry", "")
if err != nil {
t.Fatal(err)
}
defer cleanup()

// Create a mock registry, point our test runner to its URL
_, httpServer := regserver.NewMockServerRegistry(*reg)
run.RepoRoot.GetConfig().Registry.Location = httpServer.URL
err = run.RepoRoot.WriteConfigFile()
if err != nil {
t.Fatal(err)
}

// Save one commit
run.MustExec(t, "qri save me/one_ds --body testdata/movies/body_ten.csv")

// Publish to the registry
run.MustExec(t, "qri publish me/one_ds")

// Search, verify that we get the dataset back
results, err := reg.Search.Search(registry.SearchParams{
Q: "",
Limit: 2,
Offset: 0,
})
if err != nil {
t.Fatal(err)
}

if len(results) != 1 {
t.Errorf("expected: 1 result, got %d results", len(results))
}
if results[0].Name != "one_ds" {
t.Errorf("expected: dataset named \"one_ds\", got %q", results[0].Name)
}
}
9 changes: 5 additions & 4 deletions lib/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/qri-io/dataset"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/dsref"
"github.com/qri-io/qri/registry"
"github.com/qri-io/qri/registry/regserver"
"github.com/qri-io/qri/remote"
Expand Down Expand Up @@ -269,17 +270,17 @@ g,g,i,true,4`),
return res
}

func PublishToRegistry(t *testing.T, inst *Instance, refstr string) *reporef.DatasetRef {
res := &reporef.DatasetRef{}
func PublishToRegistry(t *testing.T, inst *Instance, refstr string) *dsref.Ref {
res := dsref.Ref{}
err := NewRemoteMethods(inst).Publish(&PublicationParams{
Ref: refstr,
}, res)
}, &res)

if err != nil {
log.Fatalf("publishing dataset: %s", err)
}

return res
return &res
}

func SearchFor(t *testing.T, inst *Instance, term string) []SearchResult {
Expand Down
26 changes: 16 additions & 10 deletions lib/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/qri-io/qri/logbook"
"github.com/qri-io/qri/remote"
"github.com/qri-io/qri/repo"
reporef "github.com/qri-io/qri/repo/ref"
)

const allowedDagInfoSize uint64 = 10 * 1024 * 1024
Expand Down Expand Up @@ -105,7 +104,7 @@ type PublicationParams struct {
}

// Publish posts a dataset version to a remote
func (r *RemoteMethods) Publish(p *PublicationParams, res *reporef.DatasetRef) error {
func (r *RemoteMethods) Publish(p *PublicationParams, res *dsref.Ref) error {
if r.inst.rpc != nil {
return r.inst.rpc.Call("RemoteMethods.Publish", p, res)
}
Expand All @@ -120,7 +119,6 @@ func (r *RemoteMethods) Publish(p *PublicationParams, res *reporef.DatasetRef) e
if err = repo.CanonicalizeDatasetRef(r.inst.Repo(), &ref); err != nil {
return err
}
*res = ref

addr, err := remote.Address(r.inst.Config(), p.RemoteName)
if err != nil {
Expand All @@ -142,12 +140,17 @@ func (r *RemoteMethods) Publish(p *PublicationParams, res *reporef.DatasetRef) e
return err
}

res.Published = true
return base.SetPublishStatus(r.inst.node.Repo, res, res.Published)
ref.Published = true
if err = base.SetPublishStatus(r.inst.node.Repo, &ref, ref.Published); err != nil {
return err
}

*res = ref.SimpleRef()
return nil
}

// Unpublish asks a remote to remove a dataset
func (r *RemoteMethods) Unpublish(p *PublicationParams, res *reporef.DatasetRef) error {
func (r *RemoteMethods) Unpublish(p *PublicationParams, res *dsref.Ref) error {
if r.inst.rpc != nil {
return r.inst.rpc.Call("RemoteMethods.Unpublish", p, res)
}
Expand All @@ -165,8 +168,6 @@ func (r *RemoteMethods) Unpublish(p *PublicationParams, res *reporef.DatasetRef)
return err
}

*res = ref

addr, err := remote.Address(r.inst.Config(), p.RemoteName)
if err != nil {
return err
Expand All @@ -187,8 +188,13 @@ func (r *RemoteMethods) Unpublish(p *PublicationParams, res *reporef.DatasetRef)
return err
}

res.Published = false
return base.SetPublishStatus(r.inst.node.Repo, res, res.Published)
ref.Published = false
if err = base.SetPublishStatus(r.inst.node.Repo, &ref, ref.Published); err != nil {
return err
}

*res = ref.SimpleRef()
return nil
}

// PullDataset fetches a dataset ref from a remote
Expand Down
13 changes: 11 additions & 2 deletions repo/ref/dataset_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/qri-io/dataset"
"github.com/qri-io/qri/dsref"
"github.com/qri-io/qri/repo/profile"
)

Expand Down Expand Up @@ -103,8 +104,6 @@ func (r DatasetRef) Complete() bool {
// Match checks returns true if Peername and Name are equal,
// and/or path is equal
func (r DatasetRef) Match(b DatasetRef) bool {
// fmt.Printf("\nr.Peername: %s b.Peername: %s\n", r.Peername, b.Peername)
// fmt.Printf("\nr.Name: %s b.Name: %s\n", r.Name, b.Name)
return (r.Path != "" && b.Path != "" && r.Path == b.Path) || (r.ProfileID == b.ProfileID || r.Peername == b.Peername) && r.Name == b.Name
}

Expand All @@ -122,3 +121,13 @@ func (r DatasetRef) IsPeerRef() bool {
func (r DatasetRef) IsEmpty() bool {
return r.Equal(DatasetRef{})
}

// SimpleRef converts a DatasetRef to a dsref.Ref
func (r DatasetRef) SimpleRef() dsref.Ref {
return dsref.Ref{
Username: r.Peername,
ProfileID: r.ProfileID.String(),
Name: r.Name,
Path: r.Path,
}
}

0 comments on commit 68dfff3

Please sign in to comment.