@@ -2,64 +2,25 @@ package product
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "log/slog"
87 "os"
9- "regexp"
108 "strings"
119 "text/tabwriter"
1210
1311 "github.com/rancherlabs/slsactl/internal/imagelist"
1412)
1513
16- type productInfo struct {
17- description string
18- imagesUrl string
19- windowsImagesUrl string
20- }
21-
22- var (
23- ErrInvalidVersion = errors .New ("invalid version" )
24-
25- versionRegex = regexp .MustCompile (`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]d*)(\-(?:alpha|beta|rc)\d+)?$` )
26-
27- productMapping = map [string ]productInfo {
28- "rancher-prime" : {
29- description : "SUSE Rancher Prime" ,
30- imagesUrl : "https://github.com/rancher/rancher/releases/download/%s/rancher-images.txt" ,
31- windowsImagesUrl : "https://github.com/rancher/rancher/releases/download/%s/rancher-windows-images.txt" ,
32- },
33- "storage" : {
34- description : "SUSE Storage" ,
35- imagesUrl : "https://github.com/longhorn/longhorn/releases/download/%s/longhorn-images.txt" ,
36- },
37- "virtualization" : {
38- description : "SUSE Virtualization" ,
39- imagesUrl : "https://github.com/harvester/harvester/releases/download/%s/harvester-images-list-amd64.txt" ,
40- },
41- }
42- )
43-
4414func Verify (registry , name , version string , summary bool , outputFile bool ) error {
45- if ! versionRegex .MatchString (version ) {
46- return fmt .Errorf ("%w: %s" , ErrInvalidVersion , version )
47- }
48-
49- info , found := productMapping [name ]
50- if ! found {
51- var names []string
52- for name := range productMapping {
53- names = append (names , name )
54- }
55- products := strings .Join (names , ", " )
56- return fmt .Errorf ("product %q not found: options are %s" , name , products )
15+ info , err := product (name , version )
16+ if err != nil {
17+ return err
5718 }
5819
5920 fmt .Printf ("Verifying container images for %s %s:\n \n " , info .description , version )
6021
6122 p := imagelist .NewProcessor (registry )
62- result , err := p .Process (fmt .Sprintf (info .imagesUrl , version ))
23+ result , err := p .Verify (fmt .Sprintf (info .imagesUrl , version ))
6324 if err != nil {
6425 return err
6526 }
@@ -68,7 +29,7 @@ func Verify(registry, name, version string, summary bool, outputFile bool) error
6829 result .Version = version
6930
7031 if len (info .windowsImagesUrl ) > 0 {
71- r2 , err := p .Process (fmt .Sprintf (info .windowsImagesUrl , version ))
32+ r2 , err := p .Verify (fmt .Sprintf (info .windowsImagesUrl , version ))
7233 if err == nil {
7334 result .Entries = append (result .Entries , r2 .Entries ... )
7435 } else {
@@ -77,26 +38,20 @@ func Verify(registry, name, version string, summary bool, outputFile bool) error
7738 }
7839
7940 if summary {
80- err = printSummary (result )
41+ err = printVerifySummary (result )
8142 if err != nil {
8243 return fmt .Errorf ("failed to print summary: %w" , err )
8344 }
8445 }
8546
8647 if outputFile {
87- return saveOutput (result )
48+ return savePrintOutput (result )
8849 }
8950
9051 return nil
9152}
9253
93- type summary struct {
94- count int
95- signed int
96- errors int
97- }
98-
99- func printSummary (result * imagelist.Result ) error {
54+ func printVerifySummary (result * imagelist.Result ) error {
10055 w := new (tabwriter.Writer )
10156 w .Init (os .Stdout , 12 , 12 , 4 , ' ' , 0 )
10257
@@ -131,7 +86,7 @@ func printSummary(result *imagelist.Result) error {
13186 return w .Flush ()
13287}
13388
134- func saveOutput (result * imagelist.Result ) error {
89+ func savePrintOutput (result * imagelist.Result ) error {
13590 data , err := json .MarshalIndent (result , "" , " " )
13691 if err != nil {
13792 return fmt .Errorf ("fail to marshal JSON: %w" , err )
0 commit comments