Skip to content

Commit

Permalink
refactor: move error to cmd internal packages
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
junczhu authored and qweeah committed Aug 15, 2022
1 parent c7dd5ac commit b19c639
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cmd/oras/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
)

Expand Down Expand Up @@ -90,7 +91,7 @@ func runAttach(opts attachOptions) error {
return err
}
if dst.Reference.Reference == "" {
return newErrInvalidReference(dst.Reference)
return errors.NewErrInvalidReference(dst.Reference)
}
ociSubject, err := dst.Resolve(ctx, dst.Reference.Reference)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ func runCopy(opts copyOptions) error {
}

if src.Reference.Reference == "" {
return newErrInvalidReference(src.Reference)
return errors.NewErrInvalidReference(src.Reference)
}

var desc ocispec.Descriptor
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/errors"
)

type discoverOptions struct {
Expand Down Expand Up @@ -79,7 +80,7 @@ func runDiscover(opts discoverOptions) error {
return err
}
if repo.Reference.Reference == "" {
return newErrInvalidReference(repo.Reference)
return errors.NewErrInvalidReference(repo.Reference)
}

// discover artifacts
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/errors.go → cmd/oras/internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package errors

import (
"fmt"

"oras.land/oras-go/v2/registry"
)

// newErrInvalidReference creates a new error based on the reference string.
func newErrInvalidReference(ref registry.Reference) error {
// NewErrInvalidReference creates a new error based on the reference string.
func NewErrInvalidReference(ref registry.Reference) error {
return fmt.Errorf("%s: invalid image reference, expecting <name:tag|name@digest>", ref)
}
3 changes: 2 additions & 1 deletion cmd/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/content/oci"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/cache"
)
Expand Down Expand Up @@ -88,7 +89,7 @@ func runPull(opts pullOptions) error {
return err
}
if repo.Reference.Reference == "" {
return newErrInvalidReference(repo.Reference)
return errors.NewErrInvalidReference(repo.Reference)
}
var src oras.Target = repo
if opts.cacheRoot != "" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
oras.land/oras-go/v2 v2.0.0-20220804053649-e135557babfa
oras.land/oras-go/v2 v2.0.0-rc.2
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
oras.land/oras-go/v2 v2.0.0-20220804053649-e135557babfa h1:/K4LND5cuAwZTGZ793qPQOppK8+vNX1JvSFNIEcUDKM=
oras.land/oras-go/v2 v2.0.0-20220804053649-e135557babfa/go.mod h1:IZRIoIJqkAH6x0pL3tVnpyPUyZgthjSyPcH2kgJvBMo=
oras.land/oras-go/v2 v2.0.0-rc.2 h1:dks9BxPg6HQOxn5+jVNuTFl45FuYvHfLQ6wcP7hVRdE=
oras.land/oras-go/v2 v2.0.0-rc.2/go.mod h1:IZRIoIJqkAH6x0pL3tVnpyPUyZgthjSyPcH2kgJvBMo=

0 comments on commit b19c639

Please sign in to comment.