Skip to content

Commit 1a83d24

Browse files
authored
Merge pull request #5 from unoplat/4-add-logs
feat: added logging
2 parents 0265f26 + 2000ac8 commit 1a83d24

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

code/base-project/image-scan/dockerimageprocessing/image.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
"go.uber.org/zap"
78
"gopkg.in/yaml.v3"
89
)
910

1011
type ImageInfo struct {
1112
Images map[string]string
13+
logger *zap.Logger
1214
}
1315

1416
func NewImageInfo() *ImageInfo {
17+
// Initialize the zap logger
18+
logger, _ := zap.NewProduction()
19+
defer logger.Sync()
1520
return &ImageInfo{
1621
Images: make(map[string]string),
22+
logger: logger,
1723
}
1824
}
1925

2026
func (ii *ImageInfo) UnmarshalYAML(data []byte) error {
2127
var values map[string]interface{}
2228
if err := yaml.Unmarshal(data, &values); err != nil {
29+
ii.logger.Error("Failed to unmarshal YAML", zap.Error(err))
2330
return err
2431
}
2532
ii.findImages(values, "", &ii.Images)
@@ -48,6 +55,7 @@ func (ii *ImageInfo) findImages(node interface{}, path string, images *map[strin
4855
// Directly check for image specification here
4956
imagePath, isImage := ii.constructImagePath(node)
5057
if isImage {
58+
ii.logger.Info("Found image specification", zap.String("path", currentNode.Path+".tag"), zap.String("imagePath", imagePath))
5159
(*images)[currentNode.Path+".tag"] = imagePath
5260
continue // Found an image specification, no need to go deeper in this branch
5361
}

code/base-project/image-scan/dockerimageprocessing/image_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ func TestImageInfoUnmarshalYAML(t *testing.T) {
3030
}
3131

3232
func TestImageInfoMarshalJSON(t *testing.T) {
33-
ii := &ImageInfo{
34-
Images: map[string]string{
35-
"images": "example.com/myapp:latest",
36-
},
33+
ii := NewImageInfo()
34+
ii.Images = map[string]string{
35+
"images": "example.com/myapp:latest",
3736
}
3837
jsonData, err := ii.MarshalJSON()
3938
assert.NoError(t, err)

code/base-project/image-scan/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ go 1.22.0
44

55
require (
66
github.com/stretchr/testify v1.8.4
7+
go.uber.org/zap v1.27.0
78
gopkg.in/yaml.v3 v3.0.1
89
)
910

1011
require (
1112
github.com/davecgh/go-spew v1.1.1 // indirect
1213
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
go.uber.org/multierr v1.10.0 // indirect
1315
)

code/base-project/image-scan/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
66
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
8+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
9+
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
10+
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
11+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
12+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
713
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
814
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
915
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)