-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(published): datasets now have a publish flag
- Loading branch information
Showing
14 changed files
with
249 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/qri-io/qri/repo" | ||
) | ||
|
||
// InLocalNamespace checks if a dataset ref is local, assumes the reference is already canonicalized | ||
func InLocalNamespace(r repo.Repo, ref *repo.DatasetRef) bool { | ||
p, err := r.Profile() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return p.ID == ref.ProfileID | ||
} | ||
|
||
// SetPublishStatus updates the Published field of a dataset ref | ||
func SetPublishStatus(r repo.Repo, ref *repo.DatasetRef) error { | ||
if err := repo.CanonicalizeDatasetRef(r, ref); err != nil { | ||
return err | ||
} | ||
|
||
if !InLocalNamespace(r, ref) { | ||
return fmt.Errorf("can't publish datsets that are not in your namespace") | ||
} | ||
|
||
return r.PutRef(*ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package base | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/qri-io/qri/repo" | ||
"github.com/qri-io/qri/repo/profile" | ||
) | ||
|
||
func TestInLocalNamespace(t *testing.T) { | ||
r := newTestRepo(t) | ||
reff := addCitiesDataset(t, r) | ||
ref := &reff | ||
|
||
if !InLocalNamespace(r, ref) { | ||
t.Errorf("expected %s true", ref.String()) | ||
} | ||
|
||
ref = &repo.DatasetRef{} | ||
if InLocalNamespace(r, ref) { | ||
t.Errorf("expected %s false", ref.String()) | ||
} | ||
|
||
ref = &repo.DatasetRef{ProfileID: profile.ID("fake")} | ||
if InLocalNamespace(r, ref) { | ||
t.Errorf("expected %s false", ref.String()) | ||
} | ||
} | ||
|
||
func TestSetPublishStatus(t *testing.T) { | ||
r := newTestRepo(t) | ||
ref := addCitiesDataset(t, r) | ||
|
||
ref.Published = true | ||
if err := SetPublishStatus(r, &ref); err != nil { | ||
t.Error(err) | ||
} | ||
res, err := r.GetRef(repo.DatasetRef{Peername: ref.Peername, Name: ref.Name}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res.Published != true { | ||
t.Errorf("expected published to equal true: %s,%s", ref, res) | ||
} | ||
|
||
ref.Published = false | ||
if err := SetPublishStatus(r, &ref); err != nil { | ||
t.Error(err) | ||
} | ||
res, err = r.GetRef(repo.DatasetRef{Peername: ref.Peername, Name: ref.Name}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res.Published != false { | ||
t.Errorf("expected published to equal false: %s,%s", ref, res) | ||
} | ||
|
||
if err := SetPublishStatus(r, &repo.DatasetRef{Name: "foo"}); err == nil { | ||
t.Error("expected invalid reference to error") | ||
} | ||
|
||
outside := repo.MustParseDatasetRef("a/b@QmX1oSPMbzkhk33EutuadL4sqsivsRKmMx5hAnZL2mRAM1/ipfs/d") | ||
if err := r.PutRef(outside); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
r.Profiles().PutProfile(&profile.Profile{ID: outside.ProfileID, Peername: outside.Peername}) | ||
|
||
outside.Published = true | ||
if err := SetPublishStatus(r, &outside); err == nil { | ||
t.Error("expected setting the publish status of a name outside peer's namespace to fail") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/qri-io/ioes" | ||
"github.com/qri-io/qri/lib" | ||
"github.com/qri-io/qri/repo" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewPublishCommand creates a `qri publish` subcommand for working with configured registries | ||
func NewPublishCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command { | ||
o := &PublishOptions{IOStreams: ioStreams} | ||
cmd := &cobra.Command{ | ||
Use: "publish", | ||
Short: "set dataset publicity", | ||
Long: `Publish makes your dataset available to others. While online, peers that connect | ||
to you can only see datasets and versions that you've published. Publishing a | ||
dataset always makes all previous history entries available, and any updates | ||
to a published dataset will be immideately visible to connected peers. | ||
`, | ||
Example: ` # publish a dataset | ||
$ qri publish me/dataset | ||
# publish a few datasets | ||
$ qri publish me/dataset me/other_dataset | ||
# unpublish a dataset | ||
$ qri publish -d me/dataset`, | ||
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() | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
// PublishOptions encapsulates state for the publish command | ||
type PublishOptions struct { | ||
ioes.IOStreams | ||
|
||
Refs []string | ||
|
||
DatasetRequests *lib.DatasetRequests | ||
} | ||
|
||
// 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 | ||
o.DatasetRequests, err = f.DatasetRequests() | ||
return | ||
} | ||
|
||
// Run executes the publish command | ||
func (o *PublishOptions) Run() error { | ||
var res bool | ||
|
||
for _, arg := range o.Refs { | ||
ref, err := repo.ParseDatasetRef(arg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = o.DatasetRequests.SetPublishStatus(&ref, &res); err != nil { | ||
return err | ||
} | ||
printInfo(o.Out, "published dataset %s", ref) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters