Skip to content

Commit

Permalink
feat: removing pkg/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeken committed Apr 20, 2024
1 parent 483cf34 commit 84cea41
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
28 changes: 17 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package psadm

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
)

// ssmClient allows us to inject a fake API client for testing.
Expand Down Expand Up @@ -79,12 +79,12 @@ func (c *Client) GetParameterWithDescription(ctx context.Context, key string) (*
return nil, err
}
if len(desc) == 0 {
return nil, errors.Errorf("'%s' is not found.", key)
return nil, fmt.Errorf("psadm: key '%s' is not found", key)
}

val, err := c.getParameter(ctx, key)
if err != nil {
return nil, errors.Wrapf(err, "failed to get parameter '%s'", key)
return nil, fmt.Errorf("psadm: getting the parameter '%s': %w", key, err)
}

p := desc[0]
Expand All @@ -101,7 +101,7 @@ func (c *Client) GetParameterWithDescription(ctx context.Context, key string) (*
func (c *Client) GetParameter(ctx context.Context, key string) (string, error) {
resp, err := c.getParameter(ctx, key)
if err != nil {
return "", errors.Wrapf(err, "failed to get parameter '%s'", key)
return "", fmt.Errorf("psadm: getting the parameter '%s': %w", key, err)
}
return aws.ToString(resp.Parameter.Value), nil
}
Expand All @@ -120,7 +120,7 @@ func (c *Client) GetParameterByTime(ctx context.Context, key string, at time.Tim
return nil, err
}
if len(desc) == 0 {
return nil, errors.Errorf("'%s' is not found.", key)
return nil, fmt.Errorf("psadm: key '%s' is not found", key)
}

latest := aws.ToTime(desc[0].LastModifiedDate)
Expand All @@ -135,7 +135,7 @@ func (c *Client) GetParameterByTime(ctx context.Context, key string, at time.Tim
return nil, err
}
if len(history) == 0 {
return nil, errors.Errorf("'%s' is not found.", key)
return nil, fmt.Errorf("psadm: key '%s' is not found", key)
}

// history is sorted by LastModifiedDate in ascending order
Expand Down Expand Up @@ -164,14 +164,20 @@ func (c *Client) PutParameter(ctx context.Context, param *Parameter, overwrite b
Value: aws.String(param.Value),
Overwrite: aws.Bool(overwrite),
}

if param.Description != "" {
input.Description = aws.String(param.Description)
}

if param.KMSKeyID != "" {
input.KeyId = aws.String(param.KMSKeyID)
}
_, err := c.SSM.PutParameter(ctx, input)
return errors.Wrap(err, "failed to put parameters")

if _, err := c.SSM.PutParameter(ctx, input); err != nil {
return fmt.Errorf("psadm: putting the parameter: %w", err)
}

return nil
}

func (c *Client) getParameter(ctx context.Context, key string) (*ssm.GetParameterOutput, error) {
Expand Down Expand Up @@ -206,7 +212,7 @@ func (c *Client) GetParametersByPath(ctx context.Context, pathPrefix string) ([]
for _, p := range desc {
val, err := c.getParameter(ctx, *p.Name)
if err != nil {
return nil, errors.Wrap(err, "failed to get parameters")
return nil, fmt.Errorf("psadm: getting the parameter '%s': %w", *p.Name, err)
}

params = append(params, &Parameter{
Expand All @@ -231,7 +237,7 @@ func (c *Client) getParameterHistory(ctx context.Context, key string) ([]types.P
for {
resp, err := c.SSM.GetParameterHistory(ctx, input)
if err != nil {
return nil, errors.Wrap(err, "failed to get parameter history")
return nil, fmt.Errorf("psadm: getting the parameter history '%s': %w", key, err)
}
history = append(history, resp.Parameters...)

Expand All @@ -253,7 +259,7 @@ func (c *Client) describeParameters(ctx context.Context, filters []types.Paramet
for {
desc, err := c.SSM.DescribeParameters(ctx, input)
if err != nil {
return nil, errors.Wrap(err, "failed to describe parameters")
return nil, fmt.Errorf("psadm: describing the parameters: %w", err)
}
params = append(params, desc.Parameters...)
if desc.NextToken == nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/psadm/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/goccy/go-yaml"
"github.com/nabeken/psadm"
"github.com/pkg/errors"
)

type ExportCommand struct {
Expand All @@ -30,7 +29,7 @@ func (cmd *ExportCommand) Execute(args []string) error {

out, err := yaml.Marshal(params)
if err != nil {
return errors.Wrap(err, "failed to marshal into YAML")
return fmt.Errorf("marshaling into YAML: %w", err)
}

fmt.Print(string(out))
Expand Down
8 changes: 4 additions & 4 deletions cmd/psadm/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package main

import (
"context"
"errors"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/goccy/go-yaml"
"github.com/nabeken/psadm"
"github.com/pkg/errors"
)

type GetCommand struct {
Expand All @@ -20,7 +20,7 @@ func (cmd *GetCommand) Execute(args []string) error {
var err error

if len(args) == 0 {
return errors.New("You must specify a KEY to get.")
return errors.New("You must specify a KEY to get")
}

ctx := context.Background()
Expand All @@ -38,7 +38,7 @@ func (cmd *GetCommand) Execute(args []string) error {
var at time.Time
at, err = time.Parse(time.RFC3339, cmd.At)
if err != nil {
return errors.Wrap(err, "failed to parse `at'.")
return fmt.Errorf("parsing 'at': %w", err)
}
param, err = client.GetParameterByTime(ctx, args[0], at)
}
Expand All @@ -51,7 +51,7 @@ func (cmd *GetCommand) Execute(args []string) error {
} else {
out, err := yaml.Marshal([]*psadm.Parameter{param})
if err != nil {
return errors.Wrap(err, "failed to marshal into YAML")
return fmt.Errorf("marshaling into YAML: %w", err)
}

fmt.Print(string(out))
Expand Down
12 changes: 6 additions & 6 deletions cmd/psadm/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/goccy/go-yaml"
"github.com/nabeken/psadm"
"github.com/pkg/errors"
)

type ImportCommand struct {
Expand All @@ -22,23 +22,23 @@ type ImportCommand struct {

func (cmd *ImportCommand) Execute(args []string) error {
if len(args) == 0 {
return errors.New("You must specify a YAML file to be imported.")
return errors.New("You must specify a YAML file to be imported")
}

f, err := os.Open(args[0])
if err != nil {
return errors.Wrapf(err, "failed to open %s", args[0])
return fmt.Errorf("opening %s: %w", args[0], err)
}
defer f.Close()

data, err := io.ReadAll(f)
if err != nil {
return errors.Wrapf(err, "failed to read data from %s", args[0])
return fmt.Errorf("reading from %s: %w", args[0], err)
}

var params []*psadm.Parameter
if err := yaml.Unmarshal(data, &params); err != nil {
return errors.Wrap(err, "failed to unmarshal from YAML")
return fmt.Errorf("marshaling into YAML: %w", err)
}

ctx := context.Background()
Expand Down Expand Up @@ -77,7 +77,7 @@ func (cmd *ImportCommand) Execute(args []string) error {
p.KMSKeyID = cmd.DefaultKMSKeyID
}
if err := runF(p); err != nil {
return errors.Wrapf(err, "failed to update '%s'", p.Name)
return fmt.Errorf("updating '%s': %w", p.Name, err)
}
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/goccy/go-yaml v1.11.3
github.com/jessevdk/go-flags v1.5.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.7.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 84cea41

Please sign in to comment.