From ad4b476270a779511e997e9b67ba30fbcfda2837 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Mon, 9 Oct 2023 12:37:18 +0200 Subject: [PATCH] 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 ./... ``` --- cmd/root/auth.go | 18 +++++++++++++++++- cmd/sync/sync.go | 11 ++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cmd/root/auth.go b/cmd/root/auth.go index de5648c6..ed91090e 100644 --- a/cmd/root/auth.go +++ b/cmd/root/auth.go @@ -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)) diff --git a/cmd/sync/sync.go b/cmd/sync/sync.go index 5fdfb169..7cfc1f29 100644 --- a/cmd/sync/sync.go +++ b/cmd/sync/sync.go @@ -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