1414 * limitations under the License.
1515 */
1616
17- package types
17+ package format
1818
1919import (
2020 "fmt"
21- "sort"
2221 "strconv"
2322 "strings"
2423
24+ "github.com/docker/index-cli-plugin/internal"
25+ "github.com/docker/index-cli-plugin/types"
2526 "github.com/gookit/color"
2627 "github.com/xeonx/timeago"
2728)
@@ -64,22 +65,26 @@ func init() {
6465 }
6566}
6667
67- func FormatImage (image * Image ) string {
68+ func Image (image * types. Image , imageTags bool ) string {
6869 e := ""
6970 if image .Repository .Host != "hub.docker.com" {
7071 e += image .Repository .Host + "/"
7172 }
7273 e += image .Repository .Name
7374 e = defaultColors .green .Sprintf (e )
74- if tags := Tags (image ); len (tags ) > 0 {
75+ tags := types .Tags (image )
76+ if imageTags {
77+ tags = types .ImageTags (image )
78+ }
79+ if len (tags ) > 0 {
7580 for i , t := range tags {
7681 tags [i ] = defaultColors .cyan .Sprintf (t )
7782 }
7883 e += ":" + strings .Join (tags , ", " )
7984 }
8085 if oc := officialContent (image ); oc != "" {
8186 e += " " + defaultColors .blue .Sprintf (oc )
82- if st := SupportedTag (image ); st != "" {
87+ if st := types . SupportedTag (image ); st != "" {
8388 e += " " + defaultColors .red .Sprintf (st )
8489 }
8590 }
@@ -97,7 +102,7 @@ func FormatImage(image *Image) string {
97102 return e
98103}
99104
100- func officialContent (image * Image ) string {
105+ func officialContent (image * types. Image ) string {
101106 switch image .Repository .Badge {
102107 case "open_source" :
103108 return " Sponsored OSS "
@@ -111,60 +116,19 @@ func officialContent(image *Image) string {
111116 return ""
112117}
113118
114- func Tags (image * Image ) []string {
115- currentTags := make ([]string , 0 )
116- for _ , tag := range image .Tag {
117- currentTags = append (currentTags , tag .Name )
118- }
119- for _ , manifestList := range image .ManifestList {
120- for _ , tag := range manifestList .Tags {
121- currentTags = append (currentTags , tag .Name )
122- }
123- }
124- sort .Slice (currentTags , func (i , j int ) bool {
125- return len (currentTags [i ]) < len (currentTags [j ])
126- })
127- return currentTags
128- }
129-
130- func SupportedTag (image * Image ) string {
131- if tagCount := len (image .Repository .SupportedTags ); tagCount > 0 {
132- unsupportedTags := make ([]string , 0 )
133- for _ , tag := range image .Tags {
134- if ! contains (image .Repository .SupportedTags , tag ) {
135- unsupportedTags = append (unsupportedTags , tag )
136- }
137- }
138- if len (unsupportedTags ) == len (image .Tags ) {
139- return " unsupported tag "
140- }
141- }
142- return ""
143- }
144-
145- func CurrentTag (image * Image ) string {
146- currentTags := Tags (image )
119+ func CurrentTag (image * types.Image ) string {
120+ currentTags := types .Tags (image )
147121 if len (currentTags ) > 0 {
148122 for _ , tag := range image .Tags {
149- if contains (currentTags , tag ) {
123+ if internal . Contains (currentTags , tag ) {
150124 return ""
151125 }
152126 }
153127 }
154128 return " tag moved "
155129}
156130
157- func contains (s []string , str string ) bool {
158- for _ , v := range s {
159- if v == str {
160- return true
161- }
162- }
163-
164- return false
165- }
166-
167- func RenderCommit (image * Image ) string {
131+ func RenderCommit (image * types.Image ) string {
168132 if image .TeamId == "A11PU8L1C" {
169133 return fmt .Sprintf ("https://dso.docker.com/images/%s/digests/%s" , image .Repository .Name , image .Digest )
170134 } else if image .Commit .Sha != "" {
@@ -177,7 +141,7 @@ func RenderCommit(image *Image) string {
177141 return ""
178142}
179143
180- func RenderVulnerabilities (image * Image ) string {
144+ func RenderVulnerabilities (image * types. Image ) string {
181145 if len (image .Report ) > 0 {
182146 report := image .Report [0 ]
183147 if report .Total == - 1 {
@@ -203,7 +167,7 @@ func RenderVulnerabilities(image *Image) string {
203167 return ""
204168}
205169
206- func FormatCve (sb * Sbom , c * Cve ) {
170+ func Cve (sb * types. Sbom , c * types. Cve ) {
207171 sourceId := c .SourceId
208172 if c .Cve != nil {
209173 sourceId = c .Cve .SourceId
@@ -228,7 +192,7 @@ func FormatCve(sb *Sbom, c *Cve) {
228192 }
229193}
230194
231- func FormatRemediation (remediation []string ) {
195+ func Remediation (remediation []string ) {
232196 if len (remediation ) > 0 {
233197 fmt .Println ("" )
234198 fmt .Println (defaultColors .underline .Sprintf ("Suggested remediation" ))
@@ -238,8 +202,8 @@ func FormatRemediation(remediation []string) {
238202 }
239203}
240204
241- func FormatPackageRemediation (p Package , c Cve ) string {
242- purl , _ := ToPackageUrl (p .Purl )
205+ func PackageRemediation (p types. Package , c types. Cve ) string {
206+ purl , _ := types . ToPackageUrl (p .Purl )
243207 if c .FixedBy != "not fixed" {
244208 switch purl .Type {
245209 case "alpine" :
@@ -298,8 +262,8 @@ func ColorizeSeverity(severity string) string {
298262 }
299263}
300264
301- func ToSeverity (cve Cve ) string {
302- findSeverity := func (adv * Advisory ) (string , bool ) {
265+ func ToSeverity (cve types. Cve ) string {
266+ findSeverity := func (adv * types. Advisory ) (string , bool ) {
303267 if adv == nil {
304268 return "" , false
305269 }
@@ -328,7 +292,7 @@ func ToSeverity(cve Cve) string {
328292 return "IN TRIAGE"
329293}
330294
331- func ToSeverityInt (cve Cve ) int {
295+ func ToSeverityInt (cve types. Cve ) int {
332296 severity := ToSeverity (cve )
333297 switch severity {
334298 case "CRITICAL" :
0 commit comments