github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/list/list.go (about) 1 package list 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/henvic/ctxsignal" 8 "github.com/henvic/wedeploycli/cmdflagsfromhost" 9 "github.com/henvic/wedeploycli/color" 10 "github.com/henvic/wedeploycli/command/internal/we" 11 listinstances "github.com/henvic/wedeploycli/command/list/instances" 12 listprojects "github.com/henvic/wedeploycli/command/list/projects" 13 listservices "github.com/henvic/wedeploycli/command/list/services" 14 "github.com/henvic/wedeploycli/list" 15 "github.com/henvic/wedeploycli/projects" 16 "github.com/henvic/wedeploycli/services" 17 18 "github.com/spf13/cobra" 19 ) 20 21 // ListCmd is used for getting a list of projects and services 22 var ListCmd = &cobra.Command{ 23 Use: "list", 24 Example: ` lcp list --project chat --service data 25 lcp list --url data-chat.lfr.cloud`, 26 Short: "Show list of projects and services", 27 Args: cobra.NoArgs, 28 PreRunE: preRun, 29 RunE: listRun, 30 } 31 32 var ( 33 detailed bool 34 watch bool 35 ) 36 37 var setupHost = cmdflagsfromhost.SetupHost{ 38 Pattern: cmdflagsfromhost.FullHostPattern, 39 40 Requires: cmdflagsfromhost.Requires{ 41 Auth: true, 42 }, 43 } 44 45 func preRun(cmd *cobra.Command, args []string) error { 46 if err := setupHost.Process(context.Background(), we.Context()); err != nil { 47 return err 48 } 49 50 if !watch { 51 return checkProjectOrServiceExists() 52 } 53 54 return nil 55 } 56 57 func checkProjectOrServiceExists() (err error) { 58 if setupHost.Service() != "" { 59 servicesClient := services.New(we.Context()) 60 // if service exists, project also exists 61 _, err = servicesClient.Get(context.Background(), setupHost.Project(), setupHost.Service()) 62 return err 63 } 64 65 if setupHost.Project() != "" { 66 projectsClient := projects.New(we.Context()) 67 _, err = projectsClient.Get(context.Background(), setupHost.Project()) 68 return err 69 } 70 71 return nil 72 } 73 74 func listRun(cmd *cobra.Command, args []string) error { 75 var filter = list.Filter{ 76 Project: setupHost.Project(), 77 } 78 79 if setupHost.Service() != "" { 80 filter.Services = []string{ 81 setupHost.Service(), 82 } 83 } 84 85 var l = list.New(filter) 86 87 if detailed { 88 l.Details = list.Detailed 89 } 90 91 if !watch { 92 return l.Once(context.Background(), we.Context()) 93 } 94 95 fmt.Println(color.Format(color.FgHiBlack, 96 "List of services will be updated when a change occurs.\n")) 97 98 ctx, cancel := ctxsignal.WithTermination(context.Background()) 99 defer cancel() 100 101 l.Watch(ctx, we.Context()) 102 103 if _, err := ctxsignal.Closed(ctx); err == nil { 104 fmt.Println() 105 } 106 107 return nil 108 } 109 110 func init() { 111 setupHost.Init(ListCmd) 112 113 ListCmd.Flags().BoolVarP( 114 &detailed, 115 "detailed", "d", false, "Show more services details") 116 117 ListCmd.Flags().BoolVarP(&watch, "watch", "w", false, "Show and watch for changes") 118 ListCmd.AddCommand(listprojects.ListProjectsCmd) 119 ListCmd.AddCommand(listservices.ListServicesCmd) 120 ListCmd.AddCommand(listinstances.ListInstancesCmd) 121 }