databricks-cli/cmd/sync/sync.go

83 lines
1.9 KiB
Go
Raw Normal View History

2022-07-07 18:56:59 +00:00
package sync
import (
"fmt"
"log"
"time"
"github.com/databricks/bricks/cmd/root"
"github.com/databricks/bricks/libs/git"
"github.com/databricks/bricks/libs/sync"
2022-07-07 18:56:59 +00:00
"github.com/databricks/bricks/project"
"github.com/spf13/cobra"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Synchronize a local directory to a workspace directory",
PreRunE: project.Configure,
2022-07-07 18:56:59 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
prj := project.Get(ctx)
wsc := prj.WorkspacesClient()
me, err := prj.Me()
if err != nil {
return err
}
if *remotePath == "" {
repositoryName, err := git.RepositoryName()
if err != nil {
return err
}
*remotePath = fmt.Sprintf("/Repos/%s/%s", me.UserName, repositoryName)
}
log.Printf("[INFO] Remote file sync location: %v", *remotePath)
cacheDir, err := prj.CacheDir()
if err != nil {
return err
}
opts := sync.SyncOptions{
LocalPath: prj.Root(),
RemotePath: *remotePath,
Full: *full,
SnapshotBasePath: cacheDir,
PollInterval: *interval,
WorkspaceClient: wsc,
}
s, err := sync.New(ctx, opts)
if err != nil {
return err
}
if *watch {
return s.RunContinuous(ctx)
}
return s.RunOnce(ctx)
2022-07-07 18:56:59 +00:00
},
}
// project files polling interval
var interval *time.Duration
var remotePath *string
var full *bool
var watch *bool
2022-07-07 18:56:59 +00:00
func init() {
root.RootCmd.AddCommand(syncCmd)
interval = syncCmd.Flags().Duration("interval", 1*time.Second, "file system polling interval (for --watch)")
remotePath = syncCmd.Flags().String("remote-path", "", "remote path to store repo in. eg: /Repos/me@example.com/test-repo")
full = syncCmd.Flags().Bool("full", false, "perform full synchronization (default is incremental)")
watch = syncCmd.Flags().Bool("watch", false, "watch local file system for changes")
2022-07-07 18:56:59 +00:00
}