Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use go-fsimpl to read from datasources #1336

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ linters:
# - funlen
# - gci
# - gochecknoglobals
# - gochecknoinits
- gochecknoinits
- gocognit
- goconst
- gocritic
Expand Down
3 changes: 3 additions & 0 deletions aws/ec2meta.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package aws

import (
"context"
"net/http"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hairyhenderson/gomplate/v4/env"
"github.com/hairyhenderson/gomplate/v4/internal/deprecated"
)

const (
Expand Down Expand Up @@ -38,6 +40,7 @@ func NewEc2Meta(options ClientOptions) *Ec2Meta {
config := aws.NewConfig()
config = config.WithHTTPClient(&http.Client{Timeout: options.Timeout})
if endpoint := env.Getenv("AWS_META_ENDPOINT"); endpoint != "" {
deprecated.WarnDeprecated(context.Background(), "Use AWS_EC2_METADATA_SERVICE_ENDPOINT instead of AWS_META_ENDPOINT")
config = config.WithEndpoint(endpoint)
}

Expand Down
15 changes: 12 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ func (c *tmplctx) Env() map[string]string {
}

// createTmplContext reads the datasources for the given aliases
//
//nolint:staticcheck
func createTmplContext(_ context.Context, aliases []string, d *data.Data) (interface{}, error) {
func createTmplContext(
ctx context.Context, aliases []string,
//nolint:staticcheck
d *data.Data,
) (interface{}, error) {
// we need to inject the current context into the Data value, because
// the Datasource method may need it
// TODO: remove this before v4
if d != nil {
d.Ctx = ctx
}

var err error
tctx := &tmplctx{}
for _, a := range aliases {
Expand Down
7 changes: 7 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"testing"

"github.com/hairyhenderson/go-fsimpl"
"github.com/hairyhenderson/gomplate/v4/data"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -31,6 +33,11 @@ func TestCreateContext(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, c)

fsmux := fsimpl.NewMux()
fsmux.Add(datafs.EnvFS)

ctx = datafs.ContextWithFSProvider(ctx, fsmux)

fooURL := "env:///foo?type=application/yaml"
barURL := "env:///bar?type=application/yaml"
uf, _ := url.Parse(fooURL)
Expand Down
23 changes: 12 additions & 11 deletions crypto/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ import (
"crypto/sha512"
"fmt"
"hash"
"sync"

"golang.org/x/crypto/pbkdf2"
)

var hashFuncs map[crypto.Hash]func() hash.Hash
var hashFuncs = sync.OnceValue[map[crypto.Hash]func() hash.Hash](func() map[crypto.Hash]func() hash.Hash {
h := make(map[crypto.Hash]func() hash.Hash)
h[crypto.SHA1] = sha1.New
h[crypto.SHA224] = sha256.New224
h[crypto.SHA256] = sha256.New
h[crypto.SHA384] = sha512.New384
h[crypto.SHA512] = sha512.New
h[crypto.SHA512_224] = sha512.New512_224
h[crypto.SHA512_256] = sha512.New512_256

func init() {
hashFuncs = make(map[crypto.Hash]func() hash.Hash)
hashFuncs[crypto.SHA1] = sha1.New
hashFuncs[crypto.SHA224] = sha256.New224
hashFuncs[crypto.SHA256] = sha256.New
hashFuncs[crypto.SHA384] = sha512.New384
hashFuncs[crypto.SHA512] = sha512.New
hashFuncs[crypto.SHA512_224] = sha512.New512_224
hashFuncs[crypto.SHA512_256] = sha512.New512_256
}
return h
})()

// StrToHash - find a hash given a certain string
func StrToHash(hash string) (crypto.Hash, error) {
Expand Down
98 changes: 98 additions & 0 deletions data/datafuncs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package data

import (
"github.com/hairyhenderson/gomplate/v4/internal/parsers"
)

// temporary aliases for parser functions while I figure out if they need to be
// exported from the internal parsers package

// JSON - Unmarshal a JSON Object. Can be ejson-encrypted.
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var JSON = parsers.JSON

// JSONArray - Unmarshal a JSON Array
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var JSONArray = parsers.JSONArray

// YAML - Unmarshal a YAML Object
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var YAML = parsers.YAML

// YAMLArray - Unmarshal a YAML Array
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var YAMLArray = parsers.YAMLArray

// TOML - Unmarshal a TOML Object
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var TOML = parsers.TOML

// CSV - Unmarshal CSV
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var CSV = parsers.CSV

// CSVByRow - Unmarshal CSV in a row-oriented form
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var CSVByRow = parsers.CSVByRow

// CSVByColumn - Unmarshal CSV in a Columnar form
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var CSVByColumn = parsers.CSVByColumn

// ToCSV -
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToCSV = parsers.ToCSV

// ToJSON - Stringify a struct as JSON
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToJSON = parsers.ToJSON

// ToJSONPretty - Stringify a struct as JSON (indented)
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToJSONPretty = parsers.ToJSONPretty

// ToYAML - Stringify a struct as YAML
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToYAML = parsers.ToYAML

// ToTOML - Stringify a struct as TOML
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToTOML = parsers.ToTOML

// CUE - Unmarshal a CUE expression into the appropriate type
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var CUE = parsers.CUE

// ToCUE - Stringify a struct as CUE
//
// Deprecated: will be removed in a future version of gomplate. If you have a
// need for this, please open an issue!
var ToCUE = parsers.ToCUE
Loading
Loading