Skip to content

Commit d0b6f25

Browse files
update template library
1 parent 67b9cce commit d0b6f25

File tree

9 files changed

+29
-46
lines changed

9 files changed

+29
-46
lines changed

cmd/bundle/generate/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ per target environment.`,
7878
return err
7979
}
8080

81-
outputFiler, err := filer.NewOutputFiler(ctx, w, b.BundleRootPath)
81+
outputFiler, err := filer.NewOutputFiler(ctx, b.BundleRootPath)
8282
if err != nil {
8383
return err
8484
}

cmd/bundle/generate/dashboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (d *dashboard) initialize(ctx context.Context, b *bundle.Bundle) {
364364
d.relativeDashboardDir = filepath.ToSlash(rel)
365365

366366
// Construct output filer for writing files.
367-
outputFiler, err := filer.NewOutputFiler(ctx, b.WorkspaceClient(), b.BundleRootPath)
367+
outputFiler, err := filer.NewOutputFiler(ctx, b.BundleRootPath)
368368
if err != nil {
369369
logdiag.LogError(ctx, err)
370370
return

cmd/bundle/generate/job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ After generation, you can deploy this job to other targets using:
108108
return err
109109
}
110110

111-
outputFiler, err := filer.NewOutputFiler(ctx, w, b.BundleRootPath)
111+
outputFiler, err := filer.NewOutputFiler(ctx, b.BundleRootPath)
112112
if err != nil {
113113
return err
114114
}

cmd/bundle/generate/pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ like catalogs, schemas, and compute configurations per target.`,
7979
return err
8080
}
8181

82-
outputFiler, err := filer.NewOutputFiler(ctx, w, b.BundleRootPath)
82+
outputFiler, err := filer.NewOutputFiler(ctx, b.BundleRootPath)
8383
if err != nil {
8484
return err
8585
}

libs/filer/output_filer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"path/filepath"
66
"strings"
77

8+
"github.com/databricks/cli/libs/cmdctx"
89
"github.com/databricks/cli/libs/dbr"
9-
"github.com/databricks/databricks-sdk-go"
1010
)
1111

