util.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2015 Red Hat Inc. All rights reserved.
  2. //
  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. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package doc
  14. import (
  15. "strings"
  16. "github.com/spf13/cobra"
  17. )
  18. // Test to see if we have a reason to print See Also information in docs
  19. // Basically this is a test for a parent commend or a subcommand which is
  20. // both not deprecated and not the autogenerated help command.
  21. func hasSeeAlso(cmd *cobra.Command) bool {
  22. if cmd.HasParent() {
  23. return true
  24. }
  25. for _, c := range cmd.Commands() {
  26. if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
  27. continue
  28. }
  29. return true
  30. }
  31. return false
  32. }
  33. // Temporary workaround for yaml lib generating incorrect yaml with long strings
  34. // that do not contain \n.
  35. func forceMultiLine(s string) string {
  36. if len(s) > 60 && !strings.Contains(s, "\n") {
  37. s = s + "\n"
  38. }
  39. return s
  40. }
  41. type byName []*cobra.Command
  42. func (s byName) Len() int { return len(s) }
  43. func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  44. func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }