github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/scale/scale.go (about)

     1  package scale
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
     9  	"github.com/henvic/wedeploycli/color"
    10  	"github.com/henvic/wedeploycli/command/internal/we"
    11  	"github.com/henvic/wedeploycli/fancy"
    12  	"github.com/henvic/wedeploycli/isterm"
    13  	"github.com/henvic/wedeploycli/list"
    14  	"github.com/henvic/wedeploycli/services"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  // ScaleCmd is used for getting scale
    19  var ScaleCmd = &cobra.Command{
    20  	Use:   "scale",
    21  	Short: "Configure number of instances for services",
    22  	RunE:  scaleRun,
    23  	Example: `  lcp scale --project chat --service data 3
    24    lcp scale --project chat --service data --remote lfr-cloud 5
    25    lcp scale --url data-chat.lfr.cloud 1`,
    26  }
    27  
    28  var setupHost = cmdflagsfromhost.SetupHost{
    29  	Pattern: cmdflagsfromhost.FullHostPattern,
    30  
    31  	Requires: cmdflagsfromhost.Requires{
    32  		Auth:    true,
    33  		Project: true,
    34  		Service: true,
    35  	},
    36  
    37  	PromptMissingProject: true,
    38  	PromptMissingService: true,
    39  
    40  	ListExtraDetails: list.Instances,
    41  }
    42  
    43  func init() {
    44  	setupHost.Init(ScaleCmd)
    45  }
    46  
    47  func getInstancesNumber(cmd *cobra.Command, args []string) (string, error) {
    48  	if len(args) != 0 || !isterm.Check() {
    49  		err := cobra.ExactArgs(1)(cmd, args)
    50  		return args[0], err
    51  	}
    52  
    53  	fmt.Println(fancy.Question("Number of instances"))
    54  
    55  	var sscale, err = fancy.Prompt()
    56  
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  
    61  	return sscale, nil
    62  }
    63  
    64  type scale struct {
    65  	ctx context.Context
    66  
    67  	project string
    68  	service string
    69  	current int
    70  }
    71  
    72  func (s *scale) do() (err error) {
    73  	wectx := we.Context()
    74  	servicesClient := services.New(wectx)
    75  	err = servicesClient.Scale(s.ctx, s.project, s.service, services.Scale{
    76  		Current: s.current,
    77  	})
    78  
    79  	if err == nil {
    80  		var maybePlural string
    81  
    82  		if s.current > 1 {
    83  			maybePlural = "s"
    84  		}
    85  
    86  		fmt.Printf(color.Format(color.FgHiBlack,
    87  			"Scaling service \"")+
    88  			"%s"+color.Format(color.FgHiBlack,
    89  			"\" on project \"")+
    90  			"%s"+
    91  			color.Format(color.FgHiBlack, "\" on ")+
    92  			wectx.InfrastructureDomain()+
    93  			color.Format(color.FgHiBlack, " to ")+
    94  			color.Format(color.FgMagenta, color.Bold, s.current)+
    95  			color.Format(color.FgHiBlack, " instance%s.", maybePlural)+
    96  			"\n",
    97  			s.service,
    98  			s.project)
    99  	}
   100  
   101  	return err
   102  }
   103  
   104  func (s *scale) checkServiceExists() (err error) {
   105  	servicesClient := services.New(we.Context())
   106  	_, err = servicesClient.Get(s.ctx, s.project, s.service)
   107  	return err
   108  }
   109  
   110  func isValidNumberOfInstances(instances string) bool {
   111  	n, err := strconv.Atoi(instances)
   112  	return err == nil && n > 0
   113  }
   114  
   115  func scaleRun(cmd *cobra.Command, args []string) error {
   116  	if err := setupHost.Process(context.Background(), we.Context()); err != nil {
   117  		return err
   118  	}
   119  
   120  	sscale, err := getInstancesNumber(cmd, args)
   121  
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	if !isValidNumberOfInstances(sscale) {
   127  		return fmt.Errorf(`"%v" isn't a valid number for instances`, sscale)
   128  	}
   129  
   130  	current, err := strconv.Atoi(sscale)
   131  
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	var s = &scale{
   137  		ctx: context.Background(),
   138  
   139  		project: setupHost.Project(),
   140  		service: setupHost.Service(),
   141  		current: current,
   142  	}
   143  
   144  	if err := s.checkServiceExists(); err != nil {
   145  		return err
   146  	}
   147  
   148  	return s.do()
   149  }