Skip to content

Commit

Permalink
fix(use): Move use up to cmd/ from lib/. Delete select from repo/
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Jul 2, 2019
1 parent 278deb9 commit 8286730
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 421 deletions.
2 changes: 2 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (o *DiffOptions) Run() (err error) {
Selector: o.Selector,
}

// TODO(dlong): Reenable `use` functionality for this command.

res := &lib.DiffResponse{}
if err = o.DatasetRequests.Diff(p, res); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ type ExportOptions struct {

// Complete adds any missing configuration that can only be added just before calling Run
func (o *ExportOptions) Complete(f Factory, args []string) (err error) {
if len(args) > 0 {
o.Ref = args[0]
o.Ref, err = GetDatasetRefString(f, args, 0)
if err != nil {
return err
}
if f.RPC() != nil {
return usingRPCError("export")
Expand Down
1 change: 0 additions & 1 deletion cmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Factory interface {
ProfileMethods() (*lib.ProfileMethods, error)
SearchRequests() (*lib.SearchRequests, error)
RenderRequests() (*lib.RenderRequests, error)
SelectionRequests() (*lib.SelectionRequests, error)
}

// PathFactory is a function that returns paths to qri & ipfs repos
Expand Down
5 changes: 0 additions & 5 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ func (t TestFactory) ProfileMethods() (*lib.ProfileMethods, error) {
return lib.NewProfileMethods(t.inst), nil
}

// SelectionRequests creates a lib.SelectionRequests from internal state
func (t TestFactory) SelectionRequests() (*lib.SelectionRequests, error) {
return lib.NewSelectionRequests(t.repo, t.rpc), nil
}

// SearchRequests generates a lib.SearchRequests from internal state
func (t TestFactory) SearchRequests() (*lib.SearchRequests, error) {
return lib.NewSearchRequests(t.node, t.rpc), nil
Expand Down
23 changes: 14 additions & 9 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ func (o *GetOptions) Complete(f Factory, args []string) (err error) {
args = args[1:]
}
}
o.Refs = args
o.Refs = make([]string, 0, len(args))
for i := range args {
var ref string
ref, err = GetDatasetRefString(f, args, i)
if err != nil {
return
}
o.Refs = append(o.Refs, ref)
}
if o.DatasetRequests, err = f.DatasetRequests(); err != nil {
return
}
Expand Down Expand Up @@ -123,14 +131,6 @@ func (o *GetOptions) Complete(f Factory, args []string) (err error) {

// Run executes the get command
func (o *GetOptions) Run() (err error) {
var path string
if len(o.Refs) > 0 {
path = o.Refs[0]
if err != nil {
return err
}
}

// Pretty maps to a key in the FormatConfig map.
var fc dataset.FormatConfig
if o.HasPretty {
Expand All @@ -142,6 +142,11 @@ func (o *GetOptions) Run() (err error) {
// convert Page and PageSize to Limit and Offset
page := util.NewPage(o.Page, o.PageSize)

var path string
if len(o.Refs) > 0 {
// TODO(dlong): Restore ability to `get` from multiple datasets at once.
path = o.Refs[0]
}
p := lib.GetParams{
Path: path,
Selector: o.Selector,
Expand Down
5 changes: 1 addition & 4 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ type LogOptions struct {

// Complete adds any missing configuration that can only be added just before calling Run
func (o *LogOptions) Complete(f Factory, args []string) (err error) {
if len(args) > 0 {
o.Ref = args[0]
}

o.Ref, err = GetDatasetRefString(f, args, 0)
if err != nil {
return err
}
Expand Down
8 changes: 0 additions & 8 deletions cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,6 @@ func (o *QriOptions) ProfileMethods() (m *lib.ProfileMethods, err error) {
return lib.NewProfileMethods(o.inst), nil
}

// SelectionRequests creates a lib.SelectionRequests from internal state
func (o *QriOptions) SelectionRequests() (*lib.SelectionRequests, error) {
if err := o.Init(); err != nil {
return nil, err
}
return lib.NewSelectionRequests(o.inst.Repo(), o.inst.RPC()), nil
}

// SearchRequests generates a lib.SearchRequests from internal state
func (o *QriOptions) SearchRequests() (*lib.SearchRequests, error) {
if err := o.Init(); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ type RenderOptions struct {

// Complete adds any missing configuration that can only be added just before calling Run
func (o *RenderOptions) Complete(f Factory, args []string) (err error) {
if len(args) > 0 {
o.Ref = args[0]
o.Ref, err = GetDatasetRefString(f, args, 0)
if err != nil {
return err
}
o.RenderRequests, err = f.RenderRequests()
return
Expand Down
95 changes: 81 additions & 14 deletions cmd/use.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

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

// FileSelectedRefs stores selection, is copied from github.com/qri-io/qri/repo/fs/files.go
const FileSelectedRefs = "/selected_refs.json"

// NewUseCommand creates a new `qri search` command that searches for datasets
func NewUseCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
o := &UseOptions{IOStreams: ioStreams}
Expand Down Expand Up @@ -62,13 +69,13 @@ type UseOptions struct {
List bool
Clear bool

SelectionRequests *lib.SelectionRequests
QriRepoPath string
}

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

Expand All @@ -85,16 +92,21 @@ func (o *UseOptions) Validate() error {

// Run executes the search command
func (o *UseOptions) Run() (err error) {
var (
refs []repo.DatasetRef
res bool
)
var refs []repo.DatasetRef
fileSelectionPath := filepath.Join(o.QriRepoPath, FileSelectedRefs)

if o.List {
if err = o.SelectionRequests.SelectedRefs(&res, &refs); err != nil {
refs, err = readFile(fileSelectionPath)
if err != nil {
return err
}
} else if o.Clear {
err := writeFile(fileSelectionPath, refs)
if err != nil {
return err
}
} else if len(o.Refs) > 0 || o.Clear {
printInfo(o.Out, "cleared selected datasets")
} else if len(o.Refs) > 0 {
for _, refstr := range o.Refs {
ref, err := repo.ParseDatasetRef(refstr)
if err != nil {
Expand All @@ -103,18 +115,73 @@ func (o *UseOptions) Run() (err error) {
refs = append(refs, ref)
}

if err = o.SelectionRequests.SetSelectedRefs(&refs, &res); err != nil {
err := writeFile(fileSelectionPath, refs)
if err != nil {
return err
}

if len(refs) == 0 {
printInfo(o.Out, "cleared selected datasets")
return nil
}
}

for _, ref := range refs {
fmt.Fprintln(o.Out, ref.String())
}
return nil
}

// GetDatasetRefString returns the arg at the index, or otherwise the first selected reference
func GetDatasetRefString(f Factory, args []string, index int) (string, error) {
if index < len(args) {
return args[index], nil
}
refs, err := DefaultSelectedRefList(f)
if err != nil {
return "", err
}
if len(refs) == 0 {
// If selected_refs.json is empty or doesn't exist, not an error.
return "", nil
}
return refs[0], nil
}

// DefaultSelectedRefList returns the list of currently selected dataset references
func DefaultSelectedRefList(f Factory) ([]string, error) {
fileSelectionPath := filepath.Join(f.QriRepoPath(), FileSelectedRefs)

refs, err := readFile(fileSelectionPath)
if err != nil {
// If selected_refs.json is empty or doesn't exist, not an error.
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}

res := make([]string, 0, len(refs))
for _, r := range refs {
res = append(res, r.String())
}

return res, nil
}

// writeFile serializes the list of refs to a file at path
func writeFile(path string, refs []repo.DatasetRef) error {
data, err := json.Marshal(refs)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, os.ModePerm)
}

// readFile deserializes a list of refs from a file at path
func readFile(path string) ([]repo.DatasetRef, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
res := []repo.DatasetRef{}
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}
40 changes: 21 additions & 19 deletions cmd/use_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -48,13 +50,6 @@ func TestUseComplete(t *testing.T) {
ioReset(in, out, errs)
continue
}

if opt.SelectionRequests == nil {
t.Errorf("case %d, opt.SelectionRequests not set.", i)
ioReset(in, out, errs)
continue
}
ioReset(in, out, errs)
}
}

Expand Down Expand Up @@ -100,7 +95,20 @@ func TestUseRun(t *testing.T) {
streams, in, out, errs := ioes.NewTestIOStreams()
setNoColor(true)

f, err := NewTestFactory(nil)
tmpdir := filepath.Join(os.TempDir(), "qri_use_test")
//clean up if previous cleanup failed
if _, err := os.Stat(tmpdir); os.IsNotExist(err) {
if err := os.RemoveAll(tmpdir); err != nil {
t.Fatalf("failed to cleanup from previous test execution: %s", err.Error())
}
}
if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil {
t.Errorf("error creating test path: %s", err.Error())
return
}
defer os.RemoveAll(tmpdir)

_, err := NewTestFactory(nil)
if err != nil {
t.Errorf("error creating new test factory: %s", err)
return
Expand All @@ -121,18 +129,12 @@ func TestUseRun(t *testing.T) {
}

for i, c := range cases {
slr, err := f.SelectionRequests()
if err != nil {
t.Errorf("case %d, error creating dataset request: %s", i, err)
continue
}

opt := &UseOptions{
IOStreams: streams,
Refs: c.refs,
List: c.list,
Clear: c.clear,
SelectionRequests: slr,
IOStreams: streams,
Refs: c.refs,
List: c.list,
Clear: c.clear,
QriRepoPath: tmpdir,
}

err = opt.Run()
Expand Down
2 changes: 2 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func (o *ValidateOptions) Run() (err error) {
}
}

// TODO(dlong): Reenable `use` functionality for this command. Also, change Ref to a string.

p := &lib.ValidateDatasetParams{
Ref: ref,
// TODO: restore
Expand Down
18 changes: 5 additions & 13 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,11 @@ func (r *DatasetRequests) Get(p *GetParams, res *GetResult) (err error) {
ref := &repo.DatasetRef{}

if p.Path == "" {
// Handle `qri use` to get the current default dataset.
if err = DefaultSelectedRef(r.node.Repo, ref); err != nil {
return
}
} else {
*ref, err = repo.ParseDatasetRef(p.Path)
if err != nil {
return fmt.Errorf("'%s' is not a valid dataset reference", p.Path)
}
return repo.ErrEmptyRef
}
*ref, err = repo.ParseDatasetRef(p.Path)
if err != nil {
return fmt.Errorf("'%s' is not a valid dataset reference", p.Path)
}
if err = repo.CanonicalizeDatasetRef(r.node.Repo, ref); err != nil {
return
Expand Down Expand Up @@ -554,10 +550,6 @@ func (r *DatasetRequests) Validate(p *ValidateDatasetParams, errors *[]jsonschem
return r.cli.Call("DatasetRequests.Validate", p, errors)
}

if err = DefaultSelectedRef(r.node.Repo, &p.Ref); err != nil {
return
}

// TODO: restore validating data from a URL
// if p.URL != "" && ref.IsEmpty() && o.Schema == nil {
// return (lib.NewError(ErrBadArgs, "if you are validating data from a url, please include a dataset name or supply the --schema flag with a file path that Qri can validate against"))
Expand Down
Loading

0 comments on commit 8286730

Please sign in to comment.