Skip to content

Commit

Permalink
Use go-fsimpl to read from datasources
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Jan 17, 2024
1 parent 886c3b2 commit ee4118e
Show file tree
Hide file tree
Showing 82 changed files with 2,810 additions and 4,076 deletions.
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
13 changes: 10 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ 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) {

Check failure on line 27 in context.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
// 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

0 comments on commit ee4118e

Please sign in to comment.