2023-02-20 10:33:30 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle"
|
|
|
|
"github.com/databricks/bricks/bundle/phases"
|
2023-03-17 14:17:31 +00:00
|
|
|
"github.com/databricks/bricks/libs/log"
|
2023-02-20 10:33:30 +00:00
|
|
|
"github.com/databricks/bricks/libs/sync"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) (*sync.SyncOptions, error) {
|
|
|
|
cacheDir, err := b.CacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get bundle cache directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := sync.SyncOptions{
|
|
|
|
LocalPath: b.Config.Path,
|
2023-04-12 14:54:36 +00:00
|
|
|
RemotePath: b.Config.Workspace.FilesPath,
|
2023-02-20 10:33:30 +00:00
|
|
|
Full: full,
|
|
|
|
PollInterval: interval,
|
|
|
|
|
|
|
|
SnapshotBasePath: cacheDir,
|
|
|
|
WorkspaceClient: b.WorkspaceClient(),
|
|
|
|
}
|
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var syncCmd = &cobra.Command{
|
|
|
|
Use: "sync [flags]",
|
|
|
|
Short: "Synchronize bundle tree to the workspace",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
|
2023-05-15 09:34:05 +00:00
|
|
|
PreRunE: ConfigureBundleWithVariables,
|
2023-02-20 10:33:30 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
b := bundle.Get(cmd.Context())
|
|
|
|
|
|
|
|
// Run initialize phase to make sure paths are set.
|
|
|
|
err := bundle.Apply(cmd.Context(), b, []bundle.Mutator{
|
|
|
|
phases.Initialize(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := syncOptionsFromBundle(cmd, b)
|
|
|
|
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
|
|
|
|
|
|
|
if watch {
|
|
|
|
return s.RunContinuous(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RunOnce(ctx)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var interval time.Duration
|
|
|
|
var full bool
|
|
|
|
var watch bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
AddCommand(syncCmd)
|
|
|
|
syncCmd.Flags().DurationVar(&interval, "interval", 1*time.Second, "file system polling interval (for --watch)")
|
|
|
|
syncCmd.Flags().BoolVar(&full, "full", false, "perform full synchronization (default is incremental)")
|
|
|
|
syncCmd.Flags().BoolVar(&watch, "watch", false, "watch local file system for changes")
|
|
|
|
}
|