Skip to content

Commit

Permalink
fix(list): Improve usability of list command
Browse files Browse the repository at this point in the history
I often find myself typing `qri list me/my_dataset` when I should be using
`qri get me/my_dataset`. Currently this is an error, because `list` expects
its parameter to be only a peername (whose datasets to list), but we can do
better. Instead, split the parameter into peername and a dataset name to be
used like a filter over the list results.
  • Loading branch information
dustmop committed Feb 15, 2019
1 parent 994730d commit d773363
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"strings"

"github.com/qri-io/dataset"
"github.com/qri-io/ioes"
Expand Down Expand Up @@ -105,9 +106,17 @@ func (o *ListOptions) Run() (err error) {
return fmt.Errorf("unrecognized format: %s", o.Format)
}
} else {
// if user provides "me/my_dataset", split into peername="me" and name="my_dataset"
peername := o.Peername
dsName := ""
parts := strings.Split(peername, "/")
if len(parts) > 1 {
peername = parts[0]
dsName = parts[1]
}

p := &lib.ListParams{
Peername: o.Peername,
Peername: peername,
Limit: o.Limit,
Offset: o.Offset,
}
Expand All @@ -116,19 +125,34 @@ func (o *ListOptions) Run() (err error) {
return err
}

replace := make([]repo.DatasetRef, 0)
for _, ref := range refs {
// remove profileID so names print pretty
ref.ProfileID = ""
// if there's a dsName that restricts the list operation, append matches
if dsName != "" && dsName == ref.Name {
replace = append(replace, ref)
}
}

// if there's a dsName that restricts the list operation, only show that dataset
if dsName != "" {
refs = replace
}

if len(refs) == 0 {
if dsName != "" {
printInfo(o.Out, "%s has no datasets that match \"%s\"", peername, dsName)
} else {
printInfo(o.Out, "%s has no datasets", peername)
}
return
}

switch o.Format {
case "":
if len(refs) == 0 {
printInfo(o.Out, "%s has no datasets", o.Peername)
} else {
for i, ref := range refs {
printDatasetRefInfo(o.Out, i+1, ref)
}
for i, ref := range refs {
printDatasetRefInfo(o.Out, i+1, ref)
}
case dataset.JSONDataFormat.String():
data, err := json.MarshalIndent(refs, "", " ")
Expand Down

0 comments on commit d773363

Please sign in to comment.