11package bundle
22
33import (
4+ "context"
45 "errors"
56 "fmt"
67 "io/fs"
@@ -11,6 +12,8 @@ import (
1112
1213 "github.com/databricks/cli/cmd/root"
1314 "github.com/databricks/cli/libs/cmdio"
15+ "github.com/databricks/cli/libs/dbr"
16+ "github.com/databricks/cli/libs/filer"
1417 "github.com/databricks/cli/libs/git"
1518 "github.com/databricks/cli/libs/template"
1619 "github.com/spf13/cobra"
@@ -147,6 +150,26 @@ func repoName(url string) string {
147150 return parts [len (parts )- 1 ]
148151}
149152
153+ func constructOutputFiler (ctx context.Context , outputDir string ) (filer.Filer , error ) {
154+ outputDir , err := filepath .Abs (outputDir )
155+ if err != nil {
156+ return nil , err
157+ }
158+
159+ // If the CLI is running on DBR and we're writing to the workspace file system,
160+ // use the extension-aware workspace filesystem filer to instantiate the template.
161+ //
162+ // It is not possible to write notebooks through the workspace filesystem's FUSE mount.
163+ // Therefore this is the only way we can initialize templates that contain notebooks
164+ // when running the CLI on DBR and initializing a template to the workspace.
165+ //
166+ if strings .HasPrefix (outputDir , "/Workspace/" ) && dbr .RunsOnRuntime (ctx ) {
167+ return filer .NewWorkspaceFilesExtensionsClient (root .WorkspaceClient (ctx ), outputDir )
168+ }
169+
170+ return filer .NewLocalClient (outputDir )
171+ }
172+
150173func newInitCommand () * cobra.Command {
151174 cmd := & cobra.Command {
152175 Use : "init [TEMPLATE_PATH]" ,
@@ -201,6 +224,11 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
201224 templatePath = getNativeTemplateByDescription (description )
202225 }
203226
227+ outputFiler , err := constructOutputFiler (ctx , outputDir )
228+ if err != nil {
229+ return err
230+ }
231+
204232 if templatePath == customTemplate {
205233 cmdio .LogString (ctx , "Please specify a path or Git repository to use a custom template." )
206234 cmdio .LogString (ctx , "See https://docs.databricks.com/en/dev-tools/bundles/templates.html to learn more about custom templates." )
@@ -230,7 +258,7 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
230258
231259 // skip downloading the repo because input arg is not a URL. We assume
232260 // it's a path on the local file system in that case
233- return template .Materialize (ctx , configFile , templateFS , outputDir )
261+ return template .Materialize (ctx , configFile , templateFS , outputFiler )
234262 }
235263
236264 // Create a temporary directory with the name of the repository. The '*'
@@ -255,7 +283,7 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
255283 // Clean up downloaded repository once the template is materialized.
256284 defer os .RemoveAll (repoDir )
257285 templateFS := os .DirFS (filepath .Join (repoDir , templateDir ))
258- return template .Materialize (ctx , configFile , templateFS , outputDir )
286+ return template .Materialize (ctx , configFile , templateFS , outputFiler )
259287 }
260288 return cmd
261289}
0 commit comments