Centralize code to detect if we are running on Databricks

This commit is contained in:
Fabian Jakobs 2024-09-04 11:40:15 +02:00
parent 08b6b10ff0
commit 90d6490106
No known key found for this signature in database
3 changed files with 18 additions and 11 deletions

View File

@ -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 {
@ -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
}

14
libs/runtime/detect.go Normal file
View File

@ -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
}

View File

@ -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"
)
@ -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 {