Skip to content

Commit

Permalink
Use contemporary error wrapping
Browse files Browse the repository at this point in the history
The 3rd party package github.com/pkg/errors is no longer required to
wrap errors. This was handled programmatically by the tool
github.com/xdg-go/go-rewrap-errors and should be complete.
  • Loading branch information
mckern committed May 10, 2023
1 parent b153856 commit e032a23
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 74 deletions.
11 changes: 6 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"github.com/pkg/errors"
"fmt"

analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/store"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
)

// deleteCmd represents the delete command
Expand All @@ -26,7 +27,7 @@ func init() {
func delete(cmd *cobra.Command, args []string) error {
service := utils.NormalizeService(args[0])
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
return fmt.Errorf("Failed to validate service: %w", err)
}

key := args[1]
Expand All @@ -35,7 +36,7 @@ func delete(cmd *cobra.Command, args []string) error {
}

if err := validateKey(key); err != nil {
return errors.Wrap(err, "Failed to validate key")
return fmt.Errorf("Failed to validate key: %w", err)
}

if analyticsEnabled && analyticsClient != nil {
Expand All @@ -52,7 +53,7 @@ func delete(cmd *cobra.Command, args []string) error {
}
secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
secretId := store.SecretId{
Service: service,
Expand Down
9 changes: 4 additions & 5 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"regexp"
"strings"

"github.com/pkg/errors"
analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
)

var (
Expand All @@ -30,16 +29,16 @@ func init() {
func env(cmd *cobra.Command, args []string) error {
service := utils.NormalizeService(args[0])
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
return fmt.Errorf("Failed to validate service: %w", err)
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
rawSecrets, err := secretStore.ListRaw(service)
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
return fmt.Errorf("Failed to list store contents: %w", err)
}

if analyticsEnabled && analyticsClient != nil {
Expand Down
14 changes: 7 additions & 7 deletions cmd/exec.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"

"github.com/pkg/errors"
analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/environ"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
)

// When true, only use variables retrieved from the backend, do not inherit existing environment variables
Expand All @@ -33,10 +33,10 @@ var execCmd = &cobra.Command{
return errors.New("please separate services and command with '--'. See usage")
}
if err := cobra.MinimumNArgs(1)(cmd, args[:dashIx]); err != nil {
return errors.Wrap(err, "at least one service must be specified")
return fmt.Errorf("at least one service must be specified: %w", err)
}
if err := cobra.MinimumNArgs(1)(cmd, args[dashIx:]); err != nil {
return errors.Wrap(err, "must specify command to run. See usage")
return fmt.Errorf("must specify command to run. See usage: %w", err)
}
return nil
},
Expand Down Expand Up @@ -88,13 +88,13 @@ func execRun(cmd *cobra.Command, args []string) error {

for _, service := range services {
if err := validateServiceWithLabel(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
return fmt.Errorf("Failed to validate service: %w", err)
}
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")

Expand Down Expand Up @@ -131,7 +131,7 @@ func execRun(cmd *cobra.Command, args []string) error {
err = env.Load(secretStore, service, &collisions)
}
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
return fmt.Errorf("Failed to list store contents: %w", err)
}

for _, c := range collisions {
Expand Down
7 changes: 3 additions & 4 deletions cmd/exec_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
package cmd

import (
"fmt"
"os"
osexec "os/exec"
"os/signal"
"syscall"

"github.com/pkg/errors"
)

// exec executes the given command, passing it args and setting its environment
Expand All @@ -25,7 +24,7 @@ func exec(command string, args []string, env []string) error {
signal.Notify(sigChan)

if err := ecmd.Start(); err != nil {
return errors.Wrap(err, "Failed to start command")
return fmt.Errorf("Failed to start command: %w", err)
}

go func() {
Expand All @@ -37,7 +36,7 @@ func exec(command string, args []string, env []string) error {

if err := ecmd.Wait(); err != nil {
ecmd.Process.Signal(os.Kill)
return errors.Wrap(err, "Failed to wait for command termination")
return fmt.Errorf("Failed to wait for command termination: %w", err)
}

waitStatus := ecmd.ProcessState.Sys().(syscall.WaitStatus)
Expand Down
18 changes: 9 additions & 9 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"

"github.com/magiconair/properties"
"github.com/pkg/errors"

analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -62,12 +62,12 @@ func runExport(cmd *cobra.Command, args []string) error {
for _, service := range args {
service = utils.NormalizeService(service)
if err := validateService(service); err != nil {
return errors.Wrapf(err, "Failed to validate service %s", service)
return fmt.Errorf("Failed to validate service %s: %w", service, err)
}

rawSecrets, err := secretStore.ListRaw(service)
if err != nil {
return errors.Wrapf(err, "Failed to list store contents for service %s", service)
return fmt.Errorf("Failed to list store contents for service %s: %w", service, err)
}
for _, rawSecret := range rawSecrets {
k := key(rawSecret.Key)
Expand All @@ -81,7 +81,7 @@ func runExport(cmd *cobra.Command, args []string) error {
file := os.Stdout
if exportOutput != "" {
if file, err = os.OpenFile(exportOutput, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
return errors.Wrap(err, "Failed to open output file for writing")
return fmt.Errorf("Failed to open output file for writing: %w", err)
}
defer file.Close()
defer file.Sync()
Expand All @@ -105,11 +105,11 @@ func runExport(cmd *cobra.Command, args []string) error {
case "tfvars":
err = exportAsTfvars(params, w)
default:
err = errors.Errorf("Unsupported export format: %s", exportFormat)
err = fmt.Errorf("Unsupported export format: %s", exportFormat)
}

if err != nil {
return errors.Wrap(err, "Unable to export parameters")
return fmt.Errorf("Unable to export parameters: %w", err)
}

return nil
Expand Down Expand Up @@ -173,7 +173,7 @@ func exportAsCsv(params map[string]string, w io.Writer) error {
defer csvWriter.Flush()
for _, k := range sortedKeys(params) {
if err := csvWriter.Write([]string{k, params[k]}); err != nil {
return errors.Wrapf(err, "Failed to write param %s to CSV file", k)
return fmt.Errorf("Failed to write param %s to CSV file: %w", k, err)
}
}
return nil
Expand All @@ -183,7 +183,7 @@ func exportAsTsv(params map[string]string, w io.Writer) error {
// TSV (Tab Separated Values) like:
for _, k := range sortedKeys(params) {
if _, err := fmt.Fprintf(w, "%s\t%s\n", k, params[k]); err != nil {
return errors.Wrapf(err, "Failed to write param %s to TSV file", k)
return fmt.Errorf("Failed to write param %s to TSV file: %w", k, err)
}
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/segmentio/chamber/v2/store"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -42,11 +41,11 @@ func find(cmd *cobra.Command, args []string) error {

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
services, err := secretStore.ListServices(blankService, includeSecrets)
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
return fmt.Errorf("Failed to list store contents: %w", err)
}

if byValue {
Expand Down
11 changes: 5 additions & 6 deletions cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"os"
"text/tabwriter"

"github.com/pkg/errors"
analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/store"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
)

// historyCmd represents the history command
Expand All @@ -27,12 +26,12 @@ func init() {
func history(cmd *cobra.Command, args []string) error {
service := utils.NormalizeService(args[0])
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
return fmt.Errorf("Failed to validate service: %w", err)
}

key := utils.NormalizeKey(args[1])
if err := validateKey(key); err != nil {
return errors.Wrap(err, "Failed to validate key")
return fmt.Errorf("Failed to validate key: %w", err)
}

if analyticsEnabled && analyticsClient != nil {
Expand All @@ -50,7 +49,7 @@ func history(cmd *cobra.Command, args []string) error {

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
secretId := store.SecretId{
Service: service,
Expand All @@ -59,7 +58,7 @@ func history(cmd *cobra.Command, args []string) error {

events, err := secretStore.History(secretId)
if err != nil {
return errors.Wrap(err, "Failed to get history")
return fmt.Errorf("Failed to get history: %w", err)
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
Expand Down
13 changes: 6 additions & 7 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"io"
"os"

"github.com/pkg/errors"
analytics "github.com/segmentio/analytics-go/v3"
"github.com/segmentio/chamber/v2/store"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
analytics "github.com/segmentio/analytics-go/v3"
"gopkg.in/yaml.v3"
)

Expand All @@ -31,7 +30,7 @@ func init() {
func importRun(cmd *cobra.Command, args []string) error {
service := utils.NormalizeService(args[0])
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
return fmt.Errorf("Failed to validate service: %w", err)
}

var in io.Reader
Expand All @@ -43,15 +42,15 @@ func importRun(cmd *cobra.Command, args []string) error {
} else {
in, err = os.Open(file)
if err != nil {
return errors.Wrap(err, "Failed to open file")
return fmt.Errorf("Failed to open file: %w", err)
}
}

var toBeImported map[string]string

decoder := yaml.NewDecoder(in)
if err := decoder.Decode(&toBeImported); err != nil {
return errors.Wrap(err, "Failed to decode input as json")
return fmt.Errorf("Failed to decode input as json: %w", err)
}

if analyticsEnabled && analyticsClient != nil {
Expand All @@ -68,7 +67,7 @@ func importRun(cmd *cobra.Command, args []string) error {

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}

for key, value := range toBeImported {
Expand All @@ -80,7 +79,7 @@ func importRun(cmd *cobra.Command, args []string) error {
Key: key,
}
if err := secretStore.Write(secretId, value); err != nil {
return errors.Wrap(err, "Failed to write secret")
return fmt.Errorf("Failed to write secret: %w", err)
}
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/list-services.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/segmentio/chamber/v2/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,11 +36,11 @@ func listServices(cmd *cobra.Command, args []string) error {
}
secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
return fmt.Errorf("Failed to get secret store: %w", err)
}
secrets, err := secretStore.ListServices(service, includeSecretName)
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
return fmt.Errorf("Failed to list store contents: %w", err)
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
Expand Down
Loading

0 comments on commit e032a23

Please sign in to comment.