github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/list/list.go (about) 1 package list 2 3 import ( 4 "context" 5 "io" 6 "sync" 7 "time" 8 9 "github.com/henvic/uilive" 10 "github.com/henvic/wedeploycli/config" 11 "github.com/henvic/wedeploycli/formatter" 12 "github.com/henvic/wedeploycli/projects" 13 "github.com/henvic/wedeploycli/services" 14 ) 15 16 // Filter parameters for the list command 17 type Filter struct { 18 Project string 19 Services []string 20 21 HideServices bool 22 } 23 24 // Pattern for detailed listing 25 type Pattern uint 26 27 const ( 28 // Instances info 29 Instances Pattern = 1 << iota 30 // CPU info 31 CPU 32 // Memory info 33 Memory 34 // CreatedAt info 35 CreatedAt 36 // Detailed prints all details 37 Detailed = Instances | CPU | Memory | CreatedAt 38 ) 39 40 var details = []Pattern{Instances, CPU, Memory, CreatedAt} 41 42 // List services object 43 type List struct { 44 Details Pattern 45 46 Filter Filter 47 PoolingInterval time.Duration 48 49 Projects []projects.Project 50 lastError error 51 updated chan struct{} 52 watchMutex sync.RWMutex 53 54 AllowCreateProjectOnPrompt bool 55 SelectNumber bool 56 57 livew *uilive.Writer 58 outStream io.Writer 59 60 projectsClient *projects.Client 61 servicesClient *services.Client 62 63 w *formatter.TabWriter 64 65 once bool 66 67 retry int 68 69 wectx config.Context 70 ctx context.Context 71 selectors []Selection 72 } 73 74 // Selection of a list 75 type Selection struct { 76 Project string 77 Service string 78 } 79 80 // New creates a list using the values of a passed Filter 81 func New(filter Filter) *List { 82 var l = &List{ 83 Filter: filter, 84 PoolingInterval: time.Second, 85 updated: make(chan struct{}, 1), 86 } 87 88 return l 89 } 90 91 func (l *List) prepare(ctx context.Context, wectx config.Context) { 92 l.ctx = ctx 93 l.wectx = wectx 94 95 l.projectsClient = projects.New(l.wectx) 96 l.servicesClient = services.New(l.wectx) 97 98 l.livew = uilive.New() 99 l.outStream = l.livew 100 l.w = formatter.NewTabWriter(l.outStream) 101 } 102 103 // Watch for the list 104 func (l *List) Watch(ctx context.Context, wectx config.Context) { 105 l.prepare(ctx, wectx) 106 107 go l.update() 108 l.watch() 109 } 110 111 // Once runs the list only once 112 func (l *List) Once(ctx context.Context, wectx config.Context) error { 113 l.once = true 114 115 l.PoolingInterval = time.Minute 116 l.prepare(ctx, wectx) 117 l.updateHandler() 118 l.w.Init(l.outStream) 119 l.watchHandler() 120 121 l.watchMutex.RLock() 122 var le = l.lastError 123 l.watchMutex.RUnlock() 124 return le 125 }