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

Add UUID function to bundle template functions #1612

Merged
merged 1 commit into from
Jul 19, 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
6 changes: 6 additions & 0 deletions libs/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/databricks/cli/libs/auth"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/iam"

"github.com/google/uuid"
)

type ErrFail struct {
Expand Down Expand Up @@ -51,6 +53,10 @@ func loadHelpers(ctx context.Context) template.FuncMap {
"random_int": func(n int) int {
return rand.Intn(n)
},
// Alias for https://pkg.go.dev/github.com/google/uuid#New. Returns, as a string, a UUID which is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
"uuid": func() string {
return uuid.New().String()
},
// A key value pair. This is used with the map function to generate maps
// to use inside a template
"pair": func(k string, v any) pair {
Expand Down
17 changes: 17 additions & 0 deletions libs/template/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ func TestTemplateRandIntFunction(t *testing.T) {
assert.Empty(t, err)
}

func TestTemplateUuidFunction(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()

ctx = root.SetWorkspaceClient(ctx, nil)
helpers := loadHelpers(ctx)
r, err := newRenderer(ctx, nil, helpers, "./testdata/uuid/template", "./testdata/uuid/library", tmpDir)
require.NoError(t, err)

err = r.walk()
assert.NoError(t, err)

assert.Len(t, r.files, 1)
uuid := strings.TrimSpace(string(r.files[0].(*inMemoryFile).content))
assert.Regexp(t, "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", uuid)
}

func TestTemplateUrlFunction(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
Expand Down
1 change: 1 addition & 0 deletions libs/template/testdata/uuid/template/hello.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{print (uuid)}}