|
| 1 | +package exportArtifacts |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ChristofferNissen/helmper/pkg/helm" |
| 12 | + "github.com/ChristofferNissen/helmper/pkg/image" |
| 13 | + "github.com/ChristofferNissen/helmper/pkg/registry" |
| 14 | +) |
| 15 | + |
| 16 | +type ExportOption struct { |
| 17 | + Data helm.RegistryImageStatus // image data |
| 18 | + Data2 map[*registry.Registry]map[*helm.Chart]bool // chart data |
| 19 | + ChartData helm.ChartData |
| 20 | + //RegistryImage map[*registry.Registry]map[*image.Image]bool |
| 21 | + RegistryImage map[*helm.Chart]map[*image.Image][]bool |
| 22 | +} |
| 23 | + |
| 24 | +type ChartArtifact struct { |
| 25 | + ChartOverview string `json:"chart_overview"` |
| 26 | + ChartName string `json:"chart_name"` |
| 27 | + Repository string `json:"repository"` |
| 28 | + Registry string `json:"registry"` |
| 29 | + ChartVersion string `json:"chart_version"` |
| 30 | + Import bool `json:"import"` |
| 31 | +} |
| 32 | + |
| 33 | +type ImageArtifact struct { |
| 34 | + ImageOverview string `json:"image_overview"` |
| 35 | + ImageName string `json:"image_name"` |
| 36 | + ImageTag string `json:"image_tag"` |
| 37 | + Repository string `json:"repository"` |
| 38 | + Import bool `json:"import"` |
| 39 | +} |
| 40 | + |
| 41 | +func (eo *ExportOption) Run(ctx context.Context) ([]string, []string, error) { |
| 42 | + imgOverview := []string{} |
| 43 | + chartOverview := []string{} |
| 44 | + chartDataOverview := []string{} |
| 45 | + |
| 46 | + // Collect image data |
| 47 | + for reg, imgs := range eo.Data { |
| 48 | + for img := range imgs { |
| 49 | + overview := fmt.Sprintf("Registry: %s, Image: %s, Repository: %s, Tag: %s", |
| 50 | + reg.GetName(), |
| 51 | + img.String(), img.Repository, img.Tag) |
| 52 | + fmt.Println("Registry - data 1:", reg) |
| 53 | + fmt.Println("Image - data 1:", imgs) |
| 54 | + imgOverview = append(imgOverview, overview) |
| 55 | + } |
| 56 | + } |
| 57 | + // Collect chart data |
| 58 | + for reg, charts := range eo.Data2 { |
| 59 | + for chart := range charts { |
| 60 | + overview := fmt.Sprintf("Registry: %s, Chart: %s, Version: %s", |
| 61 | + reg.Name, chart.Name, chart.Version) |
| 62 | + fmt.Println("Registry - data 2:", reg) |
| 63 | + fmt.Println("Chart - data 2:", chart) |
| 64 | + chartPath := fmt.Sprintf("charts/%s", chart.Name) |
| 65 | + fmt.Println(chartPath) |
| 66 | + chartOverview = append(chartOverview, overview) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Collect chart data (2) |
| 71 | + for reg, charts := range eo.RegistryImage { |
| 72 | + // for chart := range charts { |
| 73 | + overview := fmt.Sprintf("Reg: %s, Charts: %s", |
| 74 | + reg, charts) |
| 75 | + chartDataOverview = append(chartDataOverview, overview) |
| 76 | + // } |
| 77 | + } |
| 78 | + |
| 79 | + // Convert to JSON (IMAGES) ("Registry: %s, Image: %s, Repository: %s, Tag: %s" |
| 80 | + artifacts := []ImageArtifact{} |
| 81 | + for _, overview := range imgOverview { |
| 82 | + ia := ImageArtifact{ |
| 83 | + ImageOverview: overview, |
| 84 | + ImageName: strings.Split(overview, ", ")[1], |
| 85 | + Repository: strings.Split(overview, ", ")[2], |
| 86 | + ImageTag: strings.Split(overview, ", ")[3], |
| 87 | + } |
| 88 | + artifacts = append(artifacts, ia) |
| 89 | + } |
| 90 | + chartArtifacts := []ChartArtifact{} |
| 91 | + // Convert to JSON (CHARTS) ("Registry: %s, Chart: %s, Version: %s" |
| 92 | + for _, overview := range chartOverview { |
| 93 | + ca := ChartArtifact{ |
| 94 | + ChartOverview: overview, |
| 95 | + Registry: strings.Split(overview, ", ")[0], |
| 96 | + ChartName: strings.Split(overview, ", ")[1], |
| 97 | + ChartVersion: strings.Split(overview, ", ")[2], |
| 98 | + } |
| 99 | + chartArtifacts = append(chartArtifacts, ca) |
| 100 | + } |
| 101 | + |
| 102 | + chartData := []ChartArtifact{} |
| 103 | + // Convert to JSON (CHARTS) ("Registry: %s, Chart: %s, Version: %s" |
| 104 | + for _, overview := range chartDataOverview { |
| 105 | + cd := ChartArtifact{ |
| 106 | + ChartOverview: overview, |
| 107 | + } |
| 108 | + chartData = append(chartData, cd) |
| 109 | + } |
| 110 | + |
| 111 | + exportData := struct { |
| 112 | + Images []ImageArtifact `json:"images"` |
| 113 | + Charts []ChartArtifact `json:"charts"` |
| 114 | + Data []ChartArtifact `json:"chart_data"` |
| 115 | + }{ |
| 116 | + Images: artifacts, |
| 117 | + Charts: chartArtifacts, |
| 118 | + Data: chartData, |
| 119 | + } |
| 120 | + |
| 121 | + jsonData, err := json.MarshalIndent(exportData, "", " ") |
| 122 | + if err != nil { |
| 123 | + return nil, nil, err |
| 124 | + } |
| 125 | + |
| 126 | + // Write JSON to file |
| 127 | + err = os.WriteFile("artifacts.json", jsonData, 0644) |
| 128 | + if err != nil { |
| 129 | + return nil, nil, err |
| 130 | + } |
| 131 | + |
| 132 | + slog.Info("Exported artifacts to artifacts.json") |
| 133 | + return imgOverview, chartOverview, nil |
| 134 | +} |
0 commit comments