| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package shell
- import (
- "fmt"
- "os"
- "sort"
- "strings"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/util/printutils"
- "yunion.io/x/onecloud/pkg/util/excelutils"
- )
- const (
- OUTPUT_FORMAT_TABLE = "table" // pretty table
- OUTPUT_FORMAT_FLATTEN_TABLE = "flatten-table" // pretty table with flattened keys
- OUTPUT_FORMAT_JSON = "json" // json string
- OUTPUT_FORMAT_YAML = "yaml" // yaml string
- OUTPUT_FORMAT_KV = "kv" // "key: value" as separate line
- OUTPUT_FORMAT_FLATTEN_KV = "flatten-kv" // kv with flattened keys
- )
- var outputFormat = OUTPUT_FORMAT_TABLE
- func OutputFormat(s string) {
- outputFormat = s
- }
- func PrintList(list *printutils.ListResult, columns []string) {
- switch outputFormat {
- case OUTPUT_FORMAT_TABLE:
- printutils.PrintJSONList(list, columns)
- case OUTPUT_FORMAT_JSON:
- fmt.Print(jsonutils.Marshal(list).PrettyString())
- fmt.Print("\n")
- case OUTPUT_FORMAT_YAML:
- fmt.Print(jsonutils.Marshal(list).YAMLString())
- default:
- fmt.Fprintf(os.Stderr, "unknown output format: %q\n", outputFormat)
- }
- }
- func PrintObject(obj jsonutils.JSONObject) {
- switch outputFormat {
- case OUTPUT_FORMAT_TABLE:
- printutils.PrintJSONObject(obj)
- case OUTPUT_FORMAT_KV:
- printObjectFmtKv(obj)
- case OUTPUT_FORMAT_JSON:
- fmt.Print(obj.PrettyString())
- fmt.Print("\n")
- case OUTPUT_FORMAT_YAML:
- fmt.Print(obj.YAMLString())
- case OUTPUT_FORMAT_FLATTEN_TABLE:
- printObjectRecursive(obj)
- case OUTPUT_FORMAT_FLATTEN_KV:
- printObjectRecursiveEx(obj, printObjectFmtKv)
- default:
- fmt.Fprintf(os.Stderr, "unknown output format: %q\n", outputFormat)
- }
- }
- func printObjectFmtKv(obj jsonutils.JSONObject) {
- m, _ := obj.GetMap()
- maxWidth := 0
- keys := make([]string, 0, len(m))
- for k := range m {
- keys = append(keys, k)
- if maxWidth < len(k) {
- maxWidth = len(k)
- }
- }
- sort.Strings(keys)
- for _, k := range keys {
- var s string
- objV := m[k]
- if objS, ok := objV.(*jsonutils.JSONString); ok {
- s, _ = objS.GetString()
- s = strings.TrimRight(s, "\n")
- } else {
- s = objV.String()
- }
- fmt.Printf("%*s: %s\n", maxWidth, k, s)
- }
- }
- func printObjectRecursive(obj jsonutils.JSONObject) {
- PrintObject(obj)
- }
- func printObjectRecursiveEx(obj jsonutils.JSONObject, cb printutils.PrintJSONObjectRecursiveExFunc) {
- printutils.PrintJSONObjectRecursiveEx(obj, cb)
- }
- func printBatchResults(results []printutils.SubmitResult, columns []string) {
- printutils.PrintJSONBatchResults(results, columns)
- }
- func ExportList(list *printutils.ListResult, file string, exportKeys string, exportTexts string, columns []string) {
- var keys []string
- var texts []string
- if len(exportKeys) > 0 {
- keys = strings.Split(exportKeys, ",")
- texts = strings.Split(exportTexts, ",")
- } else {
- keys = columns
- texts = columns
- }
- excelutils.ExportFile(list.Data, keys, texts, file)
- }
|