Skip to content

Commit fc2a2b2

Browse files
committed
fix(get): fix get command not accepting ds refs
ended up writing a quick little "is this a dataset field?" regex. might be useful elsewhere closes #492
1 parent 0459b6f commit fc2a2b2

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

cmd/get.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"regexp"
5+
46
"github.com/qri-io/qri/lib"
57
"github.com/qri-io/qri/repo"
68
"github.com/spf13/cobra"
@@ -28,7 +30,7 @@ func NewGetCommand(f Factory, ioStreams IOStreams) *cobra.Command {
2830
return cmd
2931
}
3032

31-
// GetOptions encapsulates state for the search command
33+
// GetOptions encapsulates state for the get command
3234
type GetOptions struct {
3335
IOStreams
3436

@@ -40,17 +42,23 @@ type GetOptions struct {
4042
DatasetRequests *lib.DatasetRequests
4143
}
4244

45+
// isDatasetField checks if a string is a dataset field or not
46+
var isDatasetField = regexp.MustCompile("(?i)commit|structure|body|meta|viz|transform")
47+
4348
// Complete adds any missing configuration that can only be added just before calling Run
4449
func (o *GetOptions) Complete(f Factory, args []string) (err error) {
45-
if len(args) != 0 {
46-
o.Path = args[0]
47-
o.Refs = args[1:]
50+
if len(args) > 0 {
51+
if isDatasetField.MatchString(args[0]) {
52+
o.Path = args[0]
53+
args = args[1:]
54+
}
4855
}
56+
o.Refs = args
4957
o.DatasetRequests, err = f.DatasetRequests()
5058
return
5159
}
5260

53-
// Run executes the search command
61+
// Run executes the get command
5462
func (o *GetOptions) Run() (err error) {
5563
var refs []repo.DatasetRef
5664
for _, refstr := range o.Refs {

cmd/get_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetComplete(t *testing.T) {
8+
streams, in, out, errs := NewTestIOStreams()
9+
setNoColor(true)
10+
11+
f, err := NewTestFactory(nil)
12+
if err != nil {
13+
t.Errorf("error creating new test factory: %s", err)
14+
return
15+
}
16+
17+
cases := []struct {
18+
args []string
19+
path string
20+
refs []string
21+
err string
22+
}{
23+
{[]string{}, "", []string{}, ""},
24+
{[]string{"one arg"}, "", []string{"one arg"}, ""},
25+
{[]string{"commit", "peer/ds"}, "commit", []string{"peer/ds"}, ""},
26+
{[]string{"peer/ds_two", "peer/ds"}, "", []string{"peer/ds_two", "peer/ds"}, ""},
27+
{[]string{"foo", "peer/ds"}, "", []string{"foo", "peer/ds"}, ""},
28+
{[]string{"structure"}, "structure", []string{}, ""},
29+
}
30+
31+
for i, c := range cases {
32+
opt := &GetOptions{
33+
IOStreams: streams,
34+
}
35+
36+
opt.Complete(f, c.args)
37+
38+
if c.err != errs.String() {
39+
t.Errorf("case %d, error mismatch. Expected: '%s', Got: '%s'", i, c.err, errs.String())
40+
ioReset(in, out, errs)
41+
continue
42+
}
43+
44+
if !testSliceEqual(c.refs, opt.Refs) {
45+
t.Errorf("case %d, opt.Refs not set correctly. Expected: '%s', Got: '%s'", i, c.refs, opt.Refs)
46+
ioReset(in, out, errs)
47+
continue
48+
}
49+
50+
if c.path != opt.Path {
51+
t.Errorf("case %d, opt.Path not set correctly. Expected: '%s', Got: '%s'", i, c.path, opt.Path)
52+
ioReset(in, out, errs)
53+
continue
54+
}
55+
56+
if opt.DatasetRequests == nil {
57+
t.Errorf("case %d, opt.DatasetRequests not set.", i)
58+
ioReset(in, out, errs)
59+
continue
60+
}
61+
ioReset(in, out, errs)
62+
}
63+
}

0 commit comments

Comments
 (0)