<commit_before>/*
 * Copyright (C) 2017-2018 Alibaba Group Holding Limited
 */
package openapi

import (
	"github.com/aliyun/aliyun-cli/cli"
	"time"
	"fmt"
	"github.com/aliyun/aliyun-cli/i18n"
	"strconv"
)

var WaiterFlag = &cli.Flag {Category:"helper",
	Name: "waiter",
	AssignedMode: cli.AssignedRepeatable,
	Short: i18n.T(
		"use `--waiter expr=<jmesPath> to=<value>` to pull api until result equal to expected value",
		"使用 `--waiter expr=<jmesPath> to=<value>` 来轮询调用OpenAPI，知道返回期望的值"),
	Long: i18n.T(
		"",
		""),
	Fields: []cli.Field {
		{Key:"expr", Required:true, Short: i18n.T("", "")},
		{Key:"to", Required:true, Short: i18n.T("", "")},
		{Key:"timeout",DefaultValue:"180", Short:i18n.T("", "")},
		{Key:"interval",DefaultValue:"5", Short:i18n.T("","")},
	},
	ExcludeWith:[]string{"pager"},
}

type Waiter struct {
	expr string
	to string
//	timeout  time.Duration	TODO use Flag.Field to validate
//	interval time.Duration  TODO use Flag.Field to validate
}

func GetWaiter() *Waiter {
	if !WaiterFlag.IsAssigned() {
		return nil
	}

	waiter := &Waiter {
	}
	waiter.expr, _ = WaiterFlag.GetFieldValue("expr")
	waiter.to, _ = WaiterFlag.GetFieldValue("to")
	//waiter.timeout = time.Duration(time.Second * 180)
	//waiter.interval = time.Duration(time.Second * 5)

	return waiter
}

func (a *Waiter) CallWith(invoker Invoker) (string, error) {
	//
	// timeout is 1-180 seconds
	timeout := time.Duration(time.Second * 180)
	if s, ok := WaiterFlag.GetFieldValue("timeout"); ok {
		if n, err := strconv.Atoi(s); err == nil {
			if n <= 0 && n > 180 {
				return "", fmt.Errorf("--waiter timeout=%s must between 1-180 (seconds)", s)
			}
			timeout = time.Duration(time.Second * time.Duration(n))
		} else {
			return "", fmt.Errorf("--waiter timeout=%s must be integer", s)<commit_msg>wait time<commit_after>/*
 * Copyright (C) 2017-2018 Alibaba Group Holding Limited
 */
package openapi

import (
	"github.com/aliyun/aliyun-cli/cli"
	"time"
	"fmt"
	"github.com/aliyun/aliyun-cli/i18n"
	"strconv"
)

var WaiterFlag = &cli.Flag {Category:"helper",
	Name: "waiter",
	AssignedMode: cli.AssignedRepeatable,
	Short: i18n.T(
		"use `--waiter expr=<jmesPath> to=<value>` to pull api until result equal to expected value",
		"使用 `--waiter expr=<jmesPath> to=<value>` 来轮询调用OpenAPI，知道返回期望的值"),
	Long: i18n.T(
		"",
		""),
	Fields: []cli.Field {
		{Key:"expr", Required:true, Short: i18n.T("", "")},
		{Key:"to", Required:true, Short: i18n.T("", "")},
		{Key:"timeout",DefaultValue:"180", Short:i18n.T("", "")},
		{Key:"interval",DefaultValue:"5", Short:i18n.T("","")},
	},
	ExcludeWith:[]string{"pager"},
}

type Waiter struct {
	expr string
	to string
//	timeout  time.Duration	TODO use Flag.Field to validate
//	interval time.Duration  TODO use Flag.Field to validate
}

func GetWaiter() *Waiter {
	if !WaiterFlag.IsAssigned() {
		return nil
	}

	waiter := &Waiter {
	}
	waiter.expr, _ = WaiterFlag.GetFieldValue("expr")
	waiter.to, _ = WaiterFlag.GetFieldValue("to")
	//waiter.timeout = time.Duration(time.Second * 180)
	//waiter.interval = time.Duration(time.Second * 5)

	return waiter
}

func (a *Waiter) CallWith(invoker Invoker) (string, error) {
	//
	// timeout is 1-180 seconds
	timeout := time.Duration(time.Second * 180)
	if s, ok := WaiterFlag.GetFieldValue("timeout"); ok {
		if n, err := strconv.Atoi(s); err == nil {
			if n <= 0 && n > 600 {
				return "", fmt.Errorf("--waiter timeout=%s must between 1-600 (seconds)", s)
			}
			timeout = time.Duration(time.Second * time.Duration(n))
		} else {
			return "", fmt.Errorf("--waiter timeout=%s must be integer", s)
{'fraction_non_alphanumeric': 0.18400447427293065, 'fraction_numerical': 0.025167785234899327, 'mean_word_length': 4.124641833810888, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3553086', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1376, 'n_words': 304}