Skip to content

Commit cf9b120

Browse files
committed
relocate all model functionality to declcfg and eliminate model package
Signed-off-by: grokspawn <jordan@nimblewidget.com>
1 parent 0b69e6e commit cf9b120

32 files changed

Lines changed: 1049 additions & 4165 deletions

alpha/action/init.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66

77
"github.com/h2non/filetype"
8+
"github.com/h2non/go-is-svg"
89

910
"github.com/operator-framework/operator-registry/alpha/declcfg"
1011
)
@@ -32,21 +33,44 @@ func (i Init) Run() (*declcfg.Package, error) {
3233
}
3334

3435
if i.IconReader != nil {
35-
iconData, err := io.ReadAll(i.IconReader)
36+
icon, err := processIcon(i.IconReader)
3637
if err != nil {
37-
return nil, fmt.Errorf("read icon: %v", err)
38+
return nil, err
3839
}
39-
iconType, err := filetype.Match(iconData)
40-
if err != nil {
41-
return nil, fmt.Errorf("detect icon mediatype: %v", err)
40+
pkg.Icon = icon
41+
}
42+
return pkg, nil
43+
}
44+
45+
func processIcon(iconReader io.Reader) (*declcfg.Icon, error) {
46+
iconData, err := io.ReadAll(iconReader)
47+
if err != nil {
48+
return nil, fmt.Errorf("read icon: %v", err)
49+
}
50+
51+
// Try filetype detection first
52+
iconType, err := filetype.Match(iconData)
53+
if err != nil {
54+
return nil, fmt.Errorf("detect icon mediatype: %v", err)
55+
}
56+
57+
var mediaType string
58+
// If filetype didn't detect it, check if it's SVG
59+
if iconType.MIME.Value == "" {
60+
if issvg.Is(iconData) {
61+
mediaType = "image/svg+xml"
62+
} else {
63+
return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value)
4264
}
65+
} else {
4366
if iconType.MIME.Type != "image" {
4467
return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value)
4568
}
46-
pkg.Icon = &declcfg.Icon{
47-
Data: iconData,
48-
MediaType: iconType.MIME.Value,
49-
}
69+
mediaType = iconType.MIME.Value
5070
}
51-
return pkg, nil
71+
72+
return &declcfg.Icon{
73+
Data: iconData,
74+
MediaType: mediaType,
75+
}, nil
5276
}

0 commit comments

Comments
 (0)