Skip to content

Commit

Permalink
Centralize code to detect if we are running on Databricks
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs committed Sep 4, 2024
1 parent 08b6b10 commit 90d6490
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 2 additions & 4 deletions bundle/config/mutator/configure_wsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/runtime"
"github.com/databricks/cli/libs/vfs"
)

const envDatabricksRuntimeVersion = "DATABRICKS_RUNTIME_VERSION"

type configureWSFS struct{}

func ConfigureWSFS() bundle.Mutator {
Expand All @@ -32,7 +30,7 @@ func (m *configureWSFS) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagno
}

// The executable must be running on DBR.
if _, ok := env.Lookup(ctx, envDatabricksRuntimeVersion); !ok {
if !runtime.RunsOnDatabricks(ctx) {
return nil
}

Expand Down
14 changes: 14 additions & 0 deletions libs/runtime/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package runtime

import (
"context"

"github.com/databricks/cli/libs/env"
)

const envDatabricksRuntimeVersion = "DATABRICKS_RUNTIME_VERSION"

func RunsOnDatabricks(ctx context.Context) bool {
_, ok := env.Lookup(ctx, envDatabricksRuntimeVersion)
return ok
}
9 changes: 2 additions & 7 deletions libs/template/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/runtime"
"github.com/databricks/databricks-sdk-go/service/workspace"
)

Expand Down Expand Up @@ -107,13 +107,8 @@ func (f *inMemoryFile) PersistToDisk() error {
return writeFile(f.ctx, path, f.content, f.perm)
}

func runsOnDatabricks(ctx context.Context) bool {
_, ok := env.Lookup(ctx, "DATABRICKS_RUNTIME_VERSION")
return ok
}

func shouldUseImportNotebook(ctx context.Context, path string) bool {
return strings.HasPrefix(path, "/Workspace/") && runsOnDatabricks(ctx) && strings.HasSuffix(path, ".ipynb")
return strings.HasPrefix(path, "/Workspace/") && runtime.RunsOnDatabricks(ctx) && strings.HasSuffix(path, ".ipynb")
}

func writeFile(ctx context.Context, path string, content []byte, perm fs.FileMode) error {
Expand Down

0 comments on commit 90d6490

Please sign in to comment.