-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
76 lines (67 loc) · 1.81 KB
/
Copy pathrender.go
File metadata and controls
76 lines (67 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
)
const none = "<none>"
// emit renders a typed list in the format selected by the --output flag. For
// table output it prints headers + rows; for json/yaml it marshals the (already
// filtered) typed list so scripted callers get full objects.
func emit(cmd *cobra.Command, list runtime.Object, headers []string, rows [][]string) error {
format, _ := cmd.Flags().GetString("output")
out := cmd.OutOrStdout()
switch format {
case "json":
b, err := json.MarshalIndent(list, "", " ")
if err != nil {
return err
}
_, err = fmt.Fprintln(out, string(b))
return err
case "yaml":
b, err := yaml.Marshal(list)
if err != nil {
return err
}
_, err = fmt.Fprint(out, string(b))
return err
case "", "table":
return printTable(out, headers, rows)
default:
return fmt.Errorf("invalid value %q for --output; allowed: table, json, yaml", format)
}
}
func printTable(out io.Writer, headers []string, rows [][]string) error {
if len(rows) == 0 {
_, err := fmt.Fprintln(out, "No matching inventory found.")
return err
}
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(headers, "\t"))
for _, row := range rows {
fmt.Fprintln(w, strings.Join(row, "\t"))
}
return w.Flush()
}
// ready returns the status of the "Ready" condition, or "<none>" when absent.
func ready(conds []metav1.Condition) string {
if c := meta.FindStatusCondition(conds, "Ready"); c != nil && c.Status != "" {
return string(c.Status)
}
return none
}
func orNone(s string) string {
if s == "" {
return none
}
return s
}