github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/list/style.go (about) 1 package list 2 3 import ( 4 "fmt" 5 "math/bits" 6 "strings" 7 "time" 8 9 "github.com/henvic/wedeploycli/color" 10 "github.com/henvic/wedeploycli/formatter" 11 "github.com/henvic/wedeploycli/projects" 12 "github.com/henvic/wedeploycli/services" 13 ) 14 15 // Printf list 16 func (l *List) Printf(format string, a ...interface{}) { 17 _, _ = fmt.Fprintf(l.w, format, a...) 18 } 19 20 var projectsHeaders = []string{ 21 "Project", 22 "Status", 23 } 24 25 var servicesHeaders = []string{ 26 "Project", 27 "Service", 28 "Image", 29 "Status", 30 } 31 32 func (l *List) printServicesHeaders() { 33 var projects = l.Projects 34 35 var has bool 36 37 for _, p := range projects { 38 if len(p.Services) != 0 { 39 has = true 40 break 41 } 42 } 43 44 if !has { 45 l.Printf("%s\n", color.Format(color.FgHiBlack, "Project")) 46 return 47 } 48 49 var header string 50 51 if l.SelectNumber { 52 header = "#\t" 53 } 54 55 header += strings.Join(servicesHeaders, "\t") 56 57 if bits.OnesCount(uint(l.Details)) != 0 { 58 header += "\t" 59 } 60 61 if l.Details&Instances != 0 { 62 header += "Instances\t" 63 } 64 65 if l.Details&CPU != 0 { 66 header += "CPU\t" 67 } 68 69 if l.Details&Memory != 0 { 70 header += "Memory\t" 71 } 72 73 if l.Details&CreatedAt != 0 { 74 header += "Created at\t" 75 } 76 77 if formatter.Human { 78 header = strings.Replace(header, "\t", "\t ", -1) 79 } 80 81 l.Printf("%s\n", color.Format(color.FgHiBlack, header)) 82 } 83 84 func (l *List) printProjectsOnlyHeaders() { 85 var header string 86 87 if l.Filter.HideServices && l.SelectNumber { 88 header = "#\t" 89 } 90 91 header += strings.Join(projectsHeaders, "\t") 92 93 if bits.OnesCount(uint(l.Details)) != 0 { 94 header += "\t" 95 } 96 97 if l.Details&CreatedAt != 0 { 98 header += "Created at" 99 } 100 101 if formatter.Human { 102 header = strings.Replace(header, "\t", "\t ", -1) 103 } 104 105 l.Printf("%s\n", color.Format(color.FgHiBlack, header)) 106 } 107 108 func (l *List) printProjects() { 109 l.selectors = []Selection{} 110 111 l.watchMutex.RLock() 112 var projects = l.Projects 113 l.watchMutex.RUnlock() 114 115 if len(projects) == 0 { 116 l.Printf("No project found.\n") 117 return 118 } 119 120 switch l.Filter.HideServices { 121 case true: 122 l.printProjectsOnlyHeaders() 123 default: 124 l.printServicesHeaders() 125 } 126 127 for _, p := range projects { 128 l.printProject(p) 129 } 130 } 131 132 func (l *List) printProject(p projects.Project) { 133 if l.SelectNumber && l.Filter.HideServices { 134 l.selectors = append(l.selectors, Selection{ 135 Project: p.ProjectID, 136 }) 137 138 l.Printf("%d\t", len(l.selectors)) 139 } 140 141 if l.Filter.HideServices { 142 l.printProjectOnly(p) 143 return 144 } 145 146 l.printProjectServices(p) 147 } 148 149 func (l *List) printProjectOnly(p projects.Project) { 150 var s = fmt.Sprintf("%s\t%s", p.ProjectID, p.Health) 151 152 if l.Details&CreatedAt != 0 { 153 s += fmt.Sprintf("\t%v", p.CreatedAtTime().Format(time.RFC822)) 154 } 155 156 s += "\n" 157 158 l.Printf(s) 159 } 160 161 func (l *List) printProjectServices(p projects.Project) { 162 cs := p.Services 163 164 for _, service := range cs { 165 if len(l.Filter.Services) != 0 && !inArray(service.ServiceID, l.Filter.Services) { 166 continue 167 } 168 169 l.printService(p.ProjectID, service) 170 } 171 172 var tabs = len(servicesHeaders) - 1 173 174 for _, d := range details { 175 if l.Details&d != 0 { 176 tabs++ 177 } 178 } 179 180 if len(cs) == 0 { 181 if l.SelectNumber { 182 l.Printf("-\t") 183 } 184 185 l.Printf("%v \t%v", 186 p.ProjectID, 187 color.Format(color.FgYellow, "zero services deployed")+ 188 strings.Repeat("\t", tabs)+ 189 "\n") 190 return 191 } 192 } 193 194 func (l *List) printService(projectID string, s services.Service) { 195 if l.SelectNumber { 196 l.selectors = append(l.selectors, Selection{ 197 Project: projectID, 198 Service: s.ServiceID, 199 }) 200 201 l.Printf("%d\t", len(l.selectors)) 202 } 203 204 l.Printf("%v\t%v\t", projectID, l.getServiceDomain(projectID, s.ServiceID)) 205 l.Printf("%v\t", s.Type()) 206 l.Printf("%v\t", s.Health) 207 208 var ds []string 209 210 if l.Details&Instances != 0 { 211 ds = append(ds, fmt.Sprintf("%d", s.Scale)) 212 } 213 214 if l.Details&CPU != 0 { 215 ds = append(ds, fmt.Sprintf("%.6v", s.CPU)) 216 } 217 218 if l.Details&Memory != 0 { 219 ds = append(ds, fmt.Sprintf("%.6v MB", s.Memory)) 220 } 221 222 if l.Details&CreatedAt != 0 { 223 ds = append(ds, s.CreatedAtTime().Format(time.RFC822)) 224 } 225 226 l.Printf("%s\n", strings.Join(ds, "\t")) 227 } 228 229 func (l *List) getServiceDomain(projectID, serviceID string) string { 230 return fmt.Sprintf("%v-%v.%v", serviceID, projectID, l.wectx.ServiceDomain()) 231 } 232 233 func inArray(key string, haystack []string) bool { 234 for _, k := range haystack { 235 if key == k { 236 return true 237 } 238 } 239 240 return false 241 }