github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/restart/restart.go (about) 1 package restart 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/henvic/wedeploycli/cmdflagsfromhost" 8 "github.com/henvic/wedeploycli/color" 9 "github.com/henvic/wedeploycli/command/internal/we" 10 "github.com/henvic/wedeploycli/services" 11 "github.com/spf13/cobra" 12 ) 13 14 // RestartCmd is used for getting restart 15 var RestartCmd = &cobra.Command{ 16 Use: "restart", 17 Short: "Restart services", 18 Args: cobra.NoArgs, 19 PreRunE: preRun, 20 RunE: restartRun, 21 Example: ` lcp restart --project chat --service data 22 ^ lcp restart --project chat --service data --remote lfr-cloud 23 ^ lcp restart --url data-chat.lfr.cloud`, 24 } 25 26 var setupHost = cmdflagsfromhost.SetupHost{ 27 Pattern: cmdflagsfromhost.FullHostPattern, 28 29 Requires: cmdflagsfromhost.Requires{ 30 Auth: true, 31 Project: true, 32 Service: true, 33 }, 34 35 PromptMissingProject: true, 36 PromptMissingService: true, 37 } 38 39 func init() { 40 setupHost.Init(RestartCmd) 41 42 // the --quiet parameter was removed 43 _ = RestartCmd.Flags().BoolP("quiet", "q", false, "") 44 _ = RestartCmd.Flags().MarkHidden("quiet") 45 } 46 47 type restart struct { 48 project string 49 service string 50 ctx context.Context 51 } 52 53 func (r *restart) do() (err error) { 54 wectx := we.Context() 55 servicesClient := services.New(wectx) 56 err = servicesClient.Restart(r.ctx, r.project, r.service) 57 58 if err == nil { 59 fmt.Printf(color.Format(color.FgHiBlack, 60 "Restarting service \"")+ 61 "%s"+color.Format(color.FgHiBlack, 62 "\" on project \"")+ 63 "%s"+ 64 color.Format(color.FgHiBlack, "\" on ")+ 65 wectx.InfrastructureDomain()+ 66 color.Format(color.FgHiBlack, ".")+ 67 "\n", 68 r.service, 69 r.project) 70 } 71 72 return err 73 } 74 75 func (r *restart) checkServiceExists() (err error) { 76 servicesClient := services.New(we.Context()) 77 _, err = servicesClient.Get(r.ctx, r.project, r.service) 78 return err 79 } 80 81 func preRun(cmd *cobra.Command, args []string) error { 82 return setupHost.Process(context.Background(), we.Context()) 83 } 84 85 func restartRun(cmd *cobra.Command, args []string) error { 86 var r = &restart{ 87 project: setupHost.Project(), 88 service: setupHost.Service(), 89 ctx: context.Background(), 90 } 91 92 if err := r.checkServiceExists(); err != nil { 93 return err 94 } 95 96 return r.do() 97 }