help.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package pretty
  2. import (
  3. "fmt"
  4. "io"
  5. "sigs.k8s.io/controller-tools/pkg/genall/help"
  6. "github.com/fatih/color"
  7. )
  8. var (
  9. headingStyle = Decoration(*color.New(color.Bold, color.Underline))
  10. markerNameStyle = Decoration(*color.New(color.Bold))
  11. fieldSummaryStyle = Decoration(*color.New(color.FgGreen, color.Italic))
  12. markerTargetStyle = Decoration(*color.New(color.Faint))
  13. fieldDetailStyle = Decoration(*color.New(color.Italic, color.FgGreen))
  14. deprecatedStyle = Decoration(*color.New(color.CrossedOut))
  15. )
  16. // MarkersSummary returns a condensed summary of help for the given markers.
  17. func MarkersSummary(groupName string, markers []help.MarkerDoc) Span {
  18. out := new(SpanWriter)
  19. out.Print(Text("\n"))
  20. out.Print(headingStyle.Containing(Text(groupName)))
  21. out.Print(Text("\n\n"))
  22. table := &Table{Sizing: &TableCalculator{Padding: 2}}
  23. for _, marker := range markers {
  24. table.StartRow()
  25. table.Column(MarkerSyntaxHelp(marker))
  26. table.Column(markerTargetStyle.Containing(Text(marker.Target)))
  27. summary := new(SpanWriter)
  28. if marker.DeprecatedInFavorOf != nil && len(*marker.DeprecatedInFavorOf) > 0 {
  29. summary.Print(markerNameStyle.Containing(Text("(use ")))
  30. summary.Print(markerNameStyle.Containing(Text(*marker.DeprecatedInFavorOf)))
  31. summary.Print(markerNameStyle.Containing(Text(") ")))
  32. }
  33. summary.Print(Text(marker.Summary))
  34. table.Column(summary)
  35. table.EndRow()
  36. }
  37. out.Print(table)
  38. out.Print(Text("\n"))
  39. return out
  40. }
  41. // MarkersDetails returns detailed help for the given markers, including detailed field help.
  42. func MarkersDetails(fullDetail bool, groupName string, markers []help.MarkerDoc) Span {
  43. out := new(SpanWriter)
  44. out.Print(Line(headingStyle.Containing(Text(groupName))))
  45. out.Print(Newlines(2))
  46. for _, marker := range markers {
  47. out.Print(Line(markerName(marker)))
  48. out.Print(Text(" "))
  49. out.Print(markerTargetStyle.Containing(Text(marker.Target)))
  50. summary := new(SpanWriter)
  51. if marker.DeprecatedInFavorOf != nil && len(*marker.DeprecatedInFavorOf) > 0 {
  52. summary.Print(markerNameStyle.Containing(Text("(use ")))
  53. summary.Print(markerNameStyle.Containing(Text(*marker.DeprecatedInFavorOf)))
  54. summary.Print(markerNameStyle.Containing(Text(") ")))
  55. }
  56. summary.Print(Text(marker.Summary))
  57. if !marker.AnonymousField() {
  58. out.Print(Indented(1, Line(summary)))
  59. if len(marker.Details) > 0 && fullDetail {
  60. out.Print(Indented(1, Line(Text(marker.Details))))
  61. }
  62. }
  63. if marker.AnonymousField() {
  64. out.Print(Indented(1, Line(fieldDetailStyle.Containing(FieldSyntaxHelp(marker.Fields[0])))))
  65. out.Print(Text(" "))
  66. out.Print(summary)
  67. if len(marker.Details) > 0 && fullDetail {
  68. out.Print(Indented(2, Line(Text(marker.Details))))
  69. }
  70. out.Print(Newlines(1))
  71. } else if !marker.Empty() {
  72. out.Print(Newlines(1))
  73. if fullDetail {
  74. for _, arg := range marker.Fields {
  75. out.Print(Indented(1, Line(fieldDetailStyle.Containing(FieldSyntaxHelp(arg)))))
  76. out.Print(Indented(2, Line(Text(arg.Summary))))
  77. if len(arg.Details) > 0 && fullDetail {
  78. out.Print(Indented(2, Line(Text(arg.Details))))
  79. out.Print(Newlines(1))
  80. }
  81. }
  82. out.Print(Newlines(1))
  83. } else {
  84. table := &Table{Sizing: &TableCalculator{Padding: 2}}
  85. for _, arg := range marker.Fields {
  86. table.StartRow()
  87. table.Column(fieldDetailStyle.Containing(FieldSyntaxHelp(arg)))
  88. table.Column(Text(arg.Summary))
  89. table.EndRow()
  90. }
  91. out.Print(Indented(1, table))
  92. }
  93. } else {
  94. out.Print(Newlines(1))
  95. }
  96. }
  97. return out
  98. }
  99. func FieldSyntaxHelp(arg help.FieldHelp) Span {
  100. return fieldSyntaxHelp(arg, "")
  101. }
  102. // fieldSyntaxHelp prints the syntax help for a particular marker argument.
  103. func fieldSyntaxHelp(arg help.FieldHelp, sep string) Span {
  104. if arg.Optional {
  105. return FromWriter(func(out io.Writer) error {
  106. _, err := fmt.Fprintf(out, "[%s%s=<%s>]", sep, arg.Name, arg.TypeString())
  107. return err
  108. })
  109. }
  110. return FromWriter(func(out io.Writer) error {
  111. _, err := fmt.Fprintf(out, "%s%s=<%s>", sep, arg.Name, arg.TypeString())
  112. return err
  113. })
  114. }
  115. // markerName returns a span containing just the appropriately-formatted marker name.
  116. func markerName(def help.MarkerDoc) Span {
  117. if def.DeprecatedInFavorOf != nil {
  118. return deprecatedStyle.Containing(Text("+" + def.Name))
  119. }
  120. return markerNameStyle.Containing(Text("+" + def.Name))
  121. }
  122. // MarkerSyntaxHelp assembles syntax help for a given marker.
  123. func MarkerSyntaxHelp(def help.MarkerDoc) Span {
  124. out := new(SpanWriter)
  125. out.Print(markerName(def))
  126. if def.Empty() {
  127. return out
  128. }
  129. sep := ":"
  130. if def.AnonymousField() {
  131. sep = ""
  132. }
  133. fieldStyle := fieldSummaryStyle
  134. if def.DeprecatedInFavorOf != nil {
  135. fieldStyle = deprecatedStyle
  136. }
  137. for _, arg := range def.Fields {
  138. out.Print(fieldStyle.Containing(fieldSyntaxHelp(arg, sep)))
  139. sep = ","
  140. }
  141. return out
  142. }