Skip to content

Commit

Permalink
fix(get): fix get command not accepting ds refs
Browse files Browse the repository at this point in the history
ended up writing a quick little "is this a dataset field?" regex. might be useful elsewhere

closes #492
  • Loading branch information
b5 committed Jul 19, 2018
1 parent 0459b6f commit fc2a2b2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"regexp"

"github.com/qri-io/qri/lib"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -28,7 +30,7 @@ func NewGetCommand(f Factory, ioStreams IOStreams) *cobra.Command {
return cmd
}

// GetOptions encapsulates state for the search command
// GetOptions encapsulates state for the get command
type GetOptions struct {
IOStreams

Expand All @@ -40,17 +42,23 @@ type GetOptions struct {
DatasetRequests *lib.DatasetRequests
}

// isDatasetField checks if a string is a dataset field or not
var isDatasetField = regexp.MustCompile("(?i)commit|structure|body|meta|viz|transform")

// Complete adds any missing configuration that can only be added just before calling Run
func (o *GetOptions) Complete(f Factory, args []string) (err error) {
if len(args) != 0 {
o.Path = args[0]
o.Refs = args[1:]
if len(args) > 0 {
if isDatasetField.MatchString(args[0]) {
o.Path = args[0]
args = args[1:]
}
}
o.Refs = args
o.DatasetRequests, err = f.DatasetRequests()
return
}

// Run executes the search command
// Run executes the get command
func (o *GetOptions) Run() (err error) {
var refs []repo.DatasetRef
for _, refstr := range o.Refs {
Expand Down
63 changes: 63 additions & 0 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"testing"
)

func TestGetComplete(t *testing.T) {
streams, in, out, errs := NewTestIOStreams()
setNoColor(true)

f, err := NewTestFactory(nil)
if err != nil {
t.Errorf("error creating new test factory: %s", err)
return
}

cases := []struct {
args []string
path string
refs []string
err string
}{
{[]string{}, "", []string{}, ""},
{[]string{"one arg"}, "", []string{"one arg"}, ""},
{[]string{"commit", "peer/ds"}, "commit", []string{"peer/ds"}, ""},
{[]string{"peer/ds_two", "peer/ds"}, "", []string{"peer/ds_two", "peer/ds"}, ""},
{[]string{"foo", "peer/ds"}, "", []string{"foo", "peer/ds"}, ""},
{[]string{"structure"}, "structure", []string{}, ""},
}

for i, c := range cases {
opt := &GetOptions{
IOStreams: streams,
}

opt.Complete(f, c.args)

if c.err != errs.String() {
t.Errorf("case %d, error mismatch. Expected: '%s', Got: '%s'", i, c.err, errs.String())
ioReset(in, out, errs)
continue
}

if !testSliceEqual(c.refs, opt.Refs) {
t.Errorf("case %d, opt.Refs not set correctly. Expected: '%s', Got: '%s'", i, c.refs, opt.Refs)
ioReset(in, out, errs)
continue
}

if c.path != opt.Path {
t.Errorf("case %d, opt.Path not set correctly. Expected: '%s', Got: '%s'", i, c.path, opt.Path)
ioReset(in, out, errs)
continue
}

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

0 comments on commit fc2a2b2

Please sign in to comment.