diff --git a/acceptance/cmd/sync-without-args/output.txt b/acceptance/cmd/sync-without-args/output.txt deleted file mode 100644 index 6c757495507..00000000000 --- a/acceptance/cmd/sync-without-args/output.txt +++ /dev/null @@ -1,24 +0,0 @@ - ->>> musterr [CLI] sync -Error: accepts 2 arg(s), received 0 - -Usage: - databricks sync [flags] SRC DST - -Flags: - --dry-run simulate sync execution without making actual changes - --exclude strings patterns to exclude from sync (can be specified multiple times) - --exclude-from string file containing patterns to exclude from sync (one pattern per line) - --full perform full synchronization (default is incremental) - -h, --help help for sync - --include strings patterns to include in sync (can be specified multiple times) - --include-from string file containing patterns to include to sync (one pattern per line) - --interval duration file system polling interval (for --watch) (default 1s) - --watch watch local file system for changes - -Global Flags: - --debug enable debug logging - -o, --output type output type: text or json (default text) - -p, --profile string ~/.databrickscfg profile - -t, --target string bundle target to use (if applicable) - diff --git a/acceptance/cmd/sync-without-args/script b/acceptance/cmd/sync-without-args/script deleted file mode 100644 index 0841a0d4ca8..00000000000 --- a/acceptance/cmd/sync-without-args/script +++ /dev/null @@ -1 +0,0 @@ -trace musterr $CLI sync diff --git a/acceptance/cmd/sync-without-args/out.test.toml b/acceptance/cmd/sync-wrong-args/out.test.toml similarity index 100% rename from acceptance/cmd/sync-without-args/out.test.toml rename to acceptance/cmd/sync-wrong-args/out.test.toml diff --git a/acceptance/cmd/sync-wrong-args/output.txt b/acceptance/cmd/sync-wrong-args/output.txt new file mode 100644 index 00000000000..fa4842459b0 --- /dev/null +++ b/acceptance/cmd/sync-wrong-args/output.txt @@ -0,0 +1,72 @@ + +>>> musterr [CLI] sync +Error: accepts 2 arg(s), received 0 + +Usage: + databricks sync [flags] SRC DST + +Flags: + --dry-run simulate sync execution without making actual changes + --exclude strings patterns to exclude from sync (can be specified multiple times) + --exclude-from string file containing patterns to exclude from sync (one pattern per line) + --full perform full synchronization (default is incremental) + -h, --help help for sync + --include strings patterns to include in sync (can be specified multiple times) + --include-from string file containing patterns to include to sync (one pattern per line) + --interval duration file system polling interval (for --watch) (default 1s) + --watch watch local file system for changes + +Global Flags: + --debug enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) + + +>>> musterr [CLI] sync foo +Error: accepts 2 arg(s), received 1 + +Usage: + databricks sync [flags] SRC DST + +Flags: + --dry-run simulate sync execution without making actual changes + --exclude strings patterns to exclude from sync (can be specified multiple times) + --exclude-from string file containing patterns to exclude from sync (one pattern per line) + --full perform full synchronization (default is incremental) + -h, --help help for sync + --include strings patterns to include in sync (can be specified multiple times) + --include-from string file containing patterns to include to sync (one pattern per line) + --interval duration file system polling interval (for --watch) (default 1s) + --watch watch local file system for changes + +Global Flags: + --debug enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) + + +>>> musterr [CLI] sync foo bar qux +Error: accepts at most 2 arg(s), received 3 + +Usage: + databricks sync [flags] SRC DST + +Flags: + --dry-run simulate sync execution without making actual changes + --exclude strings patterns to exclude from sync (can be specified multiple times) + --exclude-from string file containing patterns to exclude from sync (one pattern per line) + --full perform full synchronization (default is incremental) + -h, --help help for sync + --include strings patterns to include in sync (can be specified multiple times) + --include-from string file containing patterns to include to sync (one pattern per line) + --interval duration file system polling interval (for --watch) (default 1s) + --watch watch local file system for changes + +Global Flags: + --debug enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) + diff --git a/acceptance/cmd/sync-wrong-args/script b/acceptance/cmd/sync-wrong-args/script new file mode 100644 index 00000000000..e06f2f9ff12 --- /dev/null +++ b/acceptance/cmd/sync-wrong-args/script @@ -0,0 +1,3 @@ +trace musterr $CLI sync +trace musterr $CLI sync foo +trace musterr $CLI sync foo bar qux diff --git a/acceptance/cmd/sync/dryrun/output.txt b/acceptance/cmd/sync/dryrun/output.txt index 091b8127014..9d405627b1f 100644 --- a/acceptance/cmd/sync/dryrun/output.txt +++ b/acceptance/cmd/sync/dryrun/output.txt @@ -47,3 +47,10 @@ Uploaded project-folder Uploaded project-folder/app.py Uploaded project-folder/app.yaml Uploaded project-folder/query.sql + +>>> [CLI] sync . /Users/[USERNAME] --dry-run -o json --exclude project-folder/** +Warn: Running in dry-run mode. No actual changes will be made. +{"type":"start","dry_run":true,"put":[".gitignore"]} +{"type":"progress","dry_run":true,"action":"put","path":".gitignore","progress":0} +{"type":"progress","dry_run":true,"action":"put","path":".gitignore","progress":1} +{"type":"complete","dry_run":true,"put":[".gitignore"]} diff --git a/acceptance/cmd/sync/dryrun/script b/acceptance/cmd/sync/dryrun/script index 46e48f1c8d9..df1844c0f6e 100644 --- a/acceptance/cmd/sync/dryrun/script +++ b/acceptance/cmd/sync/dryrun/script @@ -32,3 +32,6 @@ trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run --exclude 'project-folder/ # use negated include to exclude files from syncing: trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run --include 'ignored-folder/**' --include '!ignored-folder/folder1/big-blob' | grep -v "^Action" | sort + +# the root's -o flag selects the output format: +trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run -o json --exclude 'project-folder/**' | jq -c 'del(.timestamp, .seq)' diff --git a/cmd/bundle/sync.go b/cmd/bundle/sync.go index 955d425f208..219c82f3171 100644 --- a/cmd/bundle/sync.go +++ b/cmd/bundle/sync.go @@ -23,14 +23,16 @@ type syncFlags struct { dryRun bool } -func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) (*sync.SyncOptions, error) { - opts, err := files.GetSyncOptions(cmd.Context(), b) +// syncOptionsFromBundle returns the options to sync the bundle, writing +// progress in the output format to stdout. +func (f *syncFlags) syncOptionsFromBundle(ctx context.Context, output flags.Output, stdout io.Writer, b *bundle.Bundle) (*sync.SyncOptions, error) { + opts, err := files.GetSyncOptions(ctx, b) if err != nil { return nil, fmt.Errorf("cannot get sync options: %w", err) } var outputFunc func(context.Context, <-chan sync.Event, io.Writer) - switch root.OutputType(cmd) { + switch output { case flags.OutputText: outputFunc = sync.TextOutput case flags.OutputJSON: @@ -38,7 +40,7 @@ func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) } if outputFunc != nil { opts.OutputHandler = func(ctx context.Context, c <-chan sync.Event) { - outputFunc(ctx, c, cmd.OutOrStdout()) + outputFunc(ctx, c, stdout) } } @@ -78,7 +80,7 @@ Use 'databricks bundle deploy' for full resource deployment.`, } ctx := cmd.Context() - opts, err := f.syncOptionsFromBundle(cmd, b) + opts, err := f.syncOptionsFromBundle(ctx, root.OutputType(cmd), cmd.OutOrStdout(), b) if err != nil { return err } diff --git a/cmd/bundle/sync_test.go b/cmd/bundle/sync_test.go index 579aa69865f..8808af01a95 100644 --- a/cmd/bundle/sync_test.go +++ b/cmd/bundle/sync_test.go @@ -1,30 +1,27 @@ package bundle import ( + "io" "testing" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" - "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/vfs" - "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -// newTestSyncCommand attaches the sync command to a root command so the root persistent flags resolve. -func newTestSyncCommand(t *testing.T) *cobra.Command { - syncCmd := newSyncCommand() - root.New(t.Context()).AddCommand(syncCmd) - return syncCmd -} - -func TestBundleSyncShorthandFlags(t *testing.T) { - cmd := newTestSyncCommand(t) - require.NoError(t, cmd.ParseFlags([]string{"-o", "json", "-p", "myprofile"})) - assert.Equal(t, flags.OutputJSON, root.OutputType(cmd)) - assert.Equal(t, "myprofile", cmd.Flag("profile").Value.String()) +// Flags that bundle sync defines with the names or shorthands of the root's +// --output and --profile flags would shadow them. +func TestBundleSyncDoesNotShadowRootFlags(t *testing.T) { + cmd := newSyncCommand() + for _, name := range []string{"output", "profile"} { + assert.Nil(t, cmd.LocalFlags().Lookup(name), name) + } + for _, shorthand := range []string{"o", "p"} { + assert.Nil(t, cmd.LocalFlags().ShorthandLookup(shorthand), shorthand) + } } func TestBundleSyncInstallsOutputHandler(t *testing.T) { @@ -46,19 +43,9 @@ func TestBundleSyncInstallsOutputHandler(t *testing.T) { f := syncFlags{} - // bundle sync defaults to text output, matching the standalone sync command: - // the root --output flag resolves to text when unset, so a handler is installed. - cmd := newTestSyncCommand(t) - cmd.SetContext(t.Context()) - require.NoError(t, cmd.ParseFlags(nil)) - opts, err := f.syncOptionsFromBundle(cmd, b) - require.NoError(t, err) - assert.NotNil(t, opts.OutputHandler) - - cmd = newTestSyncCommand(t) - cmd.SetContext(t.Context()) - require.NoError(t, cmd.ParseFlags([]string{"-o", "json"})) - opts, err = f.syncOptionsFromBundle(cmd, b) - require.NoError(t, err) - assert.NotNil(t, opts.OutputHandler) + for _, output := range []flags.Output{flags.OutputText, flags.OutputJSON} { + opts, err := f.syncOptionsFromBundle(t.Context(), output, io.Discard, b) + require.NoError(t, err) + assert.NotNil(t, opts.OutputHandler, output) + } } diff --git a/cmd/sync/sync.go b/cmd/sync/sync.go index 027ef79a4a0..dd56ead34e0 100644 --- a/cmd/sync/sync.go +++ b/cmd/sync/sync.go @@ -60,12 +60,12 @@ func readPatternsFile(flagName, filePath string) ([]string, error) { return patterns, nil } -func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, args []string, b *bundle.Bundle) (*sync.SyncOptions, error) { +func (f *syncFlags) syncOptionsFromBundle(ctx context.Context, args []string, b *bundle.Bundle) (*sync.SyncOptions, error) { if len(args) > 0 { return nil, errors.New("SRC and DST are not configurable in the context of a bundle") } - opts, err := files.GetSyncOptions(cmd.Context(), b) + opts, err := files.GetSyncOptions(ctx, b) if err != nil { return nil, fmt.Errorf("cannot get sync options: %w", err) } @@ -91,13 +91,15 @@ func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, args []string, b * return opts, nil } -func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*sync.SyncOptions, error) { - if err := root.ExactArgs(2)(cmd, args); err != nil { - return nil, err +// syncOptionsFromArgs returns the options to sync the SRC and DST in args, +// writing progress in the output format to stdout. +func (f *syncFlags) syncOptionsFromArgs(ctx context.Context, output flags.Output, stdout io.Writer, args []string) (*sync.SyncOptions, error) { + if len(args) != 2 { + return nil, fmt.Errorf("expected SRC and DST, got %d arguments", len(args)) } var outputFunc func(context.Context, <-chan sync.Event, io.Writer) - switch root.OutputType(cmd) { + switch output { case flags.OutputText: outputFunc = sync.TextOutput case flags.OutputJSON: @@ -107,11 +109,10 @@ func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*syn var outputHandler sync.OutputHandler if outputFunc != nil { outputHandler = func(ctx context.Context, events <-chan sync.Event) { - outputFunc(ctx, events, cmd.OutOrStdout()) + outputFunc(ctx, events, stdout) } } - ctx := cmd.Context() client := cmdctx.WorkspaceClient(ctx) if f.dryRun { @@ -195,7 +196,10 @@ func New() *cobra.Command { var opts *sync.SyncOptions var err error - opts, err = f.syncOptionsFromArgs(cmd, args) + if err := root.ExactArgs(2)(cmd, args); err != nil { + return err + } + opts, err = f.syncOptionsFromArgs(cmd.Context(), root.OutputType(cmd), cmd.OutOrStdout(), args) if err != nil { return err } diff --git a/cmd/sync/sync_test.go b/cmd/sync/sync_test.go index 757defc34f8..e4cb0f183ff 100644 --- a/cmd/sync/sync_test.go +++ b/cmd/sync/sync_test.go @@ -1,6 +1,7 @@ package sync import ( + "io" "os" "path/filepath" "strings" @@ -8,22 +9,13 @@ import ( "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" - "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/vfs" - "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -// newTestSyncCommand attaches the sync command to a root command so the root persistent flags resolve. -func newTestSyncCommand(t *testing.T) *cobra.Command { - syncCmd := New() - root.New(t.Context()).AddCommand(syncCmd) - return syncCmd -} - func TestSyncOptionsFromBundle(t *testing.T) { tempDir := t.TempDir() b := &bundle.Bundle{ @@ -43,7 +35,7 @@ func TestSyncOptionsFromBundle(t *testing.T) { } f := syncFlags{} - opts, err := f.syncOptionsFromBundle(New(), []string{}, b) + opts, err := f.syncOptionsFromBundle(t.Context(), []string{}, b) require.NoError(t, err) assert.Equal(t, tempDir, opts.LocalRoot.Native()) assert.Equal(t, "/Users/jane@doe.com/path", opts.RemotePath) @@ -51,35 +43,34 @@ func TestSyncOptionsFromBundle(t *testing.T) { assert.NotNil(t, opts.WorkspaceClient) } -func TestSyncOptionsFromArgsRequiredTwoArgs(t *testing.T) { - var err error - f := syncFlags{} - _, err = f.syncOptionsFromArgs(New(), []string{}) - require.ErrorContains(t, err, "accepts 2 arg(s), received 0") - _, err = f.syncOptionsFromArgs(New(), []string{"foo"}) - require.ErrorContains(t, err, "accepts 2 arg(s), received 1") - _, err = f.syncOptionsFromArgs(New(), []string{"foo", "bar", "qux"}) - require.ErrorContains(t, err, "accepts 2 arg(s), received 3") -} - func TestSyncOptionsFromArgs(t *testing.T) { local := t.TempDir() remote := "/remote" f := syncFlags{} - cmd := newTestSyncCommand(t) - cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), nil)) - opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote}) + ctx := cmdctx.SetWorkspaceClient(t.Context(), nil) + opts, err := f.syncOptionsFromArgs(ctx, flags.OutputText, io.Discard, []string{local, remote}) require.NoError(t, err) assert.Equal(t, local, opts.LocalRoot.Native()) assert.Equal(t, remote, opts.RemotePath) } -func TestSyncShorthandFlags(t *testing.T) { - cmd := newTestSyncCommand(t) - require.NoError(t, cmd.ParseFlags([]string{"-o", "json", "-p", "myprofile"})) - assert.Equal(t, flags.OutputJSON, root.OutputType(cmd)) - assert.Equal(t, "myprofile", cmd.Flag("profile").Value.String()) +func TestSyncOptionsFromArgsRequiresSrcAndDst(t *testing.T) { + f := syncFlags{} + _, err := f.syncOptionsFromArgs(t.Context(), flags.OutputText, io.Discard, []string{"src"}) + assert.EqualError(t, err, "expected SRC and DST, got 1 arguments") +} + +// Flags that sync defines with the names or shorthands of the root's +// --output and --profile flags would shadow them. +func TestSyncDoesNotShadowRootFlags(t *testing.T) { + cmd := New() + for _, name := range []string{"output", "profile"} { + assert.Nil(t, cmd.LocalFlags().Lookup(name), name) + } + for _, shorthand := range []string{"o", "p"} { + assert.Nil(t, cmd.LocalFlags().ShorthandLookup(shorthand), shorthand) + } } func TestReadPatternsFileErrorNamesFlag(t *testing.T) { @@ -108,12 +99,11 @@ func TestExcludeFromFlag(t *testing.T) { // Set up the flags f := syncFlags{excludeFrom: excludeFromPath} - cmd := newTestSyncCommand(t) - cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), nil)) + ctx := cmdctx.SetWorkspaceClient(t.Context(), nil) // Test with both exclude flag and exclude-from flag f.exclude = []string{"node_modules/"} - opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote}) + opts, err := f.syncOptionsFromArgs(ctx, flags.OutputText, io.Discard, []string{local, remote}) require.NoError(t, err) // Should include both exclude flag and exclude-from patterns