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

     1  package parser
     2  
     3  import (
     4  	docopt "github.com/docopt/docopt-go"
     5  	"github.com/drycc/workflow-cli/cmd"
     6  )
     7  
     8  // Autoscale displays all relevant commands for `drycc autoscale`.
     9  func Autoscale(argv []string, cmdr cmd.Commander) error {
    10  	usage := `
    11  Valid commands for autoscale:
    12  
    13  autoscale:list   list autoscale options of an application
    14  autoscale:set    turn on autoscale for an app
    15  autoscale:unset  turn off autoscale for an app
    16  
    17  Use 'drycc help [command]' to learn more.
    18  `
    19  
    20  	switch argv[0] {
    21  	case "autoscale:list":
    22  		return autoscaleList(argv, cmdr)
    23  	case "autoscale:set":
    24  		return autoscaleSet(argv, cmdr)
    25  	case "autoscale:unset":
    26  		return autoscaleUnset(argv, cmdr)
    27  	default:
    28  		if printHelp(argv, usage) {
    29  			return nil
    30  		}
    31  
    32  		if argv[0] == "autoscale" {
    33  			argv[0] = "autoscale:list"
    34  			return autoscaleList(argv, cmdr)
    35  		}
    36  
    37  		PrintUsage(cmdr)
    38  		return nil
    39  	}
    40  }
    41  
    42  func autoscaleList(argv []string, cmdr cmd.Commander) error {
    43  	usage := `
    44  Prints a list of autoscale options for the application.
    45  
    46  Usage: drycc autoscale:list [options]
    47  
    48  Options:
    49    -a --app=<app>
    50      the uniquely identifiable name for the application.
    51  `
    52  
    53  	args, err := docopt.ParseArgs(usage, argv, "")
    54  
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	return cmdr.AutoscaleList(safeGetString(args, "--app"))
    60  }
    61  
    62  func autoscaleSet(argv []string, cmdr cmd.Commander) error {
    63  	usage := `
    64  Set autoscale option per process type for an app.
    65  
    66  Usage: drycc autoscale:set <process-type> --min=<min> --max=<max> --cpu-percent=<percent> [options]
    67  
    68  Arguments:
    69    <process-type>
    70      the process type to add to the application's autoscale settings.
    71    --min=<min>
    72  	minimum replicas to keep around
    73    --max=<max>
    74  	max replicas to scale up to
    75    --cpu-percent=<cpu-percent>
    76  	target CPU utilization
    77  
    78  Options:
    79    -a --app=<app>
    80      the uniquely identifiable name of the application.
    81  `
    82  
    83  	args, err := docopt.ParseArgs(usage, argv, "")
    84  
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	processType := args["<process-type>"].(string)
    90  	app := safeGetString(args, "--app")
    91  	min := safeGetInt(args, "--min")
    92  	max := safeGetInt(args, "--max")
    93  	CPUPercent := safeGetInt(args, "--cpu-percent")
    94  
    95  	return cmdr.AutoscaleSet(app, processType, min, max, CPUPercent)
    96  }
    97  
    98  func autoscaleUnset(argv []string, cmdr cmd.Commander) error {
    99  	usage := `
   100  Unset autoscale per process type for an app.
   101  
   102  Usage: drycc autoscale:unset <process-type> [options]
   103  
   104  Arguments:
   105    <process-type>
   106      the process type to remove from the application's autoscale settings.
   107  
   108  Options:
   109    -a --app=<app>
   110      the uniquely identifiable name of the application.
   111  `
   112  
   113  	args, err := docopt.ParseArgs(usage, argv, "")
   114  
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	processType := args["<process-type>"].(string)
   120  	app := safeGetString(args, "--app")
   121  
   122  	return cmdr.AutoscaleUnset(app, processType)
   123  }