utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 shell
  15. import (
  16. "fmt"
  17. "os"
  18. "sort"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/util/printutils"
  22. "yunion.io/x/onecloud/pkg/util/excelutils"
  23. )
  24. const (
  25. OUTPUT_FORMAT_TABLE = "table" // pretty table
  26. OUTPUT_FORMAT_FLATTEN_TABLE = "flatten-table" // pretty table with flattened keys
  27. OUTPUT_FORMAT_JSON = "json" // json string
  28. OUTPUT_FORMAT_YAML = "yaml" // yaml string
  29. OUTPUT_FORMAT_KV = "kv" // "key: value" as separate line
  30. OUTPUT_FORMAT_FLATTEN_KV = "flatten-kv" // kv with flattened keys
  31. )
  32. var outputFormat = OUTPUT_FORMAT_TABLE
  33. func OutputFormat(s string) {
  34. outputFormat = s
  35. }
  36. func PrintList(list *printutils.ListResult, columns []string) {
  37. switch outputFormat {
  38. case OUTPUT_FORMAT_TABLE:
  39. printutils.PrintJSONList(list, columns)
  40. case OUTPUT_FORMAT_JSON:
  41. fmt.Print(jsonutils.Marshal(list).PrettyString())
  42. fmt.Print("\n")
  43. case OUTPUT_FORMAT_YAML:
  44. fmt.Print(jsonutils.Marshal(list).YAMLString())
  45. default:
  46. fmt.Fprintf(os.Stderr, "unknown output format: %q\n", outputFormat)
  47. }
  48. }
  49. func PrintObject(obj jsonutils.JSONObject) {
  50. switch outputFormat {
  51. case OUTPUT_FORMAT_TABLE:
  52. printutils.PrintJSONObject(obj)
  53. case OUTPUT_FORMAT_KV:
  54. printObjectFmtKv(obj)
  55. case OUTPUT_FORMAT_JSON:
  56. fmt.Print(obj.PrettyString())
  57. fmt.Print("\n")
  58. case OUTPUT_FORMAT_YAML:
  59. fmt.Print(obj.YAMLString())
  60. case OUTPUT_FORMAT_FLATTEN_TABLE:
  61. printObjectRecursive(obj)
  62. case OUTPUT_FORMAT_FLATTEN_KV:
  63. printObjectRecursiveEx(obj, printObjectFmtKv)
  64. default:
  65. fmt.Fprintf(os.Stderr, "unknown output format: %q\n", outputFormat)
  66. }
  67. }
  68. func printObjectFmtKv(obj jsonutils.JSONObject) {
  69. m, _ := obj.GetMap()
  70. maxWidth := 0
  71. keys := make([]string, 0, len(m))
  72. for k := range m {
  73. keys = append(keys, k)
  74. if maxWidth < len(k) {
  75. maxWidth = len(k)
  76. }
  77. }
  78. sort.Strings(keys)
  79. for _, k := range keys {
  80. var s string
  81. objV := m[k]
  82. if objS, ok := objV.(*jsonutils.JSONString); ok {
  83. s, _ = objS.GetString()
  84. s = strings.TrimRight(s, "\n")
  85. } else {
  86. s = objV.String()
  87. }
  88. fmt.Printf("%*s: %s\n", maxWidth, k, s)
  89. }
  90. }
  91. func printObjectRecursive(obj jsonutils.JSONObject) {
  92. PrintObject(obj)
  93. }
  94. func printObjectRecursiveEx(obj jsonutils.JSONObject, cb printutils.PrintJSONObjectRecursiveExFunc) {
  95. printutils.PrintJSONObjectRecursiveEx(obj, cb)
  96. }
  97. func printBatchResults(results []printutils.SubmitResult, columns []string) {
  98. printutils.PrintJSONBatchResults(results, columns)
  99. }
  100. func ExportList(list *printutils.ListResult, file string, exportKeys string, exportTexts string, columns []string) {
  101. var keys []string
  102. var texts []string
  103. if len(exportKeys) > 0 {
  104. keys = strings.Split(exportKeys, ",")
  105. texts = strings.Split(exportTexts, ",")
  106. } else {
  107. keys = columns
  108. texts = columns
  109. }
  110. excelutils.ExportFile(list.Data, keys, texts, file)
  111. }