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"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/cli/bundle/deploy/files"
|
2023-05-16 16:35:39 +00:00
|
|
|
"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) {
|
2024-03-18 14:41:58 +00:00
|
|
|
opts, err := files.GetSyncOptions(cmd.Context(), b)
|
2023-02-20 10:33:30 +00:00
|
|
|
if err != nil {
|
2024-03-18 14:41:58 +00:00
|
|
|
return nil, fmt.Errorf("cannot get sync options: %w", err)
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
opts.Full = f.full
|
|
|
|
opts.PollInterval = f.interval
|
|
|
|
return opts, nil
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-03-28 10:32:34 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
b, diags := utils.ConfigureBundleWithVariables(cmd)
|
|
|
|
if err := diags.Error(); err != nil {
|
|
|
|
return diags.Error()
|
|
|
|
}
|
2023-02-20 10:33:30 +00:00
|
|
|
|
|
|
|
// Run initialize phase to make sure paths are set.
|
2024-03-28 10:32:34 +00:00
|
|
|
diags = bundle.Apply(ctx, b, phases.Initialize())
|
2024-03-25 14:18:47 +00:00
|
|
|
if err := diags.Error(); err != nil {
|
2023-02-20 10:33:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|