github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/timeouts.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/drycc/controller-sdk-go/api"
     8  	"github.com/drycc/controller-sdk-go/config"
     9  )
    10  
    11  // TimeoutsList lists an app's timeouts.
    12  func (d *DryccCmd) TimeoutsList(appID string) error {
    13  	s, appID, err := load(d.ConfigFile, appID)
    14  
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	config, err := config.List(s.Client, appID)
    20  	if d.checkAPICompatibility(s.Client, err) != nil {
    21  		return err
    22  	}
    23  
    24  	if len(config.Timeout) == 0 {
    25  		d.Println("Default (30 sec) or controlled by drycc controller.")
    26  	} else {
    27  		table := d.getDefaultFormatTable([]string{"UUID", "OWNER", "PTYPE", "TIMEOUT"})
    28  		for _, key := range *sortKeys(config.Timeout) {
    29  			table.Append([]string{
    30  				config.UUID,
    31  				config.Owner,
    32  				key,
    33  				fmt.Sprintf("%v", config.Timeout[key]),
    34  			})
    35  		}
    36  		table.Render()
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  // TimeoutsSet sets an app's timeouts.
    43  func (d *DryccCmd) TimeoutsSet(appID string, timeouts []string) error {
    44  	s, appID, err := load(d.ConfigFile, appID)
    45  
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	timeoutsMap, err := parseTimeouts(timeouts)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	d.Print("Applying timeouts... ")
    56  
    57  	quit := progress(d.WOut)
    58  	configObj := api.Config{}
    59  
    60  	configObj.Timeout = timeoutsMap
    61  
    62  	_, err = config.Set(s.Client, appID, configObj)
    63  	quit <- true
    64  	<-quit
    65  	if d.checkAPICompatibility(s.Client, err) != nil {
    66  		return err
    67  	}
    68  
    69  	d.Print("done\n\n")
    70  
    71  	return d.TimeoutsList(appID)
    72  }
    73  
    74  // TimeoutsUnset removes an app's timeouts.
    75  func (d *DryccCmd) TimeoutsUnset(appID string, timeouts []string) error {
    76  	s, appID, err := load(d.ConfigFile, appID)
    77  
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	d.Print("Applying timeouts... ")
    83  
    84  	quit := progress(d.WOut)
    85  
    86  	configObj := api.Config{}
    87  
    88  	valuesMap := make(map[string]interface{})
    89  
    90  	for _, timeout := range timeouts {
    91  		valuesMap[timeout] = nil
    92  	}
    93  
    94  	configObj.Timeout = valuesMap
    95  
    96  	_, err = config.Set(s.Client, appID, configObj)
    97  	quit <- true
    98  	<-quit
    99  	if d.checkAPICompatibility(s.Client, err) != nil {
   100  		return err
   101  	}
   102  
   103  	d.Print("done\n\n")
   104  
   105  	return d.TimeoutsList(appID)
   106  }
   107  
   108  func parseTimeouts(timeouts []string) (map[string]interface{}, error) {
   109  	timeoutsMap := make(map[string]interface{})
   110  
   111  	for _, timeout := range timeouts {
   112  		key, value, err := parseTimeout(timeout)
   113  
   114  		if err != nil {
   115  			return nil, err
   116  		}
   117  
   118  		timeoutsMap[key] = value
   119  	}
   120  
   121  	return timeoutsMap, nil
   122  }
   123  
   124  func parseTimeout(timeout string) (string, string, error) {
   125  	regex := regexp.MustCompile("^([a-z0-9]+(?:-[a-z0-9]+)*)=([0-9]+)$")
   126  
   127  	if !regex.MatchString(timeout) {
   128  		return "", "", fmt.Errorf(`%s doesn't fit format type=#
   129  Examples: web=30 worker=300`, timeout)
   130  	}
   131  
   132  	capture := regex.FindStringSubmatch(timeout)
   133  
   134  	return capture[1], capture[2], nil
   135  }