1212
// NewOutputFiler creates a filer for writing output files.
@@ -16,7 +16,7 @@ import (
1616
//
1717
// It is not possible to write notebooks through the workspace filesystem's FUSE mount for DBR versions less than 16.4.
1818
// This function ensures the correct filer is used based on the runtime environment.
19-
func NewOutputFiler(ctx context.Context, w *databricks.WorkspaceClient, outputDir string) (Filer, error) {
19+
func NewOutputFiler(ctx context.Context, outputDir string) (Filer, error) {
2020
outputDir, err := filepath.Abs(outputDir)
2121
if err != nil {
2222
return nil, err
@@ -25,6 +25,7 @@ func NewOutputFiler(ctx context.Context, w *databricks.WorkspaceClient, outputDi
2525
// If the CLI is running on DBR and we're writing to the workspace file system,
2626
// use the extension-aware workspace filesystem filer.
2727
if strings.HasPrefix(outputDir, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
28+
w := cmdctx.WorkspaceClient(ctx)
2829
return NewWorkspaceFilesExtensionsClient(w, outputDir)
2930
}
3031

libs/filer/output_filer_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"runtime"
66
"testing"
77

8+
"github.com/databricks/cli/libs/cmdctx"
89
"github.com/databricks/cli/libs/dbr"
910
"github.com/databricks/databricks-sdk-go"
1011
workspaceConfig "github.com/databricks/databricks-sdk-go/config"
@@ -15,12 +16,8 @@ import (
1516
func TestNewOutputFilerLocal(t *testing.T) {
1617
ctx := dbr.MockRuntime(context.Background(), dbr.Environment{IsDbr: false})
1718

18-
w := &databricks.WorkspaceClient{
19-
Config: &workspaceConfig.Config{Host: "https://myhost.com"},
20-
}
21-
2219
tmpDir := t.TempDir()
23-
f, err := NewOutputFiler(ctx, w, tmpDir)
20+
f, err := NewOutputFiler(ctx, tmpDir)
2421
require.NoError(t, err)
2522

2623
assert.IsType(t, &LocalClient{}, f)
@@ -35,12 +32,8 @@ func TestNewOutputFilerLocalForNonWorkspacePath(t *testing.T) {
3532
// Even on DBR, if path doesn't start with /Workspace/, use local client
3633
ctx := dbr.MockRuntime(context.Background(), dbr.Environment{IsDbr: true, Version: "15.4"})
3734

38-
w := &databricks.WorkspaceClient{
39-
Config: &workspaceConfig.Config{Host: "https://myhost.com"},
40-
}
41-
4235
tmpDir := t.TempDir()
43-
f, err := NewOutputFiler(ctx, w, tmpDir)
36+
f, err := NewOutputFiler(ctx, tmpDir)
4437
require.NoError(t, err)
4538

4639
assert.IsType(t, &LocalClient{}, f)
@@ -53,13 +46,12 @@ func TestNewOutputFilerDBR(t *testing.T) {
5346
}
5447

5548
ctx := dbr.MockRuntime(context.Background(), dbr.Environment{IsDbr: true, Version: "15.4"})
56-
57-
w := &databricks.WorkspaceClient{
49+
ctx = cmdctx.SetWorkspaceClient(ctx, &databricks.WorkspaceClient{
5850
Config: &workspaceConfig.Config{Host: "https://myhost.com"},
59-
}
51+
})
6052

6153
// On DBR with /Workspace/ path, should use workspace files extensions client
62-
f, err := NewOutputFiler(ctx, w, "/Workspace/Users/[email protected]/my-bundle")
54+
f, err := NewOutputFiler(ctx, "/Workspace/Users/[email protected]/my-bundle")
6355
require.NoError(t, err)
6456

6557
assert.IsType(t, &WorkspaceFilesExtensionsClient{}, f)

libs/template/resolver_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@ import (
55
"testing"
66

77
"github.com/databricks/cli/libs/cmdio"
8+
"github.com/databricks/cli/libs/dbr"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
)
1112

13+
func testContext(t *testing.T) context.Context {
14+
return dbr.DetectRuntime(context.Background())
15+
}
16+
1217
func TestTemplateResolverBothTagAndBranch(t *testing.T) {
1318
r := Resolver{
1419
Tag: "tag",
1520
Branch: "branch",
1621
}
1722

18-
_, err := r.Resolve(context.Background())
23+
_, err := r.Resolve(testContext(t))
1924
assert.EqualError(t, err, "only one of tag or branch can be specified")
2025
}
2126

2227
func TestTemplateResolverErrorsWhenPromptingIsNotSupported(t *testing.T) {
2328
r := Resolver{}
24-
ctx := cmdio.MockDiscard(context.Background())
29+
ctx := cmdio.MockDiscard(testContext(t))
2530

2631
_, err := r.Resolve(ctx)
2732
assert.EqualError(t, err, "prompting is not supported. Please specify the path, name or URL of the template to use")
@@ -38,7 +43,7 @@ func TestTemplateResolverForDefaultTemplates(t *testing.T) {
3843
TemplatePathOrUrl: name,
3944
}
4045

41-
tmpl, err := r.Resolve(context.Background())
46+
tmpl, err := r.Resolve(testContext(t))
4247
require.NoError(t, err)
4348

4449
assert.Equal(t, &builtinReader{name: name}, tmpl.Reader)
@@ -52,7 +57,7 @@ func TestTemplateResolverForDefaultTemplates(t *testing.T) {
5257
ConfigFile: "/config/file",
5358
}
5459

55-
tmpl, err := r.Resolve(context.Background())
60+
tmpl, err := r.Resolve(testContext(t))
5661
require.NoError(t, err)
5762

5863
// Assert reader and writer configuration
@@ -69,7 +74,7 @@ func TestTemplateResolverForCustomUrl(t *testing.T) {
6974
ConfigFile: "/config/file",
7075
}
7176

72-
tmpl, err := r.Resolve(context.Background())
77+
tmpl, err := r.Resolve(testContext(t))
7378
require.NoError(t, err)
7479

7580
assert.Equal(t, Custom, tmpl.name)
@@ -89,7 +94,7 @@ func TestTemplateResolverForCustomPath(t *testing.T) {
8994
ConfigFile: "/config/file",
9095
}
9196

92-
tmpl, err := r.Resolve(context.Background())
97+
tmpl, err := r.Resolve(testContext(t))
9398
require.NoError(t, err)
9499

95100
assert.Equal(t, Custom, tmpl.name)

libs/template/writer.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package template
22

33
import (
44
"context"
5-
"path/filepath"
65
"sort"
76
"strconv"
8-
"strings"
97

10-
"github.com/databricks/cli/libs/cmdctx"
118
"github.com/databricks/cli/libs/cmdio"
12-
"github.com/databricks/cli/libs/dbr"
139
"github.com/databricks/cli/libs/filer"
1410
"github.com/databricks/cli/libs/jsonschema"
1511
"github.com/databricks/cli/libs/telemetry"
@@ -49,20 +45,7 @@ type defaultWriter struct {
4945
func (tmpl *defaultWriter) Configure(ctx context.Context, configPath, outputDir string) error {
5046
tmpl.configPath = configPath
5147

52-
// Workspace client is only needed when running on DBR and writing to /Workspace/.
53-
// We avoid calling cmdctx.WorkspaceClient unconditionally because it panics
54-
// if the workspace client is not set in the context.
55-
var outputFiler filer.Filer
56-
var err error
57-
absOutputDir, err := filepath.Abs(outputDir)
58-
if err != nil {
59-
return err
60-
}
61-
if strings.HasPrefix(absOutputDir, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
62-
outputFiler, err = filer.NewOutputFiler(ctx, cmdctx.WorkspaceClient(ctx), outputDir)
63-
} else {
64-
outputFiler, err = filer.NewLocalClient(absOutputDir)
65-
}
48+
outputFiler, err := filer.NewOutputFiler(ctx, outputDir)
6649
if err != nil {
6750
return err
6851
}

libs/template/writer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import (
1515
)
1616

1717
func TestDefaultWriterConfigure(t *testing.T) {
18+
ctx := dbr.DetectRuntime(context.Background())
19+
1820
// Test on local file system.
1921
w := &defaultWriter{}
20-
err := w.Configure(context.Background(), "/foo/bar", "/out/abc")
22+
err := w.Configure(ctx, "/foo/bar", "/out/abc")
2123
assert.NoError(t, err)
2224

2325
assert.Equal(t, "/foo/bar", w.configPath)
@@ -46,7 +48,7 @@ func TestDefaultWriterConfigureOnDBR(t *testing.T) {
4648
func TestMaterializeForNonTemplateDirectory(t *testing.T) {
4749
tmpDir1 := t.TempDir()
4850
tmpDir2 := t.TempDir()
49-
ctx := context.Background()
51+
ctx := dbr.DetectRuntime(context.Background())
5052

5153
w := &defaultWriter{}
5254
err := w.Configure(ctx, "/foo/bar", tmpDir1)

0 commit comments

Comments
 (0)