Ensure profile flag is respected for sync command (#837)

## 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 ./...
```
This commit is contained in:
Andrew Nester 2023-10-09 12:37:18 +02:00 committed by GitHub
parent 8d8de3f509
commit ad4b476270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

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

View File

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