Skip to content

Commit

Permalink
remove pkg/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Jun 13, 2024
1 parent c5b69f8 commit fd57442
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/itchyny/gojq v0.12.11
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-isatty v0.0.17
github.com/pkg/errors v0.9.1
github.com/simeji/jid v0.7.6
google.golang.org/api v0.155.0
)
Expand Down Expand Up @@ -82,6 +81,7 @@ require (
github.com/nsf/termbox-go v0.0.0-20181027232701-60ab7e3d12ed // indirect
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
Expand Down
4 changes: 1 addition & 3 deletions tfstate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"
"text/template"

"github.com/pkg/errors"
)

const (
Expand All @@ -22,7 +20,7 @@ func FuncMap(ctx context.Context, stateLoc string) (template.FuncMap, error) {
func FuncMapWithName(ctx context.Context, name string, stateLoc string) (template.FuncMap, error) {
state, err := ReadURL(ctx, stateLoc)
if err != nil {
return nil, errors.Wrapf(err, "failed to read tfstate: %s", stateLoc)
return nil, fmt.Errorf("failed to read tfstate: %s: %w", stateLoc, err)
}
nameFunc := func(addrs string) string {
if strings.Contains(addrs, "'") {
Expand Down
15 changes: 7 additions & 8 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"

"github.com/itchyny/gojq"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -60,7 +59,7 @@ func (a *Object) Query(query string) (*Object, error) {
}
return &Object{v}, nil
}
return nil, errors.Errorf("%s is not found in the state", query)
return nil, fmt.Errorf("%s is not found in the state", query)
}

// TFState represents a tfstate
Expand Down Expand Up @@ -128,7 +127,7 @@ func ReadWithWorkspace(ctx context.Context, src io.Reader, ws string) (*TFState,
}
var s TFState
if err := json.NewDecoder(src).Decode(&s.state); err != nil {
return nil, errors.Wrap(err, "invalid json")
return nil, fmt.Errorf("invalid json: %w", err)
}
if s.state.Backend != nil {
remote, err := readRemoteState(ctx, s.state.Backend, ws)
Expand All @@ -139,7 +138,7 @@ func ReadWithWorkspace(ctx context.Context, src io.Reader, ws string) (*TFState,
return Read(ctx, remote)
}
if s.state.Version != StateVersion {
return nil, errors.Errorf("unsupported state version %d", s.state.Version)
return nil, fmt.Errorf("unsupported state version %d", s.state.Version)
}
return &s, nil
}
Expand All @@ -151,7 +150,7 @@ func ReadFile(ctx context.Context, file string) (*TFState, error) {

f, err := os.Open(file)
if err != nil {
return nil, errors.Wrapf(err, "failed to read tfstate from %s", file)
return nil, fmt.Errorf("failed to read tfstate from %s: %w", file, err)
}
defer f.Close()
return ReadWithWorkspace(ctx, f, string(ws))
Expand Down Expand Up @@ -191,10 +190,10 @@ func ReadURL(ctx context.Context, loc string) (*TFState, error) {
case "":
return ReadFile(ctx, u.Path)
default:
err = errors.Errorf("URL scheme %s is not supported", u.Scheme)
err = fmt.Errorf("URL scheme %s is not supported", u.Scheme)
}
if err != nil {
return nil, errors.Wrapf(err, "failed to read tfstate from %s", u.String())
return nil, fmt.Errorf("failed to read tfstate from %s", u.String())
}
defer src.Close()
return Read(ctx, src)
Expand All @@ -207,7 +206,7 @@ func (s *TFState) Lookup(key string) (*Object, error) {
var foundName string
for name, ins := range s.scanned {
if strings.HasPrefix(key, name) {
// logest match
// longest match
if len(foundName) < len(name) {
found = ins
foundName = name
Expand Down
22 changes: 10 additions & 12 deletions tfstate/remote_azurerm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/go-autorest/autorest/azure/cli"

"github.com/pkg/errors"
)

type azureRMOption struct {
Expand Down Expand Up @@ -51,7 +49,7 @@ func readAzureRM(ctx context.Context, resourceGroupName string, accountName stri

client, err = azblob.NewClient(serviceUrl, cred, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to setup client")
return nil, fmt.Errorf("failed to setup client: %w", err)
}
} else {
// get blob access key
Expand All @@ -70,24 +68,24 @@ func readAzureRM(ctx context.Context, resourceGroupName string, accountName stri
}
}
if accountKey == "" {
return nil, errors.New("Blob access key not found in ENV, terraform config and can't be fetched from current Azure Profile")
return nil, fmt.Errorf("Blob access key not found in ENV, terraform config and can't be fetched from current Azure Profile")
}

// Authenticate
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return nil, errors.Wrap(err, "failed to create credential")
return nil, fmt.Errorf("failed to create credential: %w", err)
}

client, err = azblob.NewClientWithSharedKeyCredential(serviceUrl, credential, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to setup client")
return nil, fmt.Errorf("failed to setup client: %w", err)
}
}

blobDownloadResponse, err := client.DownloadStream(ctx, containerName, key, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to download blob")
return nil, fmt.Errorf("failed to download blob: %w", err)
}

r := blobDownloadResponse.Body
Expand All @@ -102,7 +100,7 @@ func getDefaultAzureSubscription() (string, error) {
profilePath, _ := cli.ProfilePath()
profile, err := cli.LoadProfile(profilePath)
if err != nil {
return "", errors.Wrap(err, "failed to load profile")
return "", fmt.Errorf("failed to load profile: %w", err)
}
subscriptionID := ""
for _, x := range profile.Subscriptions {
Expand All @@ -127,11 +125,11 @@ func getDefaultAzureAccessKey(ctx context.Context, resourceGroupName string, acc

clientFactory, err := armstorage.NewClientFactory(subscriptionID, cred, nil)
if err != nil {
return "", errors.Wrap(err, "failed to create client factory")
return "", fmt.Errorf("failed to create client factory: %w", err)
}
keys, err := clientFactory.NewAccountsClient().ListKeys(ctx, resourceGroupName, accountName, nil)
if err != nil {
return "", errors.Wrap(err, "failed to list keys")
return "", fmt.Errorf("failed to list keys: %w", err)
}

return *keys.Keys[0].Value, nil
Expand All @@ -144,7 +142,7 @@ func getAzureSubscription(opt azureRMOption) (string, error) {

subscriptionID, err := getDefaultAzureSubscription()
if err != nil {
return "", errors.Wrap(err, "failed to get default subscription")
return "", fmt.Errorf("failed to get default subscription: %w", err)
}

return subscriptionID, nil
Expand All @@ -153,7 +151,7 @@ func getAzureSubscription(opt azureRMOption) (string, error) {
func getDefaultAzureCredential() (*azidentity.DefaultAzureCredential, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, errors.Wrap(err, "failed to authorize")
return nil, fmt.Errorf("failed to authorize: %w", err)
}
return cred, nil
}
6 changes: 3 additions & 3 deletions tfstate/remote_tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package tfstate

import (
"context"
"fmt"
"io"
"net/http"
"os"

tfe "github.com/hashicorp/go-tfe"
"github.com/pkg/errors"
)

func readTFEState(ctx context.Context, config map[string]interface{}, ws string) (io.ReadCloser, error) {
Expand All @@ -18,7 +18,7 @@ func readTFEState(ctx context.Context, config map[string]interface{}, ws string)

workspaces, ok := config["workspaces"].(map[string]interface{})
if !ok {
return nil, errors.Errorf("failed to parse workspaces")
return nil, fmt.Errorf("failed to parse workspaces")
}

name, prefix := *strpe(workspaces["name"]), *strpe(workspaces["prefix"])
Expand All @@ -30,7 +30,7 @@ func readTFEState(ctx context.Context, config map[string]interface{}, ws string)
return readTFE(ctx, hostname, organization, prefix+ws, token)
}

return nil, errors.Errorf("workspaces requires either name or prefix")
return nil, fmt.Errorf("workspaces requires either name or prefix")
}

func readTFE(ctx context.Context, hostname string, organization string, ws string, token string) (io.ReadCloser, error) {
Expand Down
6 changes: 3 additions & 3 deletions tfstate/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package tfstate_test

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/pkg/errors"
)

func TestRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -38,7 +38,7 @@ func testLookupRoundTrip(t *testing.T, path string) error {
return err
}
if len(names) == 0 {
return errors.Errorf("failed to list resources in %s", path)
return fmt.Errorf("failed to list resources in %s", path)
}
for _, name := range names {
t.Logf("looking up for %s", name)
Expand All @@ -47,7 +47,7 @@ func testLookupRoundTrip(t *testing.T, path string) error {
return err
}
if res == nil || res.String() == "null" {
return errors.Errorf("failed to lookup %s in %s", name, path)
return fmt.Errorf("failed to lookup %s in %s", name, path)
}
t.Logf("found %s", res)
}
Expand Down

0 comments on commit fd57442

Please sign in to comment.