Skip to content

Commit

Permalink
Merge branch 'main' into update-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Jun 13, 2024
2 parents 7ea7abe + 88bc1bc commit 8d0a52b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 43 deletions.
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.12
github.com/google/go-cmp v0.6.0
github.com/google/go-jsonnet v0.20.0
github.com/hashicorp/go-tfe v1.2.0
github.com/hashicorp/go-tfe v1.56.0
github.com/itchyny/gojq v0.12.16
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-isatty v0.0.20
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 @@ -69,9 +68,10 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/go-slug v0.8.1 // indirect
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/hashicorp/go-slug v0.15.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/jsonapi v1.3.1 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
Expand All @@ -81,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-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
Expand Down
21 changes: 11 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,21 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfF
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas=
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-slug v0.8.1 h1:srN7ivgAjHfZddYY1DjBaihRCFy20+vCcOrlx1O2AfE=
github.com/hashicorp/go-slug v0.8.1/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v1.2.0 h1:L29LCo/qIjOqBUjfiUsZSAzBdxmsOLzwnwZpA+68WW8=
github.com/hashicorp/go-tfe v1.2.0/go.mod h1:tJF/OlAXzVbmjiimAPLplSLgwg6kZDUOy0MzHuMwvF4=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0=
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/go-slug v0.15.0 h1:AhMnE6JIyW0KoDJlmRDwv4xd52a5ZK3VdioQ7SMmZhI=
github.com/hashicorp/go-slug v0.15.0/go.mod h1:THWVTAXwJEinbsp4/bBRcmbaO5EYNLTqxbG4tZ3gCYQ=
github.com/hashicorp/go-tfe v1.56.0 h1:AjBTo7TmWoz42l4KhH65Q3NvjRD5yD3XZrG1tzFySeI=
github.com/hashicorp/go-tfe v1.56.0/go.mod h1:XnTtBj3tVQ4uFkcFsv8Grn+O1CVcIcceL1uc2AgUcaU=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/jsonapi v1.3.1 h1:GtPvnmcWgYwCuDGvYT5VZBHcUyFdq9lSyCzDjn1DdPo=
github.com/hashicorp/jsonapi v1.3.1/go.mod h1:kWfdn49yCjQvbpnvY1dxxAuAFzISwrrMDQOcu6NsFoM=
github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
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
13 changes: 6 additions & 7 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 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 8d0a52b

Please sign in to comment.