@@ -21,15 +21,22 @@ import (
21
21
)
22
22
23
23
type Syft struct {
24
- SbomFormat string
24
+ sbomFormat string
25
+ resolveVersion func () string
25
26
}
26
27
27
28
func New (sbomFormat string ) Syft {
28
29
return Syft {
29
- SbomFormat : sbomFormat ,
30
+ sbomFormat : sbomFormat ,
31
+ resolveVersion : getSyftVersion ,
30
32
}
31
33
}
32
34
35
+ func (s Syft ) WithVersion (version string ) Syft {
36
+ s .resolveVersion = func () string { return version }
37
+ return s
38
+ }
39
+
33
40
func (s * Syft ) ExecuteSyft (img kubernetes.ContainerImage ) (string , error ) {
34
41
logrus .Infof ("Processing image %s" , img .ImageID )
35
42
@@ -57,24 +64,12 @@ func (s *Syft) ExecuteSyft(img kubernetes.ContainerImage) (string, error) {
57
64
defer cleanup ()
58
65
}
59
66
60
- bi , ok := debug .ReadBuildInfo ()
61
- if ! ok {
62
- logrus .Warnf ("failed to read build info" )
63
- }
64
-
65
- descriptor := sbom.Descriptor {
66
- Name : "syft" ,
67
- }
68
-
69
- for _ , dep := range bi .Deps {
70
- if strings .EqualFold ("github.com/anchore/syft" , dep .Path ) {
71
- descriptor .Version = dep .Version
72
- }
73
- }
74
-
75
67
result := sbom.SBOM {
76
- Source : src .Metadata ,
77
- Descriptor : descriptor ,
68
+ Source : src .Metadata ,
69
+ Descriptor : sbom.Descriptor {
70
+ Name : "syft" ,
71
+ Version : s .resolveVersion (),
72
+ },
78
73
// TODO: we should have helper functions for getting this built from exported library functions
79
74
}
80
75
@@ -91,7 +86,7 @@ func (s *Syft) ExecuteSyft(img kubernetes.ContainerImage) (string, error) {
91
86
result .Relationships = relationships
92
87
93
88
// you can use other formats such as format.CycloneDxJSONOption or format.SPDXJSONOption ...
94
- b , err := syft .Encode (result , format .Option (s .SbomFormat ))
89
+ b , err := syft .Encode (result , format .Option (s .sbomFormat ))
95
90
if err != nil {
96
91
logrus .WithError (err ).Error ("Encoding of result failed" )
97
92
return "" , err
@@ -121,3 +116,18 @@ func GetFileName(sbomFormat string) string {
121
116
return "sbom.json"
122
117
}
123
118
}
119
+
120
+ func getSyftVersion () string {
121
+ bi , ok := debug .ReadBuildInfo ()
122
+ if ! ok {
123
+ logrus .Warnf ("failed to read build info" )
124
+ }
125
+
126
+ for _ , dep := range bi .Deps {
127
+ if strings .EqualFold ("github.com/anchore/syft" , dep .Path ) {
128
+ return dep .Version
129
+ }
130
+ }
131
+
132
+ return ""
133
+ }
0 commit comments