humanreadable_flags.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package get
  14. import (
  15. "github.com/spf13/cobra"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/kubernetes/pkg/printers"
  19. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  20. )
  21. // HumanPrintFlags provides default flags necessary for printing.
  22. // Given the following flag values, a printer can be requested that knows
  23. // how to handle printing based on these values.
  24. type HumanPrintFlags struct {
  25. ShowKind *bool
  26. ShowLabels *bool
  27. SortBy *string
  28. ColumnLabels *[]string
  29. // get.go-specific values
  30. NoHeaders bool
  31. Kind schema.GroupKind
  32. AbsoluteTimestamps bool
  33. WithNamespace bool
  34. }
  35. // SetKind sets the Kind option
  36. func (f *HumanPrintFlags) SetKind(kind schema.GroupKind) {
  37. f.Kind = kind
  38. }
  39. // EnsureWithKind sets the "Showkind" humanreadable option to true.
  40. func (f *HumanPrintFlags) EnsureWithKind() error {
  41. showKind := true
  42. f.ShowKind = &showKind
  43. return nil
  44. }
  45. // EnsureWithNamespace sets the "WithNamespace" humanreadable option to true.
  46. func (f *HumanPrintFlags) EnsureWithNamespace() error {
  47. f.WithNamespace = true
  48. return nil
  49. }
  50. // AllowedFormats returns more customized formating options
  51. func (f *HumanPrintFlags) AllowedFormats() []string {
  52. return []string{"wide"}
  53. }
  54. // ToPrinter receives an outputFormat and returns a printer capable of
  55. // handling human-readable output.
  56. func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
  57. if len(outputFormat) > 0 && outputFormat != "wide" {
  58. return nil, genericclioptions.NoCompatiblePrinterError{Options: f, AllowedFormats: f.AllowedFormats()}
  59. }
  60. showKind := false
  61. if f.ShowKind != nil {
  62. showKind = *f.ShowKind
  63. }
  64. showLabels := false
  65. if f.ShowLabels != nil {
  66. showLabels = *f.ShowLabels
  67. }
  68. columnLabels := []string{}
  69. if f.ColumnLabels != nil {
  70. columnLabels = *f.ColumnLabels
  71. }
  72. p := printers.NewTablePrinter(printers.PrintOptions{
  73. Kind: f.Kind,
  74. WithKind: showKind,
  75. NoHeaders: f.NoHeaders,
  76. Wide: outputFormat == "wide",
  77. WithNamespace: f.WithNamespace,
  78. ColumnLabels: columnLabels,
  79. ShowLabels: showLabels,
  80. })
  81. printersinternal.AddHandlers(p)
  82. // TODO(juanvallejo): handle sorting here
  83. return p, nil
  84. }
  85. // AddFlags receives a *cobra.Command reference and binds
  86. // flags related to human-readable printing to it
  87. func (f *HumanPrintFlags) AddFlags(c *cobra.Command) {
  88. if f.ShowLabels != nil {
  89. c.Flags().BoolVar(f.ShowLabels, "show-labels", *f.ShowLabels, "When printing, show all labels as the last column (default hide labels column)")
  90. }
  91. if f.SortBy != nil {
  92. c.Flags().StringVar(f.SortBy, "sort-by", *f.SortBy, "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.")
  93. }
  94. if f.ColumnLabels != nil {
  95. c.Flags().StringSliceVarP(f.ColumnLabels, "label-columns", "L", *f.ColumnLabels, "Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag options like -L label1 -L label2...")
  96. }
  97. if f.ShowKind != nil {
  98. c.Flags().BoolVar(f.ShowKind, "show-kind", *f.ShowKind, "If present, list the resource type for the requested object(s).")
  99. }
  100. }
  101. // NewHumanPrintFlags returns flags associated with
  102. // human-readable printing, with default values set.
  103. func NewHumanPrintFlags() *HumanPrintFlags {
  104. showLabels := false
  105. sortBy := ""
  106. showKind := false
  107. columnLabels := []string{}
  108. return &HumanPrintFlags{
  109. NoHeaders: false,
  110. WithNamespace: false,
  111. AbsoluteTimestamps: false,
  112. ColumnLabels: &columnLabels,
  113. Kind: schema.GroupKind{},
  114. ShowLabels: &showLabels,
  115. SortBy: &sortBy,
  116. ShowKind: &showKind,
  117. }
  118. }