Skip to content

Commit

Permalink
feat(cmd registry pin): add registry pin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Nov 20, 2018
1 parent 88a25ab commit 2293275
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 34 deletions.
80 changes: 80 additions & 0 deletions actions/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/profile"
"github.com/qri-io/registry"
"github.com/qri-io/registry/regclient"
)

Expand Down Expand Up @@ -67,6 +68,85 @@ func Status(node *p2p.QriNode, ref repo.DatasetRef) (err error) {
return nil
}

// Pin asks a registry to host a copy of a dataset
func Pin(node *p2p.QriNode, ref repo.DatasetRef) (err error) {
node.LocalStreams.Print("📌 pinning dataset")
r := node.Repo
reg := node.Repo.Registry()
if reg == nil {
return fmt.Errorf("no registry specified")
}

pk := r.PrivateKey()
if pk == nil {
err = fmt.Errorf("repo has no configured private key")
return
}

if err = repo.CanonicalizeDatasetRef(r, &ref); err != nil {
err = fmt.Errorf("canonicalizing dataset reference: %s", err.Error())
return
}

if ref.Path == "" {
if ref, err = r.GetRef(ref); err != nil {
return
}
}

if !node.Online {
if err = node.GoOnline(); err != nil {
return err
}
}

var addrs []string
for _, maddr := range node.EncapsulatedAddresses() {
addrs = append(addrs, maddr.String())
}

if err = reg.Pin(ref.Path, pk, addrs); err != nil {
if err == registry.ErrPinsetNotSupported {
log.Info("this registry does not support pinning, dataset not pinned.")
} else {
return err
}
} else {
log.Info("done")
}

return nil
}

// Unpin reverses the pin process
func Unpin(node *p2p.QriNode, ref repo.DatasetRef) (err error) {
node.LocalStreams.Print("📌 unpinning dataset")
r := node.Repo
reg := node.Repo.Registry()
if reg == nil {
return fmt.Errorf("no registry specified")
}

pk := r.PrivateKey()
if pk == nil {
err = fmt.Errorf("repo has no configured private key")
return
}

if err = repo.CanonicalizeDatasetRef(r, &ref); err != nil {
err = fmt.Errorf("canonicalizing dataset reference: %s", err.Error())
return
}

if ref.Path == "" {
if ref, err = r.GetRef(ref); err != nil {
return
}
}

return reg.Unpin(ref.Path, pk)
}

// dsParams is a convenience func that collects params for registry dataset interaction
func dsParams(r repo.Repo, ref *repo.DatasetRef) (cli *regclient.Client, pub crypto.PubKey, ds *dataset.Dataset, err error) {
if cli = r.Registry(); cli == nil {
Expand Down
76 changes: 75 additions & 1 deletion cmd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,41 @@ This dataset will no longer show up in search results.`,
},
}

cmd.AddCommand(publish, unpublish, status)
pin := &cobra.Command{
Use: "pin",
Short: "pin dataset data to the registry",
Long: `
Pin asks a registry to host a copy of your dataset, making it available for
others to download on the d.web`,
Example: ` Pin a dataset to the registry:
$ qri registry pin me/dataset_name`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, args); err != nil {
return err
}
return o.Pin()
},
}

unpin := &cobra.Command{
Use: "unpin",
Short: "unpin dataset data from the registry",
Long: `
Unpin reverses the pin process, asking a registry to remove it's hosted copy
of your dataset from the registry`,
Example: ` Unpin a dataset from the registry:
$ qri registry unpin me/dataset_name`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, args); err != nil {
return err
}
return o.Unpin()
},
}

cmd.AddCommand(publish, unpublish, status, pin, unpin)
return cmd
}

Expand Down Expand Up @@ -194,3 +228,43 @@ func (o *RegistryOptions) Unpublish() error {
}
return nil
}

// Pin executes the pin command
func (o *RegistryOptions) Pin() error {
var res bool
o.StartSpinner()
defer o.StopSpinner()

for _, arg := range o.Refs {
ref, err := repo.ParseDatasetRef(arg)
if err != nil {
return err
}

if err = o.RegistryRequests.Pin(&ref, &res); err != nil {
return err
}
printInfo(o.Out, "pinned dataset %s", ref)
}
return nil
}

// Unpin executes the unpin command
func (o *RegistryOptions) Unpin() error {
var res bool
o.StartSpinner()
defer o.StopSpinner()

for _, arg := range o.Refs {
ref, err := repo.ParseDatasetRef(arg)
if err != nil {
return err
}

if err = o.RegistryRequests.Unpin(&ref, &res); err != nil {
return err
}
printInfo(o.Out, "unpinned dataset %s", ref)
}
return nil
}
6 changes: 4 additions & 2 deletions cmd/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/qri-io/ioes"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/lib"
"github.com/qri-io/registry/pinset"
"github.com/qri-io/registry/regserver/mock"
)

Expand Down Expand Up @@ -109,8 +110,9 @@ func TestSaveRun(t *testing.T) {
defer func() { dsfs.Timestamp = prev }()
dsfs.Timestamp = func() time.Time { return time.Date(2001, 01, 01, 01, 01, 01, 01, time.UTC) }

reg := mock.NewMemRegistryPinset()
c, _ := mock.NewMockServerRegistry(reg)
reg := mock.NewMemRegistry()
ps := &pinset.MemPinset{Profiles: reg.Profiles}
c, _ := mock.NewMockServerRegistryPinset(reg, ps)

f, err := NewTestFactory(c)
if err != nil {
Expand Down
49 changes: 18 additions & 31 deletions lib/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,7 @@ func (r *RegistryRequests) Publish(p *PublishParams, done *bool) (err error) {
if r.cli != nil {
return r.cli.Call("RegistryRequests.Publish", p, done)
}

ref := p.Ref

// if p.Pin {
// log.Info("pinning dataset...")
// node := r.node

// if !node.Online {
// if err = node.Connect(); err != nil {
// return err
// }
// if err = node.StartOnlineServices(func(string) {}); err != nil {
// return err
// }
// }

// var addrs []string
// for _, maddr := range node.EncapsulatedAddresses() {
// addrs = append(addrs, maddr.String())
// }

// if err = r.node.repo.Pin(ref, addrs); err != nil {
// if err == registry.ErrPinsetNotSupported {
// log.Info("this registry does not support pinning, dataset not pinned.")
// } else {
// return err
// }
// } else {
// log.Info("done")
// }
// }

return actions.Publish(r.node, ref)
}

Expand All @@ -92,3 +61,21 @@ func (r *RegistryRequests) Status(ref *repo.DatasetRef, done *bool) error {
}
return actions.Status(r.node, *ref)
}

// Pin asks a registry to host a copy of a dataset
func (r *RegistryRequests) Pin(ref *repo.DatasetRef, done *bool) (err error) {
if r.cli != nil {
return r.cli.Call("RegistryRequests.Pin", ref, done)
}
return actions.Pin(r.node, *ref)
}

// Unpin reverses the pin process, asking a registry to stop hosting a copy of
// an already-pinned dataset
func (r *RegistryRequests) Unpin(ref *repo.DatasetRef, done *bool) error {
if r.cli != nil {
return r.cli.Call("RegistryRequests.Unpin", ref, done)
}

return actions.Unpin(r.node, *ref)
}

0 comments on commit 2293275

Please sign in to comment.