Skip to content

Commit 0a8da16

Browse files
committed
Clean up, and test added
1 parent 591d559 commit 0a8da16

File tree

4 files changed

+79
-61
lines changed

4 files changed

+79
-61
lines changed

internal/bootstrap/viper.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ type ImportConfigSection struct {
2626
Enabled bool `yaml:"enabled"`
2727
Architecture *string `yaml:"architecture"`
2828
ReplaceRegistryReferences bool `yaml:"replaceRegistryReferences"`
29-
Artifacts struct {
30-
Enabled bool `yaml:"enabled"`
31-
Folder string `yaml:"folder"`
32-
} `yaml:"artifacts"`
33-
Copacetic struct {
29+
Copacetic struct {
3430
Enabled bool `yaml:"enabled"`
3531
IgnoreErrors bool `yaml:"ignoreErrors"`
3632
Buildkitd struct {
@@ -65,6 +61,12 @@ type ImportConfigSection struct {
6561
AllowInsecure bool `yaml:"allowInsecure"`
6662
} `yaml:"cosign"`
6763
} `yaml:"import"`
64+
Export struct {
65+
Artifacts struct {
66+
Enabled bool `yaml:"enabled"`
67+
Folder string `yaml:"folder"`
68+
} `yaml:"artifacts"`
69+
} `yaml:"export"`
6870
}
6971

7072
type imageConfigSection struct {
@@ -143,7 +145,7 @@ func LoadViperConfiguration() (*viper.Viper, error) {
143145
viper.SetDefault("verbose", false)
144146
viper.SetDefault("update", false)
145147
viper.SetDefault("k8s_version", "1.31.1")
146-
viper.SetDefault("import.artifacts.enabled", false)
148+
viper.SetDefault("export.artifacts.enabled", false)
147149

148150
// Unmarshal registries config section
149151
conf := config{}
@@ -279,9 +281,9 @@ copacetic:
279281
`
280282
return nil, xerrors.Errorf("You have enabled copacetic patching but did not specify the path to the tars output folder'. Please add the value and try again\nExample:\n%s", s)
281283
}
282-
if importConf.Import.Artifacts.Enabled && importConf.Import.Artifacts.Folder == "" {
284+
if importConf.Export.Artifacts.Enabled && importConf.Export.Artifacts.Folder == "" {
283285
s := `
284-
import:
286+
export:
285287
artifacts:
286288
enabled: true
287289
folder: /workspace/.out/artifacts <---

internal/program.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,11 @@ func program(ctx context.Context, _ []string, viper *viper.Viper, settings *cli.
271271
return err
272272
}
273273
}
274-
// Step 7: Export and output artifacts
275-
276-
if importConfig.Import.Artifacts.Enabled {
274+
// Step 7: Export artifacts to json
275+
if importConfig.Export.Artifacts.Enabled {
277276
eo := exportArtifacts.ExportOption{
278-
Data: mImgs,
279-
Data2: mCharts,
277+
Image: mImgs,
278+
Chart: mCharts,
280279
}
281280
_, _, err = eo.Run(context.WithoutCancel(ctx))
282281
if err != nil {

pkg/exportArtifacts/main.go

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,51 @@ import (
1414
)
1515

1616
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
17+
Image helm.RegistryImageStatus
18+
Chart helm.RegistryChartStatus // image data
19+
Data2 map[*registry.Registry]map[*helm.Chart]bool // chart data
20+
ChartData helm.ChartData
21+
RegistryImage map[*helm.Chart]map[*image.Image][]bool
22+
Registries []*registry.Registry
2223
}
2324

2425
type ChartArtifact struct {
2526
ChartOverview string `json:"chart_overview"`
2627
ChartName string `json:"chart_name"`
2728
Repository string `json:"repository"`
28-
Registry string `json:"registry"`
2929
ChartVersion string `json:"chart_version"`
30-
Import bool `json:"import"`
30+
ChartPath string `json:"chart_artifact_path"`
3131
}
3232

3333
type ImageArtifact struct {
3434
ImageOverview string `json:"image_overview"`
3535
ImageName string `json:"image_name"`
3636
ImageTag string `json:"image_tag"`
3737
Repository string `json:"repository"`
38-
Import bool `json:"import"`
3938
}
4039

4140
func (eo *ExportOption) Run(ctx context.Context) ([]string, []string, error) {
4241
imgOverview := []string{}
4342
chartOverview := []string{}
44-
chartDataOverview := []string{}
4543

4644
// Collect image data
47-
for reg, imgs := range eo.Data {
48-
for img := range imgs {
45+
for r, i := range eo.Image {
46+
for img := range i {
4947
overview := fmt.Sprintf("Registry: %s, Image: %s, Repository: %s, Tag: %s",
50-
reg.GetName(),
48+
r.GetName(),
5149
img.String(), img.Repository, img.Tag)
52-
fmt.Println("Registry - data 1:", reg)
53-
fmt.Println("Image - data 1:", imgs)
5450
imgOverview = append(imgOverview, overview)
5551
}
5652
}
5753
// 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)
54+
for r, c := range eo.Chart {
55+
for chart := range c {
56+
overview := fmt.Sprintf("Registry: %s, Chart: %s, Version: %s, ChartPath: %s",
57+
r.Name, chart.Name, chart.Version, fmt.Sprintf("charts/%s", chart.Name))
6658
chartOverview = append(chartOverview, overview)
6759
}
6860
}
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"
61+
// Convert to JSON
8062
artifacts := []ImageArtifact{}
8163
for _, overview := range imgOverview {
8264
ia := ImageArtifact{
@@ -87,48 +69,36 @@ func (eo *ExportOption) Run(ctx context.Context) ([]string, []string, error) {
8769
}
8870
artifacts = append(artifacts, ia)
8971
}
72+
9073
chartArtifacts := []ChartArtifact{}
91-
// Convert to JSON (CHARTS) ("Registry: %s, Chart: %s, Version: %s"
9274
for _, overview := range chartOverview {
9375
ca := ChartArtifact{
9476
ChartOverview: overview,
95-
Registry: strings.Split(overview, ", ")[0],
9677
ChartName: strings.Split(overview, ", ")[1],
9778
ChartVersion: strings.Split(overview, ", ")[2],
79+
ChartPath: strings.Split(overview, ", ")[3],
9880
}
9981
chartArtifacts = append(chartArtifacts, ca)
10082
}
10183

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-
11184
exportData := struct {
11285
Images []ImageArtifact `json:"images"`
11386
Charts []ChartArtifact `json:"charts"`
114-
Data []ChartArtifact `json:"chart_data"`
11587
}{
11688
Images: artifacts,
11789
Charts: chartArtifacts,
118-
Data: chartData,
11990
}
12091

12192
jsonData, err := json.MarshalIndent(exportData, "", " ")
12293
if err != nil {
12394
return nil, nil, err
12495
}
12596

126-
// Write JSON to file
12797
err = os.WriteFile("artifacts.json", jsonData, 0644)
12898
if err != nil {
12999
return nil, nil, err
130100
}
131101

132102
slog.Info("Exported artifacts to artifacts.json")
133103
return imgOverview, chartOverview, nil
134-
}
104+
}

pkg/exportArtifacts/main_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
package exportArtifacts
22

3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/ChristofferNissen/helmper/pkg/helm"
9+
"github.com/ChristofferNissen/helmper/pkg/image"
10+
"github.com/ChristofferNissen/helmper/pkg/registry"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestExportOptionRun(t *testing.T) {
15+
// Arrange
16+
mockRegistry := &registry.Registry{Name: "azure.registry.io"}
17+
mockImage := &image.Image{Repository: "argocd", Tag: "v2.0.5"}
18+
mockChart := &helm.Chart{Name: "prometheus", Version: "1.0.0"}
19+
20+
mockData := helm.RegistryImageStatus{
21+
mockRegistry: {
22+
mockImage: true,
23+
24+
},
25+
}
26+
27+
mockData2 := map[*registry.Registry]map[*helm.Chart]bool{
28+
mockRegistry: {
29+
mockChart: true,
30+
},
31+
}
32+
33+
eo := &ExportOption{
34+
Image: mockData,
35+
Chart: mockData2,
36+
}
37+
38+
// Act
39+
imgOverview, chartOverview, err := eo.Run(context.Background())
40+
41+
// Assert
42+
assert.NoError(t, err, "expected no error during execution")
43+
assert.FileExists(t, "artifacts.json", "expected file to be created")
44+
assert.NotEmpty(t, imgOverview, "expected image overview to be populated")
45+
assert.NotEmpty(t, chartOverview, "expected chart overview to be populated")
46+
47+
// Cleanup
48+
os.Remove("artifacts.json")
49+
}

0 commit comments

Comments
 (0)