Skip to content

Commit

Permalink
Merge pull request #384 from segmentio/mckern/papercuts
Browse files Browse the repository at this point in the history
Fix accumulated papercuts
  • Loading branch information
mckern authored May 11, 2023
2 parents d554ab6 + e032a23 commit 6f02bdf
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 202 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
9 changes: 4 additions & 5 deletions cmd/exec_default.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// +build !linux,!darwin
//go:build !linux && !darwin

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
2 changes: 1 addition & 1 deletion cmd/exec_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux darwin
//go:build linux || darwin

package cmd

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
Loading

0 comments on commit 6f02bdf

Please sign in to comment.