(p *printer.Printer, resources map[string]map[string][]*resourceWithFile)
| 181 | } |
| 182 | |
| 183 | func printResources(p *printer.Printer, resources map[string]map[string][]*resourceWithFile) { |
| 184 | if len(resources) == 0 { |
| 185 | p.PrintfWarn("No resources found\n") |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | rows := make([]map[string]any, 0) |
| 190 | for org, projectRes := range resources { |
| 191 | for proj, res := range projectRes { |
| 192 | for _, rwf := range res { |
| 193 | r := rwf.Resource |
| 194 | row := make(map[string]any, 0) |
| 195 | rows = append(rows, row) |
| 196 | |
| 197 | // each resource has a meta field and a resource(source/model/metricsview etc) which has spec and state fields |
| 198 | // we want to flatten the resource to have the meta fields and spec and state fields at the top level |
| 199 | rowJSON, err := protojson.Marshal(r) |
| 200 | if err != nil { |
| 201 | p.PrintfWarn("Failed to marshal resource for org %v, project %v : %v\n", org, proj, err) |
| 202 | continue |
| 203 | } |
| 204 | err = json.Unmarshal(rowJSON, &row) |
| 205 | if err != nil { |
| 206 | p.PrintfWarn("Failed to unmarshal resource for org %v, project %v : %v\n", org, proj, err) |
| 207 | continue |
| 208 | } |
| 209 | for k := range row { |
| 210 | if k == "meta" { |
| 211 | continue |
| 212 | } |
| 213 | resource, ok := row[k].(map[string]any) |
| 214 | if !ok { |
| 215 | delete(row, k) |
| 216 | continue |
| 217 | } |
| 218 | row["spec"] = resource["spec"] |
| 219 | row["state"] = resource["state"] |
| 220 | delete(row, k) |
| 221 | break |
| 222 | } |
| 223 | |
| 224 | row["org"] = org |
| 225 | row["project"] = proj |
| 226 | row["resource_type"] = runtime.PrettifyResourceKind(r.Meta.Name.Kind) |
| 227 | row["resource_name"] = r.Meta.Name.Name |
| 228 | if rwf.FileContent != "" { |
| 229 | row["file_content"] = rwf.FileContent |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | jsonData, err := json.MarshalIndent(rows, "", " ") |
| 236 | if err != nil { |
| 237 | p.PrintfWarn("Failed to marshal resources: %v\n", err) |
| 238 | } |
| 239 | fmt.Println(string(jsonData)) |
| 240 | } |
no test coverage detected