forked from databricks/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
124 lines (105 loc) · 3.66 KB
/
Copy pathsync.go
File metadata and controls
124 lines (105 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bundle
import (
"context"
"fmt"
"io"
"time"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/deploy/files"
"github.com/databricks/cli/cmd/bundle/utils"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/sync"
"github.com/spf13/cobra"
)
type syncFlags struct {
interval time.Duration
full bool
watch bool
output flags.Output
dryRun bool
concurrency int
retryTimeout time.Duration
}
func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) (*sync.SyncOptions, error) {
opts, err := files.GetSyncOptions(cmd.Context(), b)
if err != nil {
return nil, fmt.Errorf("cannot get sync options: %w", err)
}
if f.output != "" {
var outputFunc func(context.Context, <-chan sync.Event, io.Writer)
switch f.output {
case flags.OutputText:
outputFunc = sync.TextOutput
case flags.OutputJSON:
outputFunc = sync.JsonOutput
}
if outputFunc != nil {
opts.OutputHandler = func(ctx context.Context, c <-chan sync.Event) {
outputFunc(ctx, c, cmd.OutOrStdout())
}
}
}
opts.Full = f.full
opts.PollInterval = f.interval
opts.DryRun = f.dryRun
opts.Concurrency = f.concurrency
opts.RetryTimeout = f.retryTimeout
return opts, nil
}
func newSyncCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sync [flags]",
Short: "Synchronize bundle tree to the workspace",
Long: `Synchronize bundle files to Databricks workspace for development.
Use sync during development to upload local changes without full deployment:
databricks bundle sync # One-time sync of all files
databricks bundle sync --watch # Continuously watch and sync changes
databricks bundle sync --full # Force full sync (vs incremental)
databricks bundle sync --dry-run # Preview what would be synced
Sync is faster than deploy for rapid development but doesn't update job/pipeline definitions.
Use 'databricks bundle deploy' for full resource deployment.`,
Args: root.NoArgs,
}
var f syncFlags
cmd.Flags().DurationVar(&f.interval, "interval", 1*time.Second, "file system polling interval (for --watch)")
cmd.Flags().BoolVar(&f.full, "full", false, "perform full synchronization (default is incremental)")
cmd.Flags().BoolVar(&f.watch, "watch", false, "watch local file system for changes")
cmd.Flags().Var(&f.output, "output", "type of the output format")
cmd.Flags().BoolVar(&f.dryRun, "dry-run", false, "simulate sync execution without making actual changes")
cmd.Flags().IntVar(&f.concurrency, "concurrency", 5, "maximum number of concurrent in-flight requests during sync")
cmd.Flags().DurationVar(&f.retryTimeout, "retry-timeout", sync.DefaultRetryTimeout, "per-call deadline for retrying transient gateway errors (HTTP 502/503/504)")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if f.concurrency < 1 {
return fmt.Errorf("--concurrency must be a positive integer, got %d", f.concurrency)
}
if f.retryTimeout < 0 {
return fmt.Errorf("--retry-timeout must be non-negative, got %s", f.retryTimeout)
}
b, err := utils.ProcessBundle(cmd, utils.ProcessOptions{})
if err != nil {
return err
}
ctx := cmd.Context()
opts, err := f.syncOptionsFromBundle(cmd, b)
if err != nil {
return err
}
s, err := sync.New(ctx, *opts)
if err != nil {
return err
}
defer s.Close()
log.Infof(ctx, "Remote file sync location: %v", opts.RemotePath)
if opts.DryRun {
log.Warnf(ctx, "Running in dry-run mode. No actual changes will be made.")
}
if f.watch {
return s.RunContinuous(ctx)
}
_, err = s.RunOnce(ctx)
return err
}
return cmd
}