debian-forge-composer/vendor/github.com/osbuild/images/pkg/distroidparser/idparser.go
Tomáš Hozza 625b1578fa Port osbuild/images v0.33.0 with dot-notation to composer
Update the osbuild/images to the version which introduces "dot notation"
for distro release versions.

 - Replace all uses of distroregistry by distrofactory.
 - Delete local version of reporegistry and use the one from the
   osbuild/images.
 - Weldr: unify `createWeldrAPI()` and `createWeldrAPI2()` into a single
   `createTestWeldrAPI()` function`.
 - store/fixture: rework fixtures to allow overriding the host distro
   name and host architecture name. A cleanup function to restore the
   host distro and arch names is always part of the fixture struct.
 - Delete `distro_mock` package, since it is no longer used.
 - Bump the required version of osbuild to 98, because the OSCAP
   customization is using the 'compress_results' stage option, which is
   not available in older versions of osbuild.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2024-01-26 11:32:34 +01:00

70 lines
1.9 KiB
Go

package distroidparser
import (
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distro/fedora"
"github.com/osbuild/images/pkg/distro/rhel7"
"github.com/osbuild/images/pkg/distro/rhel8"
"github.com/osbuild/images/pkg/distro/rhel9"
)
var DefaultParser = NewDefaultParser()
type ParserFunc func(idStr string) (*distro.ID, error)
// Parser is a list of distro-specific idStr parsers.
type Parser struct {
parsers []ParserFunc
}
func New(parsers ...ParserFunc) *Parser {
return &Parser{parsers: parsers}
}
// Parse returns the distro.ID that matches the given distro ID string. If no
// distro.ID matches the given distro ID string, it returns nil. If multiple
// distro.IDs match the given distro ID string, it panics.
// If no distro-specific parser matches the given distro ID string, it falls back
// to the default parser.
//
// The fact that the Parser returns a distro.ID does not mean that the distro is
// actually supported or implemented. This functionality is provided as an easy
// and consistent way to parse distro IDs, while allowing distro-specific parsers.
func (p *Parser) Parse(idStr string) (*distro.ID, error) {
var match *distro.ID
for _, f := range p.parsers {
if d, err := f(idStr); err == nil {
if match != nil {
panic("distro ID was matched by multiple parsers")
}
match = d
}
}
// Fall back to the default parser
if match == nil {
return distro.ParseID(idStr)
}
return match, nil
}
// Standardize returns the standardized distro ID string for the given distro ID
// string. If the given distro ID string is not valid, it returns an error.
func (p *Parser) Standardize(idStr string) (string, error) {
id, err := p.Parse(idStr)
if err != nil {
return "", err
}
return id.String(), nil
}
func NewDefaultParser() *Parser {
return New(
fedora.ParseID,
rhel7.ParseID,
rhel8.ParseID,
rhel9.ParseID,
)
}