customcolumn_flags.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "fmt"
  16. "os"
  17. "strings"
  18. "github.com/spf13/cobra"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. "k8s.io/cli-runtime/pkg/printers"
  21. "k8s.io/kubernetes/pkg/kubectl/scheme"
  22. )
  23. var columnsFormats = map[string]bool{
  24. "custom-columns-file": true,
  25. "custom-columns": true,
  26. }
  27. // CustomColumnsPrintFlags provides default flags necessary for printing
  28. // custom resource columns from an inline-template or file.
  29. type CustomColumnsPrintFlags struct {
  30. NoHeaders bool
  31. TemplateArgument string
  32. }
  33. func (f *CustomColumnsPrintFlags) AllowedFormats() []string {
  34. formats := make([]string, 0, len(columnsFormats))
  35. for format := range columnsFormats {
  36. formats = append(formats, format)
  37. }
  38. return formats
  39. }
  40. // ToPrinter receives an templateFormat and returns a printer capable of
  41. // handling custom-column printing.
  42. // Returns false if the specified templateFormat does not match a supported format.
  43. // Supported format types can be found in pkg/printers/printers.go
  44. func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
  45. if len(templateFormat) == 0 {
  46. return nil, genericclioptions.NoCompatiblePrinterError{}
  47. }
  48. templateValue := ""
  49. if len(f.TemplateArgument) == 0 {
  50. for format := range columnsFormats {
  51. format = format + "="
  52. if strings.HasPrefix(templateFormat, format) {
  53. templateValue = templateFormat[len(format):]
  54. templateFormat = format[:len(format)-1]
  55. break
  56. }
  57. }
  58. } else {
  59. templateValue = f.TemplateArgument
  60. }
  61. if _, supportedFormat := columnsFormats[templateFormat]; !supportedFormat {
  62. return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
  63. }
  64. if len(templateValue) == 0 {
  65. return nil, fmt.Errorf("custom-columns format specified but no custom columns given")
  66. }
  67. // UniversalDecoder call must specify parameter versions; otherwise it will decode to internal versions.
  68. decoder := scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  69. if templateFormat == "custom-columns-file" {
  70. file, err := os.Open(templateValue)
  71. if err != nil {
  72. return nil, fmt.Errorf("error reading template %s, %v\n", templateValue, err)
  73. }
  74. defer file.Close()
  75. p, err := NewCustomColumnsPrinterFromTemplate(file, decoder)
  76. return p, err
  77. }
  78. return NewCustomColumnsPrinterFromSpec(templateValue, decoder, f.NoHeaders)
  79. }
  80. // AddFlags receives a *cobra.Command reference and binds
  81. // flags related to custom-columns printing
  82. func (f *CustomColumnsPrintFlags) AddFlags(c *cobra.Command) {}
  83. // NewCustomColumnsPrintFlags returns flags associated with
  84. // custom-column printing, with default values set.
  85. // NoHeaders and TemplateArgument should be set by callers.
  86. func NewCustomColumnsPrintFlags() *CustomColumnsPrintFlags {
  87. return &CustomColumnsPrintFlags{
  88. NoHeaders: false,
  89. TemplateArgument: "",
  90. }
  91. }