2022-07-07 18:56:59 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/cmd/root"
|
2023-01-31 18:19:16 +00:00
|
|
|
"github.com/databricks/bricks/libs/git"
|
2023-01-23 12:52:39 +00:00
|
|
|
"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",
|
2023-01-24 14:06:59 +00:00
|
|
|
Short: "Synchronize a local directory to a workspace directory",
|
2022-09-16 09:06:58 +00:00
|
|
|
|
|
|
|
PreRunE: project.Configure,
|
2022-07-07 18:56:59 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := cmd.Context()
|
2022-09-16 13:18:46 +00:00
|
|
|
prj := project.Get(ctx)
|
|
|
|
wsc := prj.WorkspacesClient()
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-01-19 14:57:41 +00:00
|
|
|
me, err := prj.Me()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-14 15:50:29 +00:00
|
|
|
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)
|
|
|
|
|
2023-01-24 07:30:10 +00:00
|
|
|
cacheDir, err := prj.CacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := sync.SyncOptions{
|
|
|
|
LocalPath: prj.Root(),
|
|
|
|
RemotePath: *remotePath,
|
2023-01-24 14:06:59 +00:00
|
|
|
Full: *full,
|
2023-01-24 07:30:10 +00:00
|
|
|
SnapshotBasePath: cacheDir,
|
|
|
|
PollInterval: *interval,
|
|
|
|
WorkspaceClient: wsc,
|
|
|
|
}
|
|
|
|
|
2023-01-24 12:58:10 +00:00
|
|
|
s, err := sync.New(ctx, opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-23 12:52:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 14:06:59 +00:00
|
|
|
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
|
|
|
|
|
2022-09-14 15:50:29 +00:00
|
|
|
var remotePath *string
|
|
|
|
|
2023-01-24 14:06:59 +00:00
|
|
|
var full *bool
|
|
|
|
var watch *bool
|
2022-09-19 14:47:55 +00:00
|
|
|
|
2022-07-07 18:56:59 +00:00
|
|
|
func init() {
|
|
|
|
root.RootCmd.AddCommand(syncCmd)
|
2023-01-24 14:06:59 +00:00
|
|
|
interval = syncCmd.Flags().Duration("interval", 1*time.Second, "file system polling interval (for --watch)")
|
2022-09-14 15:50:29 +00:00
|
|
|
remotePath = syncCmd.Flags().String("remote-path", "", "remote path to store repo in. eg: /Repos/me@example.com/test-repo")
|
2023-01-24 14:06:59 +00:00
|
|
|
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
|
|
|
}
|