2023-02-20 10:33:30 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/phases"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/databricks/cli/cmd/bundle/utils"
|
2024-03-12 14:12:34 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
|
|
|
"github.com/databricks/cli/libs/sync"
|
2023-02-20 10:33:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
type syncFlags struct {
|
|
|
|
interval time.Duration
|
|
|
|
full bool
|
|
|
|
watch bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) (*sync.SyncOptions, error) {
|
2023-09-11 08:18:43 +00:00
|
|
|
cacheDir, err := b.CacheDir(cmd.Context())
|
2023-02-20 10:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get bundle cache directory: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
includes, err := b.GetSyncIncludePatterns(cmd.Context())
|
2023-08-18 08:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get list of sync includes: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:33:30 +00:00
|
|
|
opts := sync.SyncOptions{
|
|
|
|
LocalPath: b.Config.Path,
|
2023-11-15 13:37:26 +00:00
|
|
|
RemotePath: b.Config.Workspace.FilePath,
|
2023-08-18 08:07:25 +00:00
|
|
|
Include: includes,
|
|
|
|
Exclude: b.Config.Sync.Exclude,
|
2023-07-27 10:03:08 +00:00
|
|
|
Full: f.full,
|
|
|
|
PollInterval: f.interval,
|
2023-02-20 10:33:30 +00:00
|
|
|
|
|
|
|
SnapshotBasePath: cacheDir,
|
|
|
|
WorkspaceClient: b.WorkspaceClient(),
|
|
|
|
}
|
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newSyncCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "sync [flags]",
|
|
|
|
Short: "Synchronize bundle tree to the workspace",
|
2024-03-12 14:12:34 +00:00
|
|
|
Args: root.NoArgs,
|
2023-07-27 10:03:08 +00:00
|
|
|
|
2024-02-14 18:04:45 +00:00
|
|
|
PreRunE: utils.ConfigureBundleWithVariables,
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2023-02-20 10:33:30 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-02-20 10:33:30 +00:00
|
|
|
b := bundle.Get(cmd.Context())
|
|
|
|
|
|
|
|
// Run initialize phase to make sure paths are set.
|
2023-05-24 12:45:19 +00:00
|
|
|
err := bundle.Apply(cmd.Context(), b, phases.Initialize())
|
2023-02-20 10:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
opts, err := f.syncOptionsFromBundle(cmd, b)
|
2023-02-20 10:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := cmd.Context()
|
|
|
|
s, err := sync.New(ctx, *opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Infof(ctx, "Remote file sync location: %v", opts.RemotePath)
|
2023-02-20 10:33:30 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
if f.watch {
|
2023-02-20 10:33:30 +00:00
|
|
|
return s.RunContinuous(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RunOnce(ctx)
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-02-20 10:33:30 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|