Skip to content

Commit

Permalink
Ensure profile flag is respected for sync command (#837)
Browse files Browse the repository at this point in the history
## Changes
Fixes #836 

## Tests
Manually running `sync` command with and without the flag

Integration tests pass as well

```
--- PASS: TestAccSyncFullFileSync (13.38s)
PASS
coverage: 39.1% of statements in ./...
ok      github.com/databricks/cli/internal      14.148s coverage: 39.1% of statements in ./...


--- PASS: TestAccSyncIncrementalFileSync (11.38s)
PASS
coverage: 39.1% of statements in ./...
ok      github.com/databricks/cli/internal      11.674s coverage: 39.1% of statements in ./...
```
  • Loading branch information
andrewnester authored Oct 9, 2023
1 parent 8d8de3f commit ad4b476
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 17 additions & 1 deletion cmd/root/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func MustAccountClient(cmd *cobra.Command, args []string) error {
}
}

allowPrompt := !hasProfileFlag
noPrompt, ok := cmd.Context().Value(noPromptKey).(bool)
allowPrompt := !hasProfileFlag && (!ok || !noPrompt)
a, err := accountClientOrPrompt(cmd.Context(), cfg, allowPrompt)
if err != nil {
return err
Expand All @@ -102,6 +103,21 @@ func MustAccountClient(cmd *cobra.Command, args []string) error {
return nil
}

type noPrompt int

var noPromptKey noPrompt

// NoPrompt allows to skip prompt for profile configuration in MustWorkspaceClient.
//
// When calling MustWorkspaceClient we want to be able to customise if to show prompt or not.
// Since we can't change function interface, in the code we only have an access to `cmd“ object.
// Command struct does not have any state flag which indicates that it's being called in completion mode and
// thus the Context object seems to be the only viable option for us to configure prompt behaviour based on
// the context it's executed from.
func NoPrompt(ctx context.Context) context.Context {
return context.WithValue(ctx, noPromptKey, true)
}

// Helper function to create a workspace client or prompt once if the given configuration is not valid.
func workspaceClientOrPrompt(ctx context.Context, cfg *config.Config, allowPrompt bool) (*databricks.WorkspaceClient, error) {
w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg))
Expand Down
11 changes: 6 additions & 5 deletions cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func New() *cobra.Command {
cmd.Flags().BoolVar(&f.watch, "watch", false, "watch local file system for changes")
cmd.Flags().Var(&f.output, "output", "type of output format")

cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) error {
var opts *sync.SyncOptions
var err error
Expand Down Expand Up @@ -149,7 +150,10 @@ func New() *cobra.Command {
}

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := root.TryConfigureBundle(cmd, args)
ctx := cmd.Context()
cmd.SetContext(root.NoPrompt(ctx))

err := root.MustWorkspaceClient(cmd, args)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -165,10 +169,7 @@ func New() *cobra.Command {
case 0:
return nil, cobra.ShellCompDirectiveFilterDirs
case 1:
wsc, err := databricks.NewWorkspaceClient()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
wsc := root.WorkspaceClient(cmd.Context())
return completeRemotePath(cmd.Context(), wsc, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down

0 comments on commit ad4b476

Please sign in to comment.