1+ package exportArtifacts
2+
3+ import (
4+ "context"
5+ "encoding/json"
6+ "fmt"
7+ "log/slog"
8+
9+ "github.com/ChristofferNissen/helmper/pkg/helm"
10+ "github.com/spf13/afero"
11+ )
12+
13+ type ExportOption struct {
14+ Fs afero.Fs
15+ Image helm.RegistryImageStatus
16+ Chart helm.RegistryChartStatus
17+ }
18+
19+ type ChartArtifact struct {
20+ ChartOverview string `json:"chart_overview"`
21+ ChartName string `json:"chart_name"`
22+ Repository string `json:"repository"`
23+ ChartVersion string `json:"chart_version"`
24+ ChartPath string `json:"chart_artifact_path"`
25+ }
26+
27+ type ImageArtifact struct {
28+ ImageOverview string `json:"image_overview"`
29+ ImageName string `json:"image_name"`
30+ ImageTag string `json:"image_tag"`
31+ }
32+
33+ func (eo * ExportOption ) Run (ctx context.Context , folder string ) ([]ImageArtifact , []ChartArtifact , error ) {
34+ // Collect image data
35+ imageArtifacts := []ImageArtifact {}
36+ for r , i := range eo .Image {
37+ for img := range i {
38+ overview := fmt .Sprintf ("Registry: %s, Image: %s, Tag: %s" ,
39+ r .GetName (),
40+ img .String (), img .Tag )
41+ ia := ImageArtifact {
42+ ImageOverview : overview ,
43+ ImageName : img .String (),
44+ ImageTag : img .Tag ,
45+ }
46+ imageArtifacts = append (imageArtifacts , ia )
47+ }
48+ }
49+
50+ // Collect chart data
51+ chartArtifacts := []ChartArtifact {}
52+ for r , c := range eo .Chart {
53+ for chart := range c {
54+ overview := fmt .Sprintf ("Registry: %s, Chart: %s, Version: %s, ChartPath: %s" ,
55+ r .Name , chart .Name , chart .Version , fmt .Sprintf ("charts/%s" , chart .Name ))
56+
57+ ca := ChartArtifact {
58+ ChartOverview : overview ,
59+ ChartName : chart .Name ,
60+ ChartVersion : chart .Version ,
61+ ChartPath : fmt .Sprintf ("charts/%s" , chart .Name ),
62+ }
63+ chartArtifacts = append (chartArtifacts , ca )
64+ }
65+ }
66+
67+ exportData := struct {
68+ Images []ImageArtifact `json:"images"`
69+ Charts []ChartArtifact `json:"charts"`
70+ }{
71+ Images : imageArtifacts ,
72+ Charts : chartArtifacts ,
73+ }
74+
75+ jsonData , err := json .MarshalIndent (exportData , "" , " " )
76+ if err != nil {
77+ slog .Error ("Failed to export data to JSON" , slog .String ("error" , err .Error ()))
78+ return nil , nil , fmt .Errorf ("failed to export data to JSON: %w" , err )
79+ }
80+
81+ destPath := "artifacts.json"
82+ if folder != "" {
83+ err = eo .Fs .MkdirAll (folder , 0755 )
84+ if err != nil {
85+ slog .Error ("Failed to create directory" , slog .String ("folder" , folder ), slog .String ("error" , err .Error ()))
86+ return nil , nil , fmt .Errorf ("failed to save file in the specified location %s: %w" , folder , err )
87+ }
88+ destPath = fmt .Sprintf ("%s/%s" , folder , destPath )
89+ } else {
90+ destPath = "./" + destPath
91+ slog .Info ("No folder specified, saving in the root directory" )
92+ }
93+
94+ err = afero .WriteFile (eo .Fs , destPath , jsonData , 0644 )
95+ if err != nil {
96+ slog .Error ("Failed to write artifacts to" , destPath , slog .String ("error" , err .Error ()))
97+ return nil , nil , fmt .Errorf ("failed to write artifacts to %s: %w" , destPath , err )
98+ }
99+
100+ slog .Info ("Exported artifacts" , slog .String ("path" , destPath ))
101+ return imageArtifacts , chartArtifacts , nil
102+ }
0 commit comments