Skip to content

Commit

Permalink
Add policy CLI errors package
Browse files Browse the repository at this point in the history
  • Loading branch information
carabasdaniel committed Nov 29, 2023
1 parent 2ea290f commit 9a10e07
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 44 deletions.
6 changes: 4 additions & 2 deletions cmd/policy/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/pkg/errors"
import (
perr "github.com/opcr-io/policy/pkg/errors"
)

type BuildCmd struct {
Tag string `name:"tag" short:"t" help:"Name and optionally a tag in the 'name:tag' format, if not provided it will be 'default:latest'"`
Expand Down Expand Up @@ -44,7 +46,7 @@ func (c *BuildCmd) Run(g *Globals) error {
c.ClaimsFile,
)
if err != nil {
return errors.Wrap(err, "build failed")
return perr.BuildFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
4 changes: 2 additions & 2 deletions cmd/policy/images.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/pkg/errors"
"github.com/opcr-io/policy/pkg/errors"
)

type ImagesCmd struct {
Expand All @@ -14,7 +14,7 @@ func (c *ImagesCmd) Run(g *Globals) error {

err := g.App.Images()
if err != nil {
return errors.Wrap(err, "failed to list local policies")
return errors.ImagesFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
4 changes: 2 additions & 2 deletions cmd/policy/inspect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/pkg/errors"
import "github.com/opcr-io/policy/pkg/errors"

type InspectCmd struct {
Policy string `arg:"" name:"policy" help:"Policy to inspect."`
Expand All @@ -9,7 +9,7 @@ type InspectCmd struct {
func (c *InspectCmd) Run(g *Globals) error {
err := g.App.Inspect(c.Policy)
if err != nil {
return errors.Wrap(err, "failed to inspect policy")
return errors.InspectFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
19 changes: 10 additions & 9 deletions cmd/policy/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/docker/cli/cli/config/types"

perr "github.com/opcr-io/policy/pkg/errors"
"github.com/pkg/errors"
"golang.org/x/term"
)
Expand All @@ -25,34 +26,34 @@ func (c *LoginCmd) Run(g *Globals) error {
g.App.UI.Exclamation().Msg("Using --password via the CLI is insecure. Use --password-stdin.")

if c.PasswordStdin {
return errors.New("--password and --password-stdin are mutually exclusive")
return perr.LoginFailed.WithError(errors.New("--password and --password-stdin are mutually exclusive"))
}
}

if c.PasswordStdin {
if c.Username == "" {
return errors.New("Must provide --username with --password-stdin")
return perr.LoginFailed.WithError(errors.New("Must provide --username with --password-stdin"))
}

contents, err := io.ReadAll(g.App.UI.Input())
if err != nil {
return err
return perr.LoginFailed.WithError(err)
}

c.Password = strings.TrimSuffix(string(contents), "\n")
c.Password = strings.TrimSuffix(c.Password, "\r")
}

if c.Server == "" {
return errors.New("Must provide --server")
return perr.LoginFailed.WithError(errors.New("Must provide --server"))
}

password := c.Password
if c.Password == "" {
g.App.UI.Normal().NoNewline().Msg("Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin)) // nolint:unconvert // needed for windows
if err != nil {
return errors.Wrap(err, "failed to read password from stdin")
return perr.LoginFailed.WithError(err)
}

password = string(bytePassword)
Expand All @@ -64,12 +65,12 @@ func (c *LoginCmd) Run(g *Globals) error {
Msg("Logging in.")
err := g.App.Ping(c.Server, c.Username, password)
if err != nil {
return err
return perr.LoginFailed.WithError(err)
}
var setDefault bool
stat, err := os.Stdin.Stat()
if err != nil {
return err
return perr.LoginFailed.WithError(err)
}

if (stat.Mode() & os.ModeCharDevice) == 0 {
Expand All @@ -85,7 +86,7 @@ func (c *LoginCmd) Run(g *Globals) error {
Password: password,
})
if err != nil {
return err
return perr.LoginFailed.WithError(err)
}

if setDefault {
Expand All @@ -94,7 +95,7 @@ func (c *LoginCmd) Run(g *Globals) error {

err = g.App.Configuration.SaveDefaultDomain()
if err != nil {
return err
return perr.LoginFailed.WithError(err)
}

g.App.UI.Normal().Msg("OK.")
Expand Down
4 changes: 3 additions & 1 deletion cmd/policy/logout.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "github.com/opcr-io/policy/pkg/errors"

type LogoutCmd struct {
Server string `name:"server" short:"s" help:"Server to logout from." default:"{{ .DefaultDomain }}"`
}
Expand All @@ -11,7 +13,7 @@ func (c *LogoutCmd) Run(g *Globals) error {

err := g.App.RemoveServerCreds(c.Server)
if err != nil {
return err
return errors.LogoutFailed.WithError(err)
}

g.App.UI.Normal().Msg("OK.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/policy/save.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/pkg/errors"
import "github.com/opcr-io/policy/pkg/errors"

type SaveCmd struct {
Policy string `arg:"" name:"policy" help:"Policy to save."`
Expand All @@ -10,7 +10,7 @@ type SaveCmd struct {
func (c *SaveCmd) Run(g *Globals) error {
err := g.App.Save(c.Policy, c.File)
if err != nil {
return errors.Wrap(err, "failed to save local bundle tarball")
return errors.SaveFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
4 changes: 2 additions & 2 deletions cmd/policy/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/pkg/errors"
import "github.com/opcr-io/policy/pkg/errors"

type TagCmd struct {
Policy string `arg:"" name:"policy" help:"Source policy name." type:"string"`
Expand All @@ -10,7 +10,7 @@ type TagCmd struct {
func (c *TagCmd) Run(g *Globals) error {
err := g.App.Tag(c.Policy, c.Tag)
if err != nil {
return errors.Wrap(err, "tagging failed")
return errors.TagFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
6 changes: 3 additions & 3 deletions cmd/policy/templates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/pkg/errors"
import "github.com/opcr-io/policy/pkg/errors"

type TemplatesCmd struct {
Apply ApplyCmd `cmd:"" name:"apply" help:"Create or update a policy or related artifacts from a template."`
Expand All @@ -19,7 +19,7 @@ type ListCmd struct {
func (c *ApplyCmd) Run(g *Globals) error {
err := g.App.TemplateApply(c.Template, c.Output, c.Overwrite)
if err != nil {
return errors.Wrapf(err, "failed to apply template '%s'", c.Template)
return errors.TemplateFailed.WithError(err)
}

<-g.App.Context.Done()
Expand All @@ -29,7 +29,7 @@ func (c *ApplyCmd) Run(g *Globals) error {
func (c *ListCmd) Run(g *Globals) error {
err := g.App.TemplatesList()
if err != nil {
return errors.Wrap(err, "failed to list templates")
return errors.TemplateFailed.WithError(err)
}

<-g.App.Context.Done()
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ package app
import (
"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/pkg/errors"
"github.com/opcr-io/policy/pkg/errors"
)

func (c *PolicyApp) Push(userRef string) error {
defer c.Cancel()

ref, err := parser.CalculatePolicyRef(userRef, c.Configuration.DefaultDomain)
if err != nil {
return err
return errors.PushFailed.WithError(err)
}

ociClient, err := oci.NewOCI(c.Context, c.Logger, c.getHosts, c.Configuration.PoliciesRoot())
if err != nil {
return err
return errors.PushFailed.WithError(err)
}

refs, err := ociClient.ListReferences()
if err != nil {
return err
return errors.PushFailed.WithError(err)
}

refDescriptor, ok := refs[ref]
if !ok {
return errors.Errorf("policy [%s] not found in the local store", ref)
return errors.NotFound.WithMessage("policy [%s] not in the local store", ref)
}

c.UI.Normal().
Expand All @@ -36,7 +36,7 @@ func (c *PolicyApp) Push(userRef string) error {
digest, err := ociClient.Push(ref)

if err != nil {
return err
return errors.PushFailed.WithError(err)
}

c.UI.Normal().
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/aserto-dev/runtime"
"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/opcr-io/policy/pkg/errors"
"github.com/open-policy-agent/opa/repl"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

func (c *PolicyApp) Repl(ref string, maxErrors int) error {
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *PolicyApp) Repl(ref string, maxErrors int) error {
}
descriptor, ok = existingRefs[existingRefParsed]
if !ok {
return errors.Errorf("ref [%s] not found in the local store", ref)
return errors.NotFound.WithMessage("policy [%s] not in the local store", ref)
}
}

Expand All @@ -68,18 +68,18 @@ func (c *PolicyApp) Repl(ref string, maxErrors int) error {
},
})
if err != nil {
return errors.Wrap(err, "failed to setup the OPA runtime")
return errors.ReplFailed.WithError(err)
}
defer cleanup()

err = opaRuntime.Start(c.Context)
if err != nil {
return errors.Wrap(err, "OPA runtime failed to start")
return errors.ReplFailed.WithError(err)
}

err = opaRuntime.WaitForPlugins(c.Context, time.Minute*1)
if err != nil {
return errors.Wrap(err, "plugins didn't start on time")
return errors.ReplFailed.WithError(err)
}

loop := repl.New(opaRuntime.GetPluginsManager().Store, c.Configuration.ReplHistoryFile(), c.UI.Output(), "", maxErrors, fmt.Sprintf("running policy [%s]", ref))
Expand All @@ -98,7 +98,7 @@ func (c *PolicyApp) getBundleHex(ociClient *oci.Oci, descriptor *ocispec.Descrip
}
bundleHex = bundleDescriptor.Digest.Hex()
if bundleHex == "" {
return "", errors.New("current manifest does not contain a MediaTypeImageLayerGzip")
return "", errors.ReplFailed.WithMessage("current manifest does not contain a MediaTypeImageLayerGzip")
}
} else {
bundleHex = descriptor.Digest.Hex()
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/opcr-io/policy/pkg/errors"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

func (c *PolicyApp) Rm(existingRef string, force bool) error {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (c *PolicyApp) Rm(existingRef string, force bool) error {

ref, ok := existingRefs[existingRefParsed]
if !ok {
return errors.Errorf("ref [%s] not found in the local store", existingRef)
return errors.NotFound.WithMessage("policy [%s] not in the local store", existingRef)
}
// attach ref name annotation for comparison.
if len(ref.Annotations) == 0 || ref.Annotations[ocispec.AnnotationRefName] == "" {
Expand Down
14 changes: 7 additions & 7 deletions pkg/app/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (

"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/opcr-io/policy/pkg/errors"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

func (c *PolicyApp) Save(userRef, outputFilePath string) error {
defer c.Cancel()
var outputFile *os.File
ref, err := parser.CalculatePolicyRef(userRef, c.Configuration.DefaultDomain)
if err != nil {
return err
return errors.SaveFailed.WithError(err)
}

ociClient, err := oci.NewOCI(c.Context, c.Logger, c.getHosts, c.Configuration.PoliciesRoot())
if err != nil {
return err
return errors.SaveFailed.WithError(err)
}

// if the reference descriptor is the manifest get the tarball descriptor information from the manifest layers.
refDescriptor, err := c.getRefDescriptor(ociClient, ref)
if err != nil {
return err
return errors.SaveFailed.WithError(err)
}

if outputFilePath == "-" {
Expand All @@ -39,7 +39,7 @@ func (c *PolicyApp) Save(userRef, outputFilePath string) error {
outputFile, err = os.Create(outputFilePath)

if err != nil {
return errors.Wrapf(err, "failed to create output file [%s]", outputFilePath)
return errors.SaveFailed.WithError(err).WithMessage("failed to create output file [%s]", outputFilePath)
}

defer func() {
Expand All @@ -52,7 +52,7 @@ func (c *PolicyApp) Save(userRef, outputFilePath string) error {

err = c.writePolicy(ociClient, refDescriptor, outputFile)
if err != nil {
return err
return errors.SaveFailed.WithError(err)
}

return nil
Expand All @@ -66,7 +66,7 @@ func (c *PolicyApp) getRefDescriptor(ociClient *oci.Oci, ref string) (*ocispec.D

refDescriptor, ok := refs[ref]
if !ok {
return nil, errors.Errorf("Image %s not found", ref)
return nil, errors.NotFound.WithMessage("policy [%s] not in the local store", ref)
}

if refDescriptor.MediaType == ocispec.MediaTypeImageManifest {
Expand Down
Loading

0 comments on commit 9a10e07

Please sign in to comment.