Skip to content

Commit

Permalink
Move WorkspaceClient to libs/command (#2444)
Browse files Browse the repository at this point in the history
## Changes
Move the WorkspaceClient reader and setter from the root package to
`libs/command`.

## Why
This allows us to read the workspace client set in the context in all
CLI packages. This was not possible before because using
`root.WorkspaceClient` would often result in an import cycle.

This would also allow us to standardise reads of the workspace client to
be from the context in bundle commands. Today those are read from the
bundle object tree instead.

This is a natural followup to
#2440.

## Tests
Existing tests and one new unit test.

NO_CHANGELOG=true
  • Loading branch information
shreyas-goenka authored Mar 7, 2025
1 parent 0225c77 commit af3914d
Show file tree
Hide file tree
Showing 123 changed files with 829 additions and 669 deletions.
3 changes: 2 additions & 1 deletion .codegen/service.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package {{(.TrimPrefix "account").SnakeName}}

import (
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/command"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/cmd/root"
Expand Down Expand Up @@ -240,7 +241,7 @@ func new{{.PascalName}}() *cobra.Command {
cmd.PreRunE = root.Must{{if .Service.IsAccounts}}Account{{else}}Workspace{{end}}Client
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
{{if .Service.IsAccounts}}a := root.AccountClient(ctx){{else}}w := root.WorkspaceClient(ctx){{end}}
{{if .Service.IsAccounts}}a := root.AccountClient(ctx){{else}}w := command.WorkspaceClient(ctx){{end}}
{{- if .Request }}
{{ if $canUseJson }}
if cmd.Flags().Changed("json") {
Expand Down
2 changes: 1 addition & 1 deletion cmd/auth/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try
return &status, nil
}

w := root.WorkspaceClient(ctx)
w := command.WorkspaceClient(ctx)
me, err := w.CurrentUser.Me(ctx)
if err != nil {
return &authStatus{
Expand Down
7 changes: 4 additions & 3 deletions cmd/auth/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/command"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/iam"
Expand All @@ -17,7 +18,7 @@ import (
func TestGetWorkspaceAuthStatus(t *testing.T) {
ctx := context.Background()
m := mocks.NewMockWorkspaceClient(t)
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient)
ctx = command.SetWorkspaceClient(ctx, m.WorkspaceClient)

cmd := &cobra.Command{}
cmd.SetContext(ctx)
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestGetWorkspaceAuthStatus(t *testing.T) {
func TestGetWorkspaceAuthStatusError(t *testing.T) {
ctx := context.Background()
m := mocks.NewMockWorkspaceClient(t)
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient)
ctx = command.SetWorkspaceClient(ctx, m.WorkspaceClient)

cmd := &cobra.Command{}
cmd.SetContext(ctx)
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestGetWorkspaceAuthStatusError(t *testing.T) {
func TestGetWorkspaceAuthStatusSensitive(t *testing.T) {
ctx := context.Background()
m := mocks.NewMockWorkspaceClient(t)
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient)
ctx = command.SetWorkspaceClient(ctx, m.WorkspaceClient)

cmd := &cobra.Command{}
cmd.SetContext(ctx)
Expand Down
3 changes: 2 additions & 1 deletion cmd/fs/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/command"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/filer/completer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,7 +36,7 @@ func filerForPath(ctx context.Context, fullPath string) (filer.Filer, string, er
}

path := parts[1]
w := root.WorkspaceClient(ctx)
w := command.WorkspaceClient(ctx)

// If the specified path has the "Volumes" prefix, use the Files API.
if strings.HasPrefix(path, "/Volumes/") {
Expand Down
4 changes: 2 additions & 2 deletions cmd/fs/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/command"
"github.com/databricks/cli/libs/fakefs"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
Expand Down Expand Up @@ -73,7 +73,7 @@ func mockMustWorkspaceClientFunc(cmd *cobra.Command, args []string) error {
func setupCommand(t *testing.T) (*cobra.Command, *mocks.MockWorkspaceClient) {
m := mocks.NewMockWorkspaceClient(t)
ctx := context.Background()
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient)
ctx = command.SetWorkspaceClient(ctx, m.WorkspaceClient)

cmd := &cobra.Command{}
cmd.SetContext(ctx)
Expand Down
17 changes: 2 additions & 15 deletions cmd/root/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (

// Placeholders to use as unique keys in context.Context.
var (
workspaceClient int
accountClient int
accountClient int
)

type ErrNoWorkspaceProfiles struct {
Expand Down Expand Up @@ -229,15 +228,11 @@ func MustWorkspaceClient(cmd *cobra.Command, args []string) error {
return err
}

ctx = context.WithValue(ctx, &workspaceClient, w)
ctx = command.SetWorkspaceClient(ctx, w)
cmd.SetContext(ctx)
return nil
}

func SetWorkspaceClient(ctx context.Context, w *databricks.WorkspaceClient) context.Context {
return context.WithValue(ctx, &workspaceClient, w)
}

func SetAccountClient(ctx context.Context, a *databricks.AccountClient) context.Context {
return context.WithValue(ctx, &accountClient, a)
}
Expand Down Expand Up @@ -321,14 +316,6 @@ func emptyHttpRequest(ctx context.Context) *http.Request {
return req
}

func WorkspaceClient(ctx context.Context) *databricks.WorkspaceClient {
w, ok := ctx.Value(&workspaceClient).(*databricks.WorkspaceClient)
if !ok {
panic("cannot get *databricks.WorkspaceClient. Please report it as a bug")
}
return w
}

func AccountClient(ctx context.Context) *databricks.AccountClient {
a, ok := ctx.Value(&accountClient).(*databricks.AccountClient)
if !ok {
Expand Down
3 changes: 2 additions & 1 deletion cmd/root/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/command"
"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -263,7 +264,7 @@ func TestMustAnyClientCanCreateWorkspaceClient(t *testing.T) {
require.False(t, isAccount)
require.NoError(t, err)

w := WorkspaceClient(cmd.Context())
w := command.WorkspaceClient(cmd.Context())
require.NotNil(t, w)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/deploy/files"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/command"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/git"
"github.com/databricks/cli/libs/log"
Expand Down Expand Up @@ -65,7 +66,7 @@ func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*syn
}

ctx := cmd.Context()
client := root.WorkspaceClient(ctx)
client := command.WorkspaceClient(ctx)

localRoot := vfs.MustNew(args[0])
info, err := git.FetchRepositoryInfo(ctx, localRoot.Native(), client)
Expand Down Expand Up @@ -186,7 +187,7 @@ func New() *cobra.Command {
case 0:
return nil, cobra.ShellCompDirectiveFilterDirs
case 1:
wsc := root.WorkspaceClient(cmd.Context())
wsc := command.WorkspaceClient(cmd.Context())
return completeRemotePath(cmd.Context(), wsc, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
4 changes: 2 additions & 2 deletions cmd/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/command"
"github.com/databricks/cli/libs/vfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestSyncOptionsFromArgs(t *testing.T) {

f := syncFlags{}
cmd := New()
cmd.SetContext(root.SetWorkspaceClient(context.Background(), nil))
cmd.SetContext(command.SetWorkspaceClient(context.Background(), nil))
opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote})
require.NoError(t, err)
assert.Equal(t, local, opts.LocalRoot.Native())
Expand Down
3 changes: 2 additions & 1 deletion cmd/workspace/access-control/access-control.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions cmd/workspace/alerts-legacy/alerts-legacy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions cmd/workspace/alerts/alerts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af3914d

Please sign in to comment.