Skip to content

Commit

Permalink
Merge pull request #142 from CircleCI-Public/error-messages
Browse files Browse the repository at this point in the history
Error messages
  • Loading branch information
Zachary Scott authored Oct 4, 2018
2 parents 5218bdd + a3f92e0 commit 1cf5bcf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
11 changes: 8 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/CircleCI-Public/circleci-cli/client"
"github.com/CircleCI-Public/circleci-cli/logger"
"github.com/CircleCI-Public/circleci-cli/references"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
"github.com/spf13/viper"
Expand Down Expand Up @@ -763,6 +764,11 @@ func orbVersionRef(orb string) string {

// OrbSource gets the source or an orb
func OrbSource(ctx context.Context, logger *logger.Logger, orbRef string) (string, error) {

if err := references.IsOrbRefWithOptionalVersion(orbRef); err != nil {
return "", err
}

ref := orbVersionRef(orbRef)

var response struct {
Expand Down Expand Up @@ -792,16 +798,15 @@ func OrbSource(ctx context.Context, logger *logger.Logger, orbRef string) (strin
if err != nil {
return "", err
}
graphQLclient := client.NewClient(address, logger)

err = graphQLclient.Run(ctx, request, &response)
err = client.NewClient(address, logger).Run(ctx, request, &response)

if err != nil {
return "", err
}

if response.OrbVersion.ID == "" {
return "", fmt.Errorf("the %s orb has never published a revision", orbRef)
return "", fmt.Errorf("no Orb '%s' was found; please check that the Orb reference is correct", orbRef)
}

return response.OrbVersion.Source, nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func promoteOrb(cmd *cobra.Command, args []string) error {
return err
}

if !references.IsDevVersion(version) {
return fmt.Errorf("The version '%s' must be a dev version (the string should begin `dev:`)", version)
if err = references.IsDevVersion(version); err != nil {
return err
}

response, err := api.OrbPromote(context.Background(), Logger, namespace, orb, version, segment)
Expand Down
36 changes: 32 additions & 4 deletions references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,46 @@ func SplitIntoOrbAndNamespace(ref string) (namespace, orb string, err error) {
// SplitIntoOrbNamespaceAndVersion splits ref into namespace, orb and version.
func SplitIntoOrbNamespaceAndVersion(ref string) (namespace, orb, version string, err error) {

errorMessage := fmt.Errorf("Invalid orb reference '%s': Expected a namespace, orb and version in the format 'namespace/orb@version'", ref)

if len(strings.Split(ref, "/")) != 2 {
return "", "", "", errorMessage
}

re := regexp.MustCompile("^(.+)/(.+)@(.+)$")

matches := re.FindStringSubmatch(ref)

if len(matches) != 4 {
return "", "", "", fmt.Errorf("Invalid orb reference '%s': Expected a namespace, orb and version in the format 'namespace/orb@version'", ref)
return "", "", "", errorMessage
}

return matches[1], matches[2], matches[3], nil
}

// IsDevVersion returns true iff `version` is of the form dev:...
func IsDevVersion(version string) bool {
return strings.HasPrefix(version, "dev:")
// IsDevVersion returns a helpful error unless `version` is of the form dev:...
func IsDevVersion(version string) error {
if !strings.HasPrefix(version, "dev:") {
return fmt.Errorf("The version '%s' must be a dev version (the string should begin `dev:`)", version)
}
return nil
}

// IsOrbRefWithOptionalVersion returns an error unless ref is a valid orb reference with optional version.
func IsOrbRefWithOptionalVersion(ref string) error {

_, _, _, err := SplitIntoOrbNamespaceAndVersion(ref)

if err == nil {
return nil
}

_, _, err = SplitIntoOrbAndNamespace(ref)

if err == nil {
return nil
}

return fmt.Errorf("Invalid orb reference '%s': expected a string of the form namespace/orb or namespace/orb@version", ref)

}
6 changes: 3 additions & 3 deletions references/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
var _ = Describe("Parsing Orbs", func() {

It("Should parse dev versions", func() {
Expect(references.IsDevVersion("dev:master")).Should(BeTrue())
Expect(references.IsDevVersion("1.2.1")).Should(BeFalse())
Expect(references.IsDevVersion("")).Should(BeFalse())
Expect(references.IsDevVersion("dev:master")).ToNot(HaveOccurred())
Expect(references.IsDevVersion("1.2.1")).To(HaveOccurred())
Expect(references.IsDevVersion("")).To(HaveOccurred())
})

It("Should parse full references", func() {
Expand Down

0 comments on commit 1cf5bcf

Please sign in to comment.