Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CLI util for list commands #443

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func newAdminShardManagementCommands() []cli.Command {
{
Name: "list_tasks",
Usage: "List tasks for given shard Id and task type",
Flags: append(
Flags: append(append(
getDBFlags(),
getFlagsForList()...),
cli.StringFlag{
Name: FlagTargetCluster,
Value: "active",
Expand Down
18 changes: 14 additions & 4 deletions tools/cli/adminCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,21 @@ func AdminListTasks(c *cli.Context) {
maxVis := time.Unix(0, maxVisFlag)

req := &persistence.GetTimerIndexTasksRequest{MinTimestamp: minVis, MaxTimestamp: maxVis}
tasks, err := executionManager.GetTimerIndexTasks(req)
if err != nil {
ErrorAndExit("Failed to get Timer Tasks", err)
paginationFunc := func(paginationToken []byte) ([]interface{}, []byte, error) {
req.NextPageToken = paginationToken
response, err := executionManager.GetTimerIndexTasks(req)
if err != nil {
return nil, nil, err
}
token := response.NextPageToken

var items []interface{}
for _, task := range response.Timers {
items = append(items, task)
}
return items, token, nil
}
prettyPrintJSONObject(tasks)
paginate(c, paginationFunc)
} else if category == commongenpb.TASK_CATEGORY_REPLICATION {
req := &persistence.GetReplicationTasksRequest{}
task, err := executionManager.GetReplicationTasks(req)
Expand Down
1 change: 0 additions & 1 deletion tools/cli/persistenceUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
// CreatePersistenceFactory returns an initialized persistence managers factory.
// The factory allows to easily initialize concrete persistence managers to execute commands against persistence layer
func CreatePersistenceFactory(c *cli.Context) persistenceClient.Factory {

defaultStore, err := CreateDefaultDBConfig(c)
if err != nil {
ErrorAndExit("CreatePersistenceFactory err", err)
Expand Down
27 changes: 27 additions & 0 deletions tools/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
sdkclient "go.temporal.io/temporal/client"

"github.com/temporalio/temporal/common/codec"
"github.com/temporalio/temporal/common/collection"
"github.com/temporalio/temporal/common/payload"
"github.com/temporalio/temporal/common/payloads"
"github.com/temporalio/temporal/common/rpc"
Expand Down Expand Up @@ -901,6 +902,32 @@ func showNextPage() bool {
return strings.Trim(input, " ") == ""
}

// paginate creates an interactive CLI mode to control the printing of items
func paginate(c *cli.Context, paginationFn collection.PaginationFn) error {
more := c.Bool(FlagMore)
pageSize := c.Int(FlagPageSize)
if pageSize == 0 {
pageSize = defaultPageSize
}
iter := collection.NewPagingIterator(paginationFn)

pageItemsCount := 0
for iter.HasNext() {
batch, err := iter.Next()
if err != nil {
return err
}

prettyPrintJSONObject(batch)
pageItemsCount++
if pageItemsCount%pageSize == 0 && (!more || !showNextPage()) {
break
}
}

return nil
}

// prompt will show input msg, then waiting user input y/yes to continue
func prompt(msg string, autoConfirm bool) {
reader := bufio.NewReader(os.Stdin)
Expand Down