Skip to content

Commit f4f92a3

Browse files
committed
Fix error wrapping
1 parent 27b382c commit f4f92a3

37 files changed

+155
-173
lines changed

go/build.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"github.com/candiddev/etcha/go/config"
88
"github.com/candiddev/etcha/go/pattern"
99
"github.com/candiddev/shared/go/cli"
10-
"github.com/candiddev/shared/go/errs"
1110
)
1211

1312
var build = cli.Command[*config.Config]{ //nolint:gochecknoglobals
1413
ArgumentsRequired: []string{
1514
"pattern path",
1615
"destination path",
1716
},
18-
Run: func(ctx context.Context, args []string, _ cli.Flags, c *config.Config) errs.Err {
17+
Run: func(ctx context.Context, args []string, _ cli.Flags, c *config.Config) error {
1918
source := args[1]
2019
destination := args[2]
2120

go/commands/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type CommandRunOpts struct {
5151
}
5252

5353
// Run will run the Command script for the given Mode.
54-
func (cmd *Command) Run(ctx context.Context, c cli.Config, exec Exec, opts CommandRunOpts) (out *Output, newEnv types.EnvVars, err errs.Err) { //nolint:gocognit,gocyclo
54+
func (cmd *Command) Run(ctx context.Context, c cli.Config, exec Exec, opts CommandRunOpts) (out *Output, newEnv types.EnvVars, err error) { //nolint:gocognit,gocyclo
5555
cfg := exec.Override(cmd.Exec)
5656
ctx = metrics.SetCommandID(ctx, cmd.ID)
5757

@@ -179,14 +179,14 @@ func (cmd *Command) Run(ctx context.Context, c cli.Config, exec Exec, opts Comma
179179
newEnv[cmd.EnvPrefix+"_REMOVE"] = "1"
180180
newEnv[cmd.EnvPrefix+"_REMOVE_OUT"] = out.Remove.String()
181181

182-
err = logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error removing id %s", id)).Wrap(err.Unwrap()...), out.Remove.String())
182+
err = logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error removing id %s", id), err), out.Remove.String())
183183
} else {
184184
out.Change = o
185185
out.ChangeFail = true
186186
newEnv[cmd.EnvPrefix+"_CHANGE"] = "1"
187187
newEnv[cmd.EnvPrefix+"_CHANGE_OUT"] = out.Change.String()
188188

189-
err = logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error changing id %s", id)).Wrap(err.Unwrap()...).Wrap(errors.New(out.Change.String())))
189+
err = logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error changing id %s", id), err).Wrap(errors.New(out.Change.String())))
190190
}
191191

192192
metrics.CollectCommands(ctx, true)

go/commands/commands.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type CommandsRunOpts struct { //nolint:revive
100100
}
101101

102102
// Run the commands, either as change (default) or remove, and optionally as check only.
103-
func (cmds Commands) Run(ctx context.Context, c cli.Config, exe *Exec, opts CommandsRunOpts) (out Outputs, err errs.Err) { //nolint:gocognit,gocyclo
103+
func (cmds Commands) Run(ctx context.Context, c cli.Config, exe *Exec, opts CommandsRunOpts) (out Outputs, err error) { //nolint:gocognit,gocyclo
104104
cout := Outputs{}
105105

106106
var exec Exec
@@ -180,7 +180,7 @@ func (cmds Commands) Run(ctx context.Context, c cli.Config, exe *Exec, opts Comm
180180
for k := range cmd.OnFail {
181181
r, err := regexp.Compile(cmd.OnFail[k])
182182
if err != nil { //nolint:revive
183-
return cout, logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error parsing onFail %s for %s: %w", cmd.OnFail[k], cmd.ID, err)))
183+
return cout, logger.Error(ctx, fmt.Errorf("error parsing onFail %s for %s: %w", cmd.OnFail[k], cmd.ID, err))
184184
}
185185

186186
if r.MatchString(cmds[j].ID) { //nolint:revive
@@ -194,7 +194,7 @@ func (cmds Commands) Run(ctx context.Context, c cli.Config, exe *Exec, opts Comm
194194
}
195195

196196
if run {
197-
var e errs.Err
197+
var e error
198198

199199
out := &Output{
200200
Changed: true,
@@ -204,7 +204,7 @@ func (cmds Commands) Run(ctx context.Context, c cli.Config, exe *Exec, opts Comm
204204
cout = append(cout, out)
205205

206206
if out.Change, e = cfg.Run(ctx, c, cmds[j].Change); e != nil { //nolint:revive
207-
logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error changing id %s", cmds[j].ID)).Wrap(e.Unwrap()...), out.Change.String()) //nolint:errcheck
207+
logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error changing id %s", cmds[j].ID), e), out.Change.String()) //nolint:errcheck
208208
}
209209
}
210210
}

go/commands/exec.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/candiddev/shared/go/cli"
10-
"github.com/candiddev/shared/go/errs"
1110
"github.com/candiddev/shared/go/logger"
1211
"github.com/candiddev/shared/go/types"
1312
)
@@ -56,7 +55,7 @@ func (e Exec) Override(o ...*Exec) *Exec {
5655
return &out
5756
}
5857

59-
func (e *Exec) RunOpts(ctx context.Context, script string) (cli.RunOpts, errs.Err) {
58+
func (e *Exec) RunOpts(ctx context.Context, script string) (cli.RunOpts, error) {
6059
var s []string
6160

6261
if e.Command == "" {
@@ -104,7 +103,7 @@ func (e *Exec) RunOpts(ctx context.Context, script string) (cli.RunOpts, errs.Er
104103
}
105104

106105
// Run will run a script using the Exec.
107-
func (e *Exec) Run(ctx context.Context, c cli.Config, script string) (cli.CmdOutput, errs.Err) {
106+
func (e *Exec) Run(ctx context.Context, c cli.Config, script string) (cli.CmdOutput, error) {
108107
opts, err := e.RunOpts(ctx, script)
109108
if err != nil {
110109
return "", logger.Error(ctx, err)

go/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var compare = cli.Command[*config.Config]{ //nolint:gochecknoglobals
2222
Usage: "Ignore JWT etchaVersion differences",
2323
},
2424
},
25-
Run: func(ctx context.Context, args []string, flags cli.Flags, c *config.Config) errs.Err {
25+
Run: func(ctx context.Context, args []string, flags cli.Flags, c *config.Config) error {
2626
ctx = logger.SetLevel(ctx, logger.LevelError)
2727
j1, _, err := pattern.ParseJWTFromPath(ctx, c, "", args[1])
2828
if err != nil {

go/config/config.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ func (c *Config) CLIConfig() *cli.Config {
123123
return &c.CLI
124124
}
125125

126+
func (c *Config) Show() any {
127+
return &c
128+
}
129+
126130
func Default() *Config {
127131
return &Config{
128132
Build: Build{
@@ -198,7 +202,7 @@ func GetSysInfo() *SysInfo {
198202
return &s
199203
}
200204

201-
func (c *Config) Parse(ctx context.Context, configArgs []string) errs.Err {
205+
func (c *Config) Parse(ctx context.Context, configArgs []string) error {
202206
if err := config.Parse(ctx, c, configArgs, "ETCHA", c.CLI.ConfigPath); err != nil {
203207
return logger.Error(ctx, err)
204208
}
@@ -320,20 +324,20 @@ func (c *Config) ParseJWT(ctx context.Context, customClaims any, token string, s
320324
}
321325

322326
// ParseJWTFile reads a JWT file path and parses the token into customClaims.
323-
func (c *Config) ParseJWTFile(ctx context.Context, customClaims any, path, source string) (key cryptolib.Key[cryptolib.KeyProviderPublic], r *jwt.RegisteredClaims, err errs.Err) {
324-
s, e := os.ReadFile(path)
325-
if e != nil {
326-
return key, r, logger.Error(ctx, errs.ErrReceiver.Wrap(ErrParseJWT, e))
327+
func (c *Config) ParseJWTFile(ctx context.Context, customClaims any, path, source string) (key cryptolib.Key[cryptolib.KeyProviderPublic], r *jwt.RegisteredClaims, err error) {
328+
s, err := os.ReadFile(path)
329+
if err != nil {
330+
return key, r, logger.Error(ctx, errs.ErrReceiver.Wrap(ErrParseJWT, err))
327331
}
328332

329-
if key, r, e = c.ParseJWT(ctx, customClaims, string(s), source); e != nil {
330-
return key, r, logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error parsing JWT %s", path), e))
333+
if key, r, err = c.ParseJWT(ctx, customClaims, string(s), source); err != nil {
334+
return key, r, logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error parsing JWT %s", path), err))
331335
}
332336

333337
return key, r, logger.Error(ctx, nil)
334338
}
335339

336-
func (c *Config) SignJWT(ctx context.Context, j *jwt.Token) (string, errs.Err) {
340+
func (c *Config) SignJWT(ctx context.Context, j *jwt.Token) (string, error) {
337341
if c.Build.key.IsNil() {
338342
key, err := cryptolib.ParseKey[cryptolib.KeyProviderPrivate](c.Build.SigningKey)
339343
if err != nil {
@@ -377,7 +381,7 @@ func (c *Config) SignJWT(ctx context.Context, j *jwt.Token) (string, errs.Err) {
377381
}
378382

379383
if err := j.Sign(c.Build.key); err != nil {
380-
return "", logger.Error(ctx, errs.ErrReceiver.Wrap(err))
384+
return "", logger.Error(ctx, err)
381385
}
382386

383387
return j.String(), logger.Error(ctx, nil)

go/copy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var copyCmd = cli.Command[*config.Config]{ //nolint:gochecknoglobals
2525
"src path",
2626
"dst path or - for stdout",
2727
},
28-
Run: func(ctx context.Context, args []string, _ cli.Flags, _ *config.Config) errs.Err {
28+
Run: func(ctx context.Context, args []string, _ cli.Flags, _ *config.Config) error {
2929
mode, e := parseMode(args[1])
3030
if e != nil {
3131
return logger.Error(ctx, e)
@@ -58,15 +58,15 @@ var copyCmd = cli.Command[*config.Config]{ //nolint:gochecknoglobals
5858
if dst != "-" {
5959
checkByte, err = os.Open(dst)
6060
if err != nil {
61-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
61+
return logger.Error(ctx, err)
6262
}
6363
}
6464
case dst == "-":
6565
dstByte = logger.Stdout
6666
default:
6767
dstByte, err = os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, p)
6868
if err != nil {
69-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
69+
return logger.Error(ctx, err)
7070
}
7171
}
7272

go/helpers.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/candiddev/etcha/go/config"
1515
"github.com/candiddev/etcha/go/metrics"
1616
"github.com/candiddev/shared/go/cli"
17-
"github.com/candiddev/shared/go/errs"
1817
"github.com/candiddev/shared/go/logger"
1918
)
2019

@@ -117,7 +116,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
117116
Usage: "Manage the permissions",
118117
},
119118
},
120-
Run: func(ctx context.Context, args []string, flags cli.Flags, _ *config.Config) errs.Err {
119+
Run: func(ctx context.Context, args []string, flags cli.Flags, _ *config.Config) error {
121120
dir := args[0] == "dir"
122121
mode, e := parseMode(args[1])
123122
if e != nil {
@@ -127,7 +126,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
127126
path := args[2]
128127
permissions, owner, group, err := dirFileRunParse(dir, flags)
129128
if err != nil {
130-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
129+
return logger.Error(ctx, err)
131130
}
132131

133132
var contents []byte
@@ -147,12 +146,12 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
147146
return nil
148147
}
149148

150-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
149+
return logger.Error(ctx, err)
151150
}
152151

153152
if mode == metrics.CommandModeRemove {
154153
if err := os.Remove(path); err != nil {
155-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
154+
return logger.Error(ctx, err)
156155
}
157156

158157
return nil
@@ -163,7 +162,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
163162
if len(contents) > 0 && !dir {
164163
c, err := os.ReadFile(path)
165164
if err != nil {
166-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
165+
return logger.Error(ctx, err)
167166
}
168167

169168
if !bytes.Equal(c, contents) {
@@ -176,7 +175,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
176175
}
177176

178177
if err := os.WriteFile(path, contents, p); err != nil {
179-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
178+
return logger.Error(ctx, err)
180179
}
181180
}
182181
}
@@ -198,7 +197,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
198197

199198
if (g != stat.Gid || o != stat.Uid) && mode == metrics.CommandModeChange {
200199
if err := os.Chown(path, int(o), int(g)); err != nil {
201-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
200+
return logger.Error(ctx, err)
202201
}
203202
}
204203
}
@@ -208,13 +207,13 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
208207

209208
if mode == metrics.CommandModeChange {
210209
if err := os.Chmod(path, *permissions); err != nil {
211-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
210+
return logger.Error(ctx, err)
212211
}
213212
}
214213
}
215214

216215
if len(mismatch) > 0 && mode == metrics.CommandModeCheck {
217-
return logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("file %s does not match:\n\t%s", path, strings.Join(mismatch, "\n\t"))))
216+
return logger.Error(ctx, fmt.Errorf("file %s does not match:\n\t%s", path, strings.Join(mismatch, "\n\t")))
218217
}
219218

220219
return nil
@@ -223,7 +222,7 @@ func dirFileRun(file bool, usage string) cli.Command[*config.Config] { //nolint:
223222
}
224223
}
225224

226-
func parseMode(mode string) (metrics.CommandMode, errs.Err) {
225+
func parseMode(mode string) (metrics.CommandMode, error) {
227226
switch mode {
228227
case "check":
229228
return metrics.CommandModeCheck, nil
@@ -233,5 +232,5 @@ func parseMode(mode string) (metrics.CommandMode, errs.Err) {
233232
return metrics.CommandModeRemove, nil
234233
}
235234

236-
return "", errs.ErrReceiver.Wrap(fmt.Errorf("unrecognized mode: %s", mode))
235+
return "", fmt.Errorf("unrecognized mode: %s", mode)
237236
}

go/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func TestDirFileRun(t *testing.T) {
464464
for i := range tests {
465465
t.Run(tests[i].name, func(t *testing.T) {
466466
logger.SetStd()
467-
cli.SetStdin(tests[i].stdin)
467+
logger.SetStdin(tests[i].stdin)
468468

469469
cmd := "file"
470470
if tests[i].dir {

go/initdir.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"github.com/candiddev/etcha/go/config"
99
"github.com/candiddev/etcha/go/initdir"
1010
"github.com/candiddev/shared/go/cli"
11-
"github.com/candiddev/shared/go/errs"
1211
"github.com/candiddev/shared/go/logger"
1312
)
1413

1514
var initCmd = cli.Command[*config.Config]{ //nolint:gochecknoglobals
1615
ArgumentsOptional: []string{
1716
"directory, default: current directory",
1817
},
19-
Run: func(ctx context.Context, args []string, _ cli.Flags, _ *config.Config) errs.Err {
18+
Run: func(ctx context.Context, args []string, _ cli.Flags, _ *config.Config) error {
2019
var path string
2120

2221
var err error
@@ -26,7 +25,7 @@ var initCmd = cli.Command[*config.Config]{ //nolint:gochecknoglobals
2625
} else {
2726
path, err = os.Getwd()
2827
if err != nil {
29-
return logger.Error(ctx, errs.ErrReceiver.Wrap(errors.New("error getting current directory path")))
28+
return logger.Error(ctx, errors.New("error getting current directory path"))
3029
}
3130
}
3231

go/initdir/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This repository contains Etcha libraries and patterns written in Jsonnet. This
2828
`
2929

3030
// Init creates or updates Etcha files in a path.
31-
func Init(ctx context.Context, path string) errs.Err {
31+
func Init(ctx context.Context, path string) error {
3232
// If path doesn't exist, create it
3333
if err := os.MkdirAll(path, 0755); err != nil { //nolint:gosec
3434
return logger.Error(ctx, errs.ErrReceiver.Wrap(fmt.Errorf("error creating %s", path), err))
@@ -85,7 +85,7 @@ func Init(ctx context.Context, path string) errs.Err {
8585

8686
return nil
8787
}); err != nil {
88-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
88+
return logger.Error(ctx, err)
8989
}
9090

9191
// Remove unknown lib files
@@ -101,7 +101,7 @@ func Init(ctx context.Context, path string) errs.Err {
101101

102102
return nil
103103
}); err != nil {
104-
return logger.Error(ctx, errs.ErrReceiver.Wrap(err))
104+
return logger.Error(ctx, err)
105105
}
106106

107107
return logger.Error(ctx, nil)

go/initdir/lib/etcha/rot.libsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local copy = import './copy.libsonnet';
1010
{
1111
id: 'download Rot to %s' % cache,
1212
check: '%s version 2>&1 | grep "$(etcha copy change https://github.com/candiddev/rot/releases/latest/download/version -)" > /dev/null' % cache,
13-
change: 'etcha copy change https://github.com/candiddev/rot/releases/latest/download/rot_linux_%s.tar.gz - | tar -xOz rot > %s && chmod +x %s' % [arch, cache, cache],
13+
change: 'etcha copy change https://github.com/candiddev/rot/releases/latest/download/rot_linux_%s.gz - | gzip -d > %s && chmod +x %s' % [arch, cache, cache],
1414
remove: 'rm %s' % cache,
1515
onChange: onChange,
1616
},

go/jwt.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/candiddev/etcha/go/config"
77
"github.com/candiddev/etcha/go/pattern"
88
"github.com/candiddev/shared/go/cli"
9-
"github.com/candiddev/shared/go/errs"
109
"github.com/candiddev/shared/go/logger"
1110
"github.com/candiddev/shared/go/types"
1211
)
@@ -15,7 +14,7 @@ var jwt = cli.Command[*config.Config]{ //nolint:gochecknoglobals
1514
ArgumentsRequired: []string{
1615
"jwt path",
1716
},
18-
Run: func(ctx context.Context, args []string, _ cli.Flags, c *config.Config) errs.Err {
17+
Run: func(ctx context.Context, args []string, _ cli.Flags, c *config.Config) error {
1918
j, _, err := pattern.ParseJWTFromPath(ctx, c, "", args[1])
2019

2120
if j != nil {

0 commit comments

Comments
 (0)