@@ -53,50 +53,13 @@ func Versions(ctx context.Context, workDir, cacheDir string, hMode HydrateMode,
53
53
if err := freshFab .CalculateVersions (fab .Versions ); err != nil {
54
54
return fmt .Errorf ("calculating default versions: %w" , err )
55
55
}
56
- releaseData , err := kyaml .Marshal (freshFab .Status .Versions )
57
- if err != nil {
58
- return fmt .Errorf ("marshalling release versions: %w" , err )
59
- }
60
- var release map [string ]interface {}
61
- if err := kyaml .Unmarshal (releaseData , & release ); err != nil {
62
- return fmt .Errorf ("unmarshalling release versions: %w" , err )
63
- }
64
56
65
- overridesRaw , err := kyaml . Marshal ( cfg .Fab .Spec . Overrides .Versions )
57
+ versionsData , err := formatVersions ( freshFab . Status . Versions , cfg .Fab .Status .Versions )
66
58
if err != nil {
67
- slog .Warn ("Failed to marshal overrides" , "error" , err )
68
- fmt .Println (string (releaseData ))
69
-
70
- return nil
59
+ return fmt .Errorf ("formatting versions: %w" , err )
71
60
}
72
61
73
- var overrides map [string ]interface {}
74
- if err := kyaml .Unmarshal (overridesRaw , & overrides ); err != nil {
75
- slog .Warn ("Failed to unmarshal overrides" , "error" , err )
76
- fmt .Println (string (releaseData ))
77
-
78
- return nil
79
- }
80
-
81
- if len (overrides ) == 0 {
82
- slog .Info ("Printing versions of all components" )
83
- fmt .Println (string (releaseData ))
84
-
85
- return nil
86
- }
87
-
88
- slog .Info ("Printing versions of all components (release → override)" )
89
- merged := make (map [string ]interface {})
90
-
91
- for category , value := range release {
92
- merged [category ] = processVersionCategory (category , value , overrides )
93
- }
94
-
95
- mergedData , err := kyaml .Marshal (merged )
96
- if err != nil {
97
- return fmt .Errorf ("marshalling merged versions: %w" , err )
98
- }
99
- fmt .Println (string (mergedData ))
62
+ fmt .Println (versionsData )
100
63
101
64
return nil
102
65
}
@@ -108,101 +71,99 @@ func getVersionsFromCluster(ctx context.Context, c *Config) error {
108
71
return fmt .Errorf ("creating kube client: %w" , err )
109
72
}
110
73
111
- fab := & fabapi.Fabricator {}
112
- if err := kube .Get (ctx , kclient.ObjectKey {Name : "default" , Namespace : "fab" }, fab ); err != nil {
74
+ fabObj := & fabapi.Fabricator {}
75
+ if err := kube .Get (ctx , kclient.ObjectKey {Name : "default" , Namespace : "fab" }, fabObj ); err != nil {
113
76
return fmt .Errorf ("getting fabricator object: %w" , err )
114
77
}
115
78
116
79
slog .Info ("Printing versions from live cluster" )
117
80
118
- releaseData , err := kyaml . Marshal ( fab . Status . Versions )
119
- if err != nil {
120
- return fmt .Errorf ("marshalling versions: %w" , err )
81
+ freshFab := fabapi. Fabricator {}
82
+ if err := freshFab . CalculateVersions ( fab . Versions ); err != nil {
83
+ return fmt .Errorf ("calculating default versions: %w" , err )
121
84
}
122
85
123
- overridesRaw , err := kyaml . Marshal ( fab . Spec . Overrides .Versions )
86
+ versionsData , err := formatVersions ( freshFab . Status . Versions , fabObj . Status .Versions )
124
87
if err != nil {
125
- slog .Warn ("Failed to marshal overrides" , "error" , err )
126
- fmt .Println (string (releaseData ))
127
-
128
- return nil
88
+ return fmt .Errorf ("formatting versions: %w" , err )
129
89
}
130
90
131
- var overrides map [string ]interface {}
132
- if err := kyaml .Unmarshal (overridesRaw , & overrides ); err != nil {
133
- slog .Warn ("Failed to unmarshal overrides" , "error" , err )
134
- fmt .Println (string (releaseData ))
91
+ fmt .Println (versionsData )
135
92
136
- return nil
137
- }
138
-
139
- if len (overrides ) == 0 {
140
- slog .Info ("Printing versions of all components" )
141
- fmt .Println (string (releaseData ))
93
+ return nil
94
+ }
142
95
143
- return nil
96
+ func formatVersions (releaseVersions , overriddenVersions fabapi.Versions ) (string , error ) {
97
+ releaseMap , err := convertToMap (releaseVersions )
98
+ if err != nil {
99
+ return "" , fmt .Errorf ("converting release versions to map: %w" , err )
144
100
}
145
101
146
- var release map [ string ] interface {}
147
- if err := kyaml . Unmarshal ( releaseData , & release ); err != nil {
148
- slog .Warn ("Failed to unmarshal release versions" , "error" , err )
149
- fmt . Println ( string ( releaseData ) )
102
+ overriddenMap , err := convertToMap ( overriddenVersions )
103
+ if err != nil {
104
+ slog .Warn ("Failed to convert overridden versions" , "error" , err )
105
+ data , _ := kyaml . Marshal ( releaseVersions )
150
106
151
- return nil
107
+ return string ( data ), nil
152
108
}
153
109
154
110
slog .Info ("Printing versions of all components (release → override)" )
155
- merged := make (map [string ]interface {})
156
111
157
- for category , value := range release {
158
- merged [category ] = processVersionCategory (category , value , overrides )
159
- }
112
+ result := compareVersionMaps (releaseMap , overriddenMap )
160
113
161
- mergedData , err := kyaml .Marshal (merged )
114
+ resultData , err := kyaml .Marshal (result )
162
115
if err != nil {
163
- return fmt .Errorf ("marshalling merged versions : %w" , err )
116
+ return "" , fmt .Errorf ("marshalling result : %w" , err )
164
117
}
165
- fmt .Println (string (mergedData ))
166
118
167
- return nil
119
+ return string ( resultData ), nil
168
120
}
169
121
170
- func processVersionCategory (category string , releaseValue interface {}, overrides map [string ]interface {}) interface {} {
171
- releaseCat , isMapRelease := releaseValue .(map [string ]interface {})
172
- if ! isMapRelease {
173
- return releaseValue
174
- }
175
-
176
- result := make (map [string ]interface {})
177
-
178
- overrideCat , overrideExists := overrides [category ]
179
- if ! overrideExists {
180
- return releaseCat
122
+ func convertToMap (v interface {}) (map [string ]interface {}, error ) {
123
+ data , err := kyaml .Marshal (v )
124
+ if err != nil {
125
+ return nil , fmt .Errorf ("marshalling: %w" , err )
181
126
}
182
127
183
- overrideMap , isMapOverride := overrideCat .( map [string ]interface {})
184
- if ! isMapOverride {
185
- return releaseCat
128
+ var result map [string ]interface {}
129
+ if err := kyaml . Unmarshal ( data , & result ); err != nil {
130
+ return nil , fmt . Errorf ( "unmarshalling: %w" , err )
186
131
}
187
132
188
- for compName , releaseVer := range releaseCat {
189
- releaseVerStr , isString := releaseVer .(string )
190
- if ! isString {
191
- nestedResult := processVersionCategory (compName , releaseVer , overrideMap )
192
- result [compName ] = nestedResult
133
+ return result , nil
134
+ }
193
135
194
- continue
195
- }
136
+ func compareVersionMaps ( releases , overridden map [ string ] interface {}) map [ string ] interface {} {
137
+ result := make ( map [ string ] interface {})
196
138
197
- if overrideComp , exists := overrideMap [compName ]; exists {
198
- overrideVerStr , isOverrideString := overrideComp .(string )
199
- if isOverrideString {
200
- result [compName ] = fmt .Sprintf ("%s → %s" , releaseVerStr , overrideVerStr )
139
+ for key , releaseValue := range releases {
140
+ releaseMap , isMap := releaseValue .(map [string ]interface {})
141
+ if isMap {
142
+ overriddenMap , hasOverridden := overridden [key ].(map [string ]interface {})
143
+ if hasOverridden {
144
+ result [key ] = compareVersionMaps (releaseMap , overriddenMap )
201
145
} else {
202
- result [compName ] = releaseVer
146
+ result [key ] = releaseMap
203
147
}
204
148
} else {
205
- result [compName ] = releaseVer
149
+ releaseStr , isString := releaseValue .(string )
150
+ if ! isString {
151
+ result [key ] = releaseValue
152
+
153
+ continue
154
+ }
155
+
156
+ overriddenValue , hasOverridden := overridden [key ]
157
+ if hasOverridden {
158
+ overriddenStr , isString := overriddenValue .(string )
159
+ if isString && overriddenStr != "" && overriddenStr != releaseStr {
160
+ result [key ] = fmt .Sprintf ("%s → %s" , releaseStr , overriddenStr )
161
+ } else {
162
+ result [key ] = releaseValue
163
+ }
164
+ } else {
165
+ result [key ] = releaseValue
166
+ }
206
167
}
207
168
}
208
169
0 commit comments