Skip to content

Commit

Permalink
chore: Replace util.JoinErrors with errors.Join
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 14, 2024
1 parent 9a9eadc commit 9ff86ec
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cmd/chronic/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func printBuf(cmd *cobra.Command, buf *execbuf.Buffer, exitCode int, verbose boo
errs = append(errs, buf.Print(cmd.ErrOrStderr()))

_, _ = fmt.Fprintln(cmd.OutOrStdout(), "\nRETVAL:", strconv.Itoa(exitCode))
return util.JoinErrors(errs...)
return errors.Join(errs...)
}
4 changes: 1 addition & 3 deletions cmd/combine/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"fmt"
"io"

"gabe565.com/moreutils/internal/util"
)

//go:generate go run github.com/dmarkham/enumer -type operator -trimprefix operator -transform lower -output operator_string.go
Expand Down Expand Up @@ -39,7 +37,7 @@ func (op operator) compare(out io.Writer, r1, r2 io.ReadSeeker) error {

// compareOr outputs lines from both r1 and r2
func compareOr(out io.Writer, r1, r2 io.Reader) error {
return util.JoinErrors(
return errors.Join(
iterLines(r1, func(line string) error {
_, err := fmt.Fprintln(out, line)
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/errno/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package errno

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -71,7 +72,7 @@ func run(cmd *cobra.Command, args []string) error {
errs = append(errs, err)
}
}
return util.JoinErrors(errs...)
return errors.Join(errs...)
}

func findErrno(cmd *cobra.Command, arg string) error {
Expand Down
3 changes: 1 addition & 2 deletions cmd/ifdata/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"gabe565.com/moreutils/internal/cmdutil"
"gabe565.com/moreutils/internal/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -180,5 +179,5 @@ func run(cmd *cobra.Command, args []string) error {
_, _ = io.WriteString(cmd.OutOrStdout(), s+"\n")
}
}
return util.JoinErrors(errs...)
return errors.Join(errs...)
}
3 changes: 2 additions & 1 deletion cmd/install/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package install

import (
"errors"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -88,7 +89,7 @@ func run(cmd *cobra.Command, args []string) error {
}
}

return util.JoinErrors(errs...)
return errors.Join(errs...)
}

func link(symbolic bool, oldname, newname string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/isutf8/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func run(cmd *cobra.Command, args []string) error {
if notAllUTF8 {
errs = append(errs, ErrNotAllUTF8)
}
return util.JoinErrors(errs...)
return errors.Join(errs...)
}

var errNotUTF8 = errors.New("file is not UTF-8 encoded")
Expand Down
3 changes: 2 additions & 1 deletion cmd/pee/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pee

import (
"errors"
"io"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -115,5 +116,5 @@ func run(cmd *cobra.Command, args []string) error {
}

wg.Wait()
return util.JoinErrors(errs...)
return errors.Join(errs...)
}
5 changes: 2 additions & 3 deletions internal/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"

"gabe565.com/moreutils/internal/util"
"github.com/google/shlex"
"github.com/mattn/go-tty"
)
Expand All @@ -26,13 +25,13 @@ func Get() ([]string, error) {
var errs []error
for _, env := range []string{envEditor, envVisual} {
if editor, err := parseEnv(env); err == nil {
return editor, util.JoinErrors(errs...)
return editor, errors.Join(errs...)
} else if !errors.Is(err, ErrUnset) {
errs = append(errs, err)
}
}

return []string{defaultEditor}, util.JoinErrors(errs...)
return []string{defaultEditor}, errors.Join(errs...)
}

func parseEnv(env string) ([]string, error) {
Expand Down
4 changes: 1 addition & 3 deletions internal/execbuf/execbuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"slices"
"sync"
"time"

"gabe565.com/moreutils/internal/util"
)

// Buffer is an io.Writer that buffers exec.Cmd output.
Expand Down Expand Up @@ -86,7 +84,7 @@ func (e *Buffer) Print(source io.Writer) error {
}
}

return util.JoinErrors(errs...)
return errors.Join(errs...)
}

// Bytes returns the bytes for a source, or all sources if nil.
Expand Down
16 changes: 0 additions & 16 deletions internal/util/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@ package util

import (
"errors"
"slices"
"strconv"
)

var ErrNotAPipe = errors.New("this command should be run in a pipe")

// JoinErrors behaves similarly to errors.Join, but returns the error verbatim if there is only 1.
func JoinErrors(errs ...error) error {
errs = slices.DeleteFunc(errs, func(err error) bool {
return err == nil
})
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
return errors.Join(errs...)
}
}

func NewExitCodeError(code int) error {
return &ExitCodeError{code}
}
Expand Down
30 changes: 0 additions & 30 deletions internal/util/errors_test.go

This file was deleted.

0 comments on commit 9ff86ec

Please sign in to comment.