github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/genall/help/sort.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package help 18 19 import ( 20 "strings" 21 22 "sigs.k8s.io/controller-tools/pkg/markers" 23 ) 24 25 // SortGroup knows how to sort and group marker definitions. 26 type SortGroup interface { 27 // Less is equivalent to the Less function from sort, and is used to sort the markers. 28 Less(*markers.Definition, *markers.Definition) bool 29 // Group returns the "group" that a given marker belongs to. 30 Group(*markers.Definition, *markers.DefinitionHelp) string 31 } 32 33 var ( 34 // SortByCategory sorts the markers by name and groups them by their help category. 35 SortByCategory = sortByCategory{} 36 37 // SortByOption sorts by the generator that the option belongs to. 38 SortByOption = optionsSort{} 39 ) 40 41 type sortByCategory struct{} 42 43 func (sortByCategory) Group(_ *markers.Definition, help *markers.DefinitionHelp) string { 44 if help == nil { 45 return "" 46 } 47 return help.Category 48 } 49 func (sortByCategory) Less(i, j *markers.Definition) bool { 50 return i.Name < j.Name 51 } 52 53 type optionsSort struct{} 54 55 func (optionsSort) Less(i, j *markers.Definition) bool { 56 iParts := strings.Split(i.Name, ":") 57 jParts := strings.Split(j.Name, ":") 58 59 iGen := "" 60 iRule := "" 61 jGen := "" 62 jRule := "" 63 64 switch len(iParts) { 65 case 1: 66 iGen = iParts[0] 67 // two means a default output rule, so ignore 68 case 2: 69 iRule = iParts[1] 70 case 3: 71 iGen = iParts[1] 72 iRule = iParts[2] 73 } 74 switch len(jParts) { 75 case 1: 76 jGen = jParts[0] 77 // two means a default output rule, so ignore 78 case 2: 79 jRule = jParts[1] 80 case 3: 81 jGen = jParts[1] 82 jRule = jParts[2] 83 } 84 85 if iGen != jGen { 86 return iGen > jGen 87 } 88 89 return iRule < jRule 90 } 91 func (optionsSort) Group(def *markers.Definition, _ *markers.DefinitionHelp) string { 92 parts := strings.Split(def.Name, ":") 93 94 switch len(parts) { 95 case 1: 96 if parts[0] == "paths" { 97 return "generic" 98 } 99 return "generators" 100 case 2: 101 return "output rules (optionally as output:<generator>:...)" 102 default: 103 return "" 104 // three means a marker-specific output rule, ignore 105 } 106 }