Skip to content

Commit

Permalink
refactor(cmd): change limit and offset flags to page and `page-…
Browse files Browse the repository at this point in the history
…size`

BREAKING CHANGE:
On the cli, all `limit` and `offset` flags have been changed to `page` and `page-size`.
  • Loading branch information
ramfox committed Apr 29, 2019
1 parent 2e623db commit 62d5da8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestCommandsIntegration(t *testing.T) {
// "qri registry unpublish me/movies",
// "qri registry publish me/movies",
"qri rename me/movies me/movie",
"qri get body --limit=1 --format=cbor me/movie",
"qri get body --page-size=1 --format=cbor me/movie",
"qri validate me/movie",
"qri remove me/movie --revisions=all",
fmt.Sprintf("qri export --blank -o=%s/blank_dataset.yaml", path),
Expand Down
35 changes: 19 additions & 16 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ dataset and its fields.`,

cmd.Flags().StringVarP(&o.Format, "format", "f", "", "set output format [json, yaml]")
cmd.Flags().BoolVar(&o.Concise, "concise", false, "print output without indentation, only applies to json format")
cmd.Flags().IntVarP(&o.Limit, "limit", "l", -1, "for body, limit how many entries to get")
cmd.Flags().IntVarP(&o.Offset, "offset", "s", -1, "for body, offset at which to get entries")
cmd.Flags().IntVar(&o.PageSize, "page-size", -1, "for body, limit how many entries to get per page")
cmd.Flags().IntVar(&o.Page, "page", -1, "for body, page at which to get entries")
cmd.Flags().BoolVarP(&o.All, "all", "a", true, "for body, whether to get all entries")

return cmd
Expand All @@ -65,9 +65,9 @@ type GetOptions struct {
Format string
Concise bool

Limit int
Offset int
All bool
Page int
PageSize int
All bool

DatasetRequests *lib.DatasetRequests
}
Expand All @@ -89,20 +89,20 @@ func (o *GetOptions) Complete(f Factory, args []string) (err error) {
}

if o.Selector == "body" {
// if we have a limit, but not offset, assume an offset of 0
if o.Limit != -1 && o.Offset == -1 {
o.Offset = 0
// if we have a PageSize, but not Page, assume an Page of 1
if o.PageSize != -1 && o.Page == -1 {
o.Page = 1
}
// set all to false if limit or offset values are provided
if o.Limit != -1 || o.Offset != -1 {
// set all to false if PageSize or Page values are provided
if o.PageSize != -1 || o.Page != -1 {
o.All = false
}
} else {
if o.Limit != -1 {
return fmt.Errorf("can only use --limit flag when getting body")
if o.PageSize != -1 {
return fmt.Errorf("can only use --page-size flag when getting body")
}
if o.Offset != -1 {
return fmt.Errorf("can only use --offset flag when getting body")
if o.Page != -1 {
return fmt.Errorf("can only use --page flag when getting body")
}
if !o.All {
return fmt.Errorf("can only use --all flag when getting body")
Expand All @@ -122,13 +122,16 @@ func (o *GetOptions) Run() (err error) {
}
}

// convert Page and PageSize to Limit and Offset
listParams := lib.NewListParams("", o.Page, o.PageSize)

p := lib.GetParams{
Path: path,
Selector: o.Selector,
Format: o.Format,
Concise: o.Concise,
Offset: o.Offset,
Limit: o.Limit,
Offset: listParams.Offset,
Limit: listParams.Limit,
All: o.All,
}
res := lib.GetResult{}
Expand Down
15 changes: 9 additions & 6 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ must have ` + "`qri connect`" + ` running in a separate terminal window.`,
}

cmd.Flags().StringVarP(&o.Format, "format", "f", "", "set output format [json]")
cmd.Flags().IntVarP(&o.Limit, "limit", "l", 25, "limit results, default 25")
cmd.Flags().IntVarP(&o.Offset, "offset", "o", 0, "offset results, default 0")
cmd.Flags().IntVar(&o.PageSize, "page-size", 25, "page size of results, default 25")
cmd.Flags().IntVar(&o.Page, "page", 1, "page number results, default 1")
cmd.Flags().BoolVarP(&o.Published, "published", "p", false, "list only published datasets")
cmd.Flags().BoolVarP(&o.ShowNumVersions, "num-versions", "n", false, "show number of versions")
cmd.Flags().StringVar(&o.Peername, "peer", "", "peer whose datasets to list")
Expand All @@ -65,8 +65,8 @@ type ListOptions struct {
ioes.IOStreams

Format string
Limit int
Offset int
PageSize int
Page int
Term string
Peername string
Published bool
Expand All @@ -87,12 +87,15 @@ func (o *ListOptions) Complete(f Factory, args []string) (err error) {
// Run executes the list command
func (o *ListOptions) Run() (err error) {

// convert Page and PageSize to Limit and Offset
listParams := lib.NewListParams("", o.Page, o.PageSize)

refs := []repo.DatasetRef{}
p := &lib.ListParams{
Term: o.Term,
Peername: o.Peername,
Limit: o.Limit,
Offset: o.Offset,
Limit: listParams.Limit,
Offset: listParams.Offset,
Published: o.Published,
ShowNumVersions: o.ShowNumVersions,
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ You must have ` + "`qri connect`" + ` running in another terminal.`,

list.Flags().BoolVarP(&o.Cached, "cached", "c", false, "show peers that aren't online, but previously seen")
list.Flags().StringVarP(&o.Network, "network", "n", "", "specify network to show peers from [ipfs]")
list.Flags().IntVarP(&o.Limit, "limit", "l", 200, "limit max number of peers to show")
list.Flags().IntVarP(&o.Offset, "offset", "s", 0, "number of peers to skip during listing")
// TODO (ramfox): when we determine the best way to order and paginate peers, restore!
// list.Flags().IntVar(&o.PageSize, "page-size", 200, "max page size number of peers to show, default 200")
// list.Flags().IntVar(&o.Page, "page", 1, "page number of peers, default 1")

cmd.AddCommand(info, list, connect, disconnect)

Expand All @@ -158,8 +159,8 @@ type PeersOptions struct {
Format string
Cached bool
Network string
Limit int
Offset int
PageSize int
Page int

UsingRPC bool
PeerRequests *lib.PeerRequests
Expand Down Expand Up @@ -210,9 +211,13 @@ func (o *PeersOptions) Info() (err error) {

// List shows a list of peers
func (o *PeersOptions) List() (err error) {

// convert Page and PageSize to Limit and Offset
listParams := lib.NewListParams("", o.Page, o.PageSize)

if o.Network == "ipfs" {
res := []string{}
if err := o.PeerRequests.ConnectedIPFSPeers(&o.Limit, &res); err != nil {
if err := o.PeerRequests.ConnectedIPFSPeers(&listParams.Limit, &res); err != nil {
return err
}

Expand All @@ -227,8 +232,8 @@ func (o *PeersOptions) List() (err error) {
}

p := &lib.PeerListParams{
Limit: o.Limit,
Offset: o.Offset,
Limit: listParams.Limit,
Offset: listParams.Offset,
Cached: o.Cached,
}
res := []*config.ProfilePod{}
Expand All @@ -242,7 +247,7 @@ func (o *PeersOptions) List() (err error) {
// it make sense to try out printing to less here
// where limit and offset don't mean as much
buf := bytes.Buffer{}
fmt.Fprintln(&buf, "Cached Qri Peers List:\n")
fmt.Fprintln(&buf, "Cached Qri Peers List:")
for i, peer := range res {
printPeerInfoNoColor(&buf, i, peer)
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Any dataset that has been published to the registry is available for search.`,
}

cmd.Flags().StringVarP(&o.Format, "format", "f", "", "set output format [json]")
cmd.Flags().IntVarP(&o.Limit, "limit", "l", 25, "limit results, default 25")
cmd.Flags().IntVarP(&o.Offset, "offset", "o", 0, "offset results, default 0")
cmd.Flags().IntVar(&o.PageSize, "page-size", 25, "page size of results, default 25")
cmd.Flags().IntVar(&o.Page, "page", 1, "page number of results, default 1")

return cmd
}
Expand All @@ -51,9 +51,8 @@ type SearchOptions struct {
Query string
SearchRequests *lib.SearchRequests
Format string
// TODO: add support for specifying limit and offset
Limit int
Offset int
PageSize int
Page int
// Reindex bool
}

Expand Down Expand Up @@ -81,10 +80,13 @@ func (o *SearchOptions) Run() (err error) {

// TODO: add reindex option back in

// convert Page and PageSize to Limit and Offset
listParams := lib.NewListParams("", o.Page, o.PageSize)

p := &lib.SearchParams{
QueryString: o.Query,
Limit: o.Limit,
Offset: o.Offset,
Limit: listParams.Limit,
Offset: listParams.Offset,
}

results := []lib.SearchResult{}
Expand Down

0 comments on commit 62d5da8

Please sign in to comment.