list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "github.com/gosuri/uitable"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/util/printutils"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  25. )
  26. func getPrinterRowValue(printer k8s.ListPrinter, obj jsonutils.JSONObject, col string) interface{} {
  27. getFuncName := fmt.Sprintf("Get_%s", strings.Title(col))
  28. manValue := reflect.ValueOf(printer)
  29. funcValue := manValue.MethodByName(getFuncName)
  30. if !funcValue.IsValid() || funcValue.IsNil() {
  31. log.Errorf("Can't get function: %q of manager: %#v", getFuncName, printer)
  32. return nil
  33. }
  34. params := []reflect.Value{
  35. reflect.ValueOf(obj),
  36. }
  37. outs := funcValue.Call(params)
  38. if len(outs) != 1 {
  39. log.Errorf("Invalid return value of function: %q", getFuncName)
  40. return nil
  41. }
  42. return outs[0].Interface()
  43. }
  44. func getPrinterRowValues(printer k8s.ListPrinter, obj jsonutils.JSONObject, cols []string) []interface{} {
  45. ret := make([]interface{}, 0)
  46. for _, col := range cols {
  47. ret = append(ret, getPrinterRowValue(printer, obj, col))
  48. }
  49. return ret
  50. }
  51. func ListerTable(res *printutils.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) *uitable.Table {
  52. min := func(x, y int) int {
  53. if x < y {
  54. return x
  55. }
  56. return y
  57. }
  58. table := uitable.New()
  59. table.MaxColWidth = 80
  60. cols := printer.GetColumns(s)
  61. colsI := make([]interface{}, len(cols))
  62. for i, v := range cols {
  63. colsI[i] = v
  64. }
  65. table.AddRow(colsI...)
  66. var idx int
  67. for ; idx < min(res.Limit, res.Total-res.Offset); idx++ {
  68. table.AddRow(getPrinterRowValues(printer, res.Data[idx], cols)...)
  69. }
  70. return table
  71. }
  72. func PrintListResultTable(res *printutils.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) {
  73. fmt.Println(ListerTable(res, printer, s))
  74. table := uitable.New()
  75. total := res.Total
  76. offset := res.Offset
  77. limit := res.Limit
  78. var (
  79. page = 0
  80. pages = 0
  81. )
  82. if limit != 0 {
  83. page = (offset / limit) + 1
  84. pages = total / limit
  85. }
  86. if pages*limit < total {
  87. pages += 1
  88. }
  89. table.AddRow("")
  90. table.AddRow("Total", "Pages", "Limit", "Offset", "Page")
  91. table.AddRow(total, pages, limit, offset, page)
  92. fmt.Println(table)
  93. }