CompareInputFiles is one of the convenience main entry points for comparing objects. In this case the representation of an input file, which might contain multiple documents. It returns a report with the list of differences.
(from ytbx.InputFile, to ytbx.InputFile, compareOptions ...CompareOption)
| 109 | // objects. In this case the representation of an input file, which might |
| 110 | // contain multiple documents. It returns a report with the list of differences. |
| 111 | func CompareInputFiles(from ytbx.InputFile, to ytbx.InputFile, compareOptions ...CompareOption) (Report, error) { |
| 112 | // initialize the comparator with the tool defaults |
| 113 | cmpr := compare{ |
| 114 | settings: compareSettings{ |
| 115 | NonStandardIdentifierGuessCountThreshold: 3, |
| 116 | IgnoreOrderChanges: false, |
| 117 | KubernetesEntityDetection: true, |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | // apply the optional compare options provided to this function call |
| 122 | for _, compareOption := range compareOptions { |
| 123 | compareOption(&cmpr.settings) |
| 124 | } |
| 125 | |
| 126 | // in case Kubernetes mode is enabled, try to compare documents in the YAML |
| 127 | // file by their names rather than just by the order of the documents |
| 128 | if cmpr.settings.KubernetesEntityDetection { |
| 129 | var fromDocs, toDocs []*yamlv3.Node |
| 130 | var fromNames, toNames []string |
| 131 | |
| 132 | for i := range from.Documents { |
| 133 | if entry := from.Documents[i]; !isEmptyDocument(entry) { |
| 134 | fromDocs = append(fromDocs, entry) |
| 135 | if name, err := k8sItem.Name(entry.Content[0]); err == nil { |
| 136 | fromNames = append(fromNames, name) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | for i := range to.Documents { |
| 142 | if entry := to.Documents[i]; !isEmptyDocument(entry) { |
| 143 | toDocs = append(toDocs, entry) |
| 144 | if name, err := k8sItem.Name(entry.Content[0]); err == nil { |
| 145 | toNames = append(toNames, name) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // when the look-up of a name for each document in each file worked out, it |
| 151 | // means that the documents are most likely Kubernetes resources, so a comparison |
| 152 | // using the names can be done, otherwise, leave and continue with default behavior |
| 153 | if len(fromNames) == len(fromDocs) && len(toNames) == len(toDocs) { |
| 154 | // Reset the docs and names based on the collected details |
| 155 | from.Documents, from.Names = fromDocs, fromNames |
| 156 | to.Documents, to.Names = toDocs, toNames |
| 157 | |
| 158 | // Compare the document nodes |
| 159 | result, err := cmpr.documentNodes(from, to) |
| 160 | if err != nil { |
| 161 | return Report{}, fmt.Errorf("comparing Kubernetes resources: %w", err) |
| 162 | } |
| 163 | return Report{from, to, result}, nil |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if len(from.Documents) != len(to.Documents) { |
| 168 | return Report{}, fmt.Errorf("comparing YAMLs with a different number of documents is currently not supported") |
searching dependent graphs…