From 4064a21797a7b0c85c2d262d247793ae93541f1d Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 30 Nov 2022 14:40:41 +0100 Subject: [PATCH] Function to return bundle's cache directory (#109) Parallel of `project.CacheDir()` introduced in https://github.com/databricks/bricks/pull/82. --- bundle/bundle.go | 20 ++++++++++++++++++++ bundle/bundle_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/bundle/bundle.go b/bundle/bundle.go index ab85a057..86e0872a 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -1,6 +1,7 @@ package bundle import ( + "os" "path/filepath" "sync" @@ -49,3 +50,22 @@ func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient { }) return b.client } + +var cacheDirName = filepath.Join(".databricks", "bundle") + +// CacheDir returns directory to use for temporary files for this bundle. +// Scoped to the bundle's environment. +func (b *Bundle) CacheDir() (string, error) { + if b.Config.Bundle.Environment == "" { + panic("environment not set") + } + + // Make directory if it doesn't exist yet. + dir := filepath.Join(b.Config.Path, cacheDirName, b.Config.Bundle.Environment) + err := os.MkdirAll(dir, 0700) + if err != nil { + return "", err + } + + return dir, nil +} diff --git a/bundle/bundle_test.go b/bundle/bundle_test.go index 9196563d..0aa58c0a 100644 --- a/bundle/bundle_test.go +++ b/bundle/bundle_test.go @@ -2,6 +2,8 @@ package bundle import ( "os" + "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -19,3 +21,21 @@ func TestLoadExists(t *testing.T) { require.Nil(t, err) assert.Equal(t, "basic", b.Config.Bundle.Name) } + +func TestBundleCacheDir(t *testing.T) { + projectDir := t.TempDir() + f1, err := os.Create(filepath.Join(projectDir, "bundle.yml")) + require.NoError(t, err) + f1.Close() + + bundle, err := Load(projectDir) + require.NoError(t, err) + + // Artificially set environment. + // This is otherwise done by [mutators.SelectEnvironment]. + bundle.Config.Bundle.Environment = "default" + + cacheDir, err := bundle.CacheDir() + assert.NoError(t, err) + assert.True(t, strings.HasPrefix(cacheDir, projectDir)) +}