Skip to content

Commit

Permalink
Merge pull request #321 from sgotti/use_new_error_library
Browse files Browse the repository at this point in the history
*: use new errors handling library
  • Loading branch information
sgotti authored Feb 28, 2022
2 parents 5df9ee1 + d2b09d8 commit 8fe9196
Show file tree
Hide file tree
Showing 183 changed files with 2,912 additions and 2,348 deletions.
25 changes: 24 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
linters:
enable: errorlint
enable:
- errorlint
- wrapcheck

linters-settings:
wrapcheck:
# An array of strings that specify substrings of signatures to ignore.
# If this set, it will override the default set of ignored signatures.
# See https://github.com/tomarrell/wrapcheck#configuration for more information.
ignoreSigs:
- .Errorf(
- errors.New(
- errors.Unwrap(
- .Wrap(
- .Wrapf(
- .WithMessage(
- .WithMessagef(
- .WithStack(
- .NewDetailedError(
ignoreSigRegexps:
- \.New.*Error\(
ignorePackageGlobs:
- encoding/*
- github.com/pkg/*
16 changes: 11 additions & 5 deletions cmd/agola/cmd/agola.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ import (
"time"

"agola.io/agola/cmd"
"agola.io/agola/internal/errors"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var token string

func init() {
cw := zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339Nano,
Out: os.Stderr,
TimeFormat: time.RFC3339Nano,
FormatErrFieldValue: errors.FormatErrFieldValue,
}

zerolog.TimeFieldFormat = time.RFC3339Nano
Expand All @@ -53,6 +54,9 @@ var cmdAgola = &cobra.Command{
if agolaOpts.debug {
log.Logger = log.Level(zerolog.DebugLevel)
}
if agolaOpts.detailedErrors {
zerolog.ErrorMarshalFunc = errors.ErrorMarshalFunc
}
},
Run: func(c *cobra.Command, args []string) {
if err := c.Help(); err != nil {
Expand All @@ -62,8 +66,9 @@ var cmdAgola = &cobra.Command{
}

type agolaOptions struct {
gatewayURL string
debug bool
gatewayURL string
debug bool
detailedErrors bool
}

var agolaOpts agolaOptions
Expand All @@ -84,6 +89,7 @@ func init() {
flags.StringVarP(&agolaOpts.gatewayURL, "gateway-url", "u", gatewayURL, "agola gateway exposed url")
flags.StringVar(&token, "token", token, "api token")
flags.BoolVarP(&agolaOpts.debug, "debug", "d", false, "debug")
flags.BoolVar(&agolaOpts.detailedErrors, "detailed-errors", false, "enabled detailed errors logging")
}

func Execute() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/agola/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"os"

"agola.io/agola/internal/errors"
"github.com/spf13/cobra"
)

Expand All @@ -33,11 +34,11 @@ func completionShell(cmd *cobra.Command, args []string, shell string) error {
switch shell {
case "bash":
if err := cmdAgola.GenBashCompletion(os.Stdout); err != nil {
return err
return errors.WithStack(err)
}
case "zsh":
if err := cmdAgola.GenZshCompletion(os.Stdout); err != nil {
return err
return errors.WithStack(err)
}
}
return nil
Expand Down
32 changes: 16 additions & 16 deletions cmd/agola/cmd/directrunstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"unicode"

"agola.io/agola/internal/errors"
gitsave "agola.io/agola/internal/git-save"
"agola.io/agola/internal/util"
gwapitypes "agola.io/agola/services/gateway/api/types"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/gofrs/uuid"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdDirectRunStart = &cobra.Command{
Expand Down Expand Up @@ -80,15 +80,15 @@ func parseVariable(variable string) (string, string, error) {
variable = strings.TrimLeftFunc(variable, unicode.IsSpace)
arr := strings.SplitN(variable, "=", 2)
if len(arr) != 2 {
return "", "", fmt.Errorf("invalid variable definition: %s", variable)
return "", "", errors.Errorf("invalid variable definition: %s", variable)
}
varname := arr[0]
varvalue := arr[1]
if varname == "" {
return "", "", fmt.Errorf("invalid variable definition: %s", variable)
return "", "", errors.Errorf("invalid variable definition: %s", variable)
}
if varvalue == "" {
return "", "", fmt.Errorf("invalid variable definition: %s", variable)
return "", "", errors.Errorf("invalid variable definition: %s", variable)
}
return varname, varvalue, nil
}
Expand All @@ -98,7 +98,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {

for _, res := range directRunStartOpts.prRefRegexes {
if _, err := regexp.Compile(res); err != nil {
return fmt.Errorf("wrong regular expression %q: %w", res, err)
return errors.Wrapf(err, "wrong regular expression %q", res)
}
}

Expand All @@ -123,12 +123,12 @@ func directRunStart(cmd *cobra.Command, args []string) error {
branch = ""
}
if set > 1 {
return fmt.Errorf(`only one of "--branch", "--tag" or "--ref" can be provided`)
return errors.Errorf(`only one of "--branch", "--tag" or "--ref" can be provided`)
}

user, _, err := gwclient.GetCurrentUser(context.TODO())
if err != nil {
return err
return errors.WithStack(err)
}

variables := map[string]string{}
Expand All @@ -142,11 +142,11 @@ func directRunStart(cmd *cobra.Command, args []string) error {
var err error
data, err = ioutil.ReadFile(varFile)
if err != nil {
return err
return errors.WithStack(err)
}

if err := yaml.Unmarshal(data, &variables); err != nil {
return errors.Errorf("failed to unmarshal values: %w", err)
return errors.Wrapf(err, "failed to unmarshal values")
}

// TODO(sgotti) validate variable name
Expand All @@ -155,7 +155,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {
for _, variable := range directRunStartOpts.vars {
varname, varvalue, err := parseVariable(variable)
if err != nil {
return err
return errors.WithStack(err)
}
variables[varname] = varvalue
}
Expand All @@ -166,7 +166,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {
if repoUUID == "" {
repoUUID = uuid.Must(uuid.NewV4()).String()
if _, err := git.ConfigSet(context.Background(), "agola.repouuid", repoUUID); err != nil {
return fmt.Errorf("failed to set agola repo uid in git config: %w", err)
return errors.Wrapf(err, "failed to set agola repo uid in git config")
}
}

Expand All @@ -180,7 +180,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {

commitSHA, err := gs.Save(message, localBranch)
if err != nil {
return err
return errors.WithStack(err)
}

log.Info().Msgf("pushing branch")
Expand All @@ -190,15 +190,15 @@ func directRunStart(cmd *cobra.Command, args []string) error {
// push to a branch with default branch refs "refs/heads/branch"
if branch != "" {
if err := gitsave.GitPush("", repoURL, fmt.Sprintf("%s:refs/heads/%s", path.Join(gs.RefsPrefix(), localBranch), branch)); err != nil {
return err
return errors.WithStack(err)
}
} else if tag != "" {
if err := gitsave.GitPush("", repoURL, fmt.Sprintf("%s:refs/tags/%s", path.Join(gs.RefsPrefix(), localBranch), tag)); err != nil {
return err
return errors.WithStack(err)
}
} else if ref != "" {
if err := gitsave.GitPush("", repoURL, fmt.Sprintf("%s:%s", path.Join(gs.RefsPrefix(), localBranch), ref)); err != nil {
return err
return errors.WithStack(err)
}
}

Expand All @@ -215,7 +215,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {
Variables: variables,
}
if _, err := gwclient.UserCreateRun(context.TODO(), req); err != nil {
return err
return errors.WithStack(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/agola/cmd/logdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package cmd
import (
"context"

"agola.io/agola/internal/errors"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdLogDelete = &cobra.Command{
Expand Down Expand Up @@ -93,7 +93,7 @@ func logDelete(cmd *cobra.Command, args []string) error {

run, _, err := gwclient.GetRun(context.TODO(), logDeleteOpts.runid)
if err != nil {
return err
return errors.WithStack(err)
}
for _, t := range run.Tasks {
if t.Name == logDeleteOpts.taskname {
Expand Down
6 changes: 3 additions & 3 deletions cmd/agola/cmd/logget.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
"io"
"os"

"agola.io/agola/internal/errors"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdLogGet = &cobra.Command{
Expand Down Expand Up @@ -102,7 +102,7 @@ func logGet(cmd *cobra.Command, args []string) error {

run, _, err := gwclient.GetRun(context.TODO(), logGetOpts.runid)
if err != nil {
return err
return errors.WithStack(err)
}
for _, t := range run.Tasks {
if t.Name == logGetOpts.taskname {
Expand All @@ -127,7 +127,7 @@ func logGet(cmd *cobra.Command, args []string) error {
if flags.Changed("output") {
f, err := os.Create(logGetOpts.output)
if err != nil {
return err
return errors.WithStack(err)
}
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/agola/cmd/orgcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package cmd
import (
"context"

"agola.io/agola/internal/errors"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdOrgCreate = &cobra.Command{
Expand Down Expand Up @@ -71,7 +71,7 @@ func orgCreate(cmd *cobra.Command, args []string) error {
log.Info().Msgf("creating org")
org, _, err := gwclient.CreateOrg(context.TODO(), req)
if err != nil {
return errors.Errorf("failed to create org: %w", err)
return errors.Wrapf(err, "failed to create org")
}
log.Info().Msgf("org %q created, ID: %q", org.Name, org.ID)

Expand Down
4 changes: 2 additions & 2 deletions cmd/agola/cmd/orgdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package cmd
import (
"context"

"agola.io/agola/internal/errors"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdOrgDelete = &cobra.Command{
Expand Down Expand Up @@ -57,7 +57,7 @@ func orgDelete(cmd *cobra.Command, args []string) error {

log.Info().Msgf("deleting organization %q", orgDeleteOpts.name)
if _, err := gwclient.DeleteOrg(context.TODO(), orgDeleteOpts.name); err != nil {
return errors.Errorf("failed to delete organization: %w", err)
return errors.Wrapf(err, "failed to delete organization")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/agola/cmd/orgmemberadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package cmd
import (
"context"

"agola.io/agola/internal/errors"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdOrgMemberAdd = &cobra.Command{
Expand Down Expand Up @@ -66,7 +66,7 @@ func orgMemberAdd(cmd *cobra.Command, args []string) error {
log.Info().Msgf("adding/updating member %q to organization %q with role %q", orgMemberAddOpts.username, orgMemberAddOpts.orgname, orgMemberAddOpts.role)
_, _, err := gwclient.AddOrgMember(context.TODO(), orgMemberAddOpts.orgname, orgMemberAddOpts.username, gwapitypes.MemberRole(orgMemberAddOpts.role))
if err != nil {
return errors.Errorf("failed to add/update organization member: %w", err)
return errors.Wrapf(err, "failed to add/update organization member")
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/agola/cmd/orgmemberlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"encoding/json"
"os"

"agola.io/agola/internal/errors"
gwclient "agola.io/agola/services/gateway/client"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)

var cmdOrgMemberList = &cobra.Command{
Expand Down Expand Up @@ -59,12 +59,12 @@ func orgMemberList(cmd *cobra.Command, args []string) error {

orgMembers, _, err := gwclient.GetOrgMembers(context.TODO(), orgMemberListOpts.orgname)
if err != nil {
return errors.Errorf("failed to get organization member: %w", err)
return errors.Wrapf(err, "failed to get organization member")
}

out, err := json.MarshalIndent(orgMembers, "", "\t")
if err != nil {
return err
return errors.WithStack(err)
}
os.Stdout.Write(out)

Expand Down
Loading

0 comments on commit 8fe9196

Please # to comment.