diff --git a/snap/naming/componentref.go b/snap/naming/componentref.go index e6e1cdf054c4..1d00eb59df7f 100644 --- a/snap/naming/componentref.go +++ b/snap/naming/componentref.go @@ -35,6 +35,17 @@ func NewComponentRef(snapName, componentName string) ComponentRef { return ComponentRef{SnapName: snapName, ComponentName: componentName} } +// ParseComponentRef parses a string in the form + into a +// ComponentRef. +func ParseComponentRef(s string) (ComponentRef, error) { + snapName, componentName, err := SplitFullComponentName(s) + if err != nil { + return ComponentRef{}, err + } + + return ComponentRef{SnapName: snapName, ComponentName: componentName}, nil +} + // SplitFullComponentName splits + in and strings. func SplitFullComponentName(fullComp string) (string, string, error) { names := strings.Split(fullComp, "+") diff --git a/snap/naming/componentref_test.go b/snap/naming/componentref_test.go index d0043b5d2f93..7d67a7c4304b 100644 --- a/snap/naming/componentref_test.go +++ b/snap/naming/componentref_test.go @@ -85,3 +85,13 @@ func (s *componentRefSuite) TestSplitFullComponentNameErr(c *C) { c.Check(comp, Equals, "") } } + +func (s *componentRefSuite) TestParseComponentRef(c *C) { + cr, err := naming.ParseComponentRef("foo+bar") + c.Check(err, IsNil) + c.Check(cr.SnapName, Equals, "foo") + c.Check(cr.ComponentName, Equals, "bar") + + cr, err = naming.ParseComponentRef("foo+bar+") + c.Assert(err, ErrorMatches, `.*incorrect component name "foo\+bar\+"`) +}