2022-07-07 18:56:59 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/cmd/root"
|
|
|
|
"github.com/databricks/bricks/git"
|
|
|
|
"github.com/databricks/bricks/project"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// syncCmd represents the sync command
|
|
|
|
var syncCmd = &cobra.Command{
|
|
|
|
Use: "sync",
|
|
|
|
Short: "run syncs for the project",
|
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
|
|
|
|
|
|
|
if *remotePath == "" {
|
2022-11-23 12:44:10 +00:00
|
|
|
prjRemotePath, err := prj.RemoteRoot()
|
2022-09-14 15:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-23 12:44:10 +00:00
|
|
|
*remotePath = prjRemotePath
|
|
|
|
} else {
|
|
|
|
prj.OverrideRemoteRoot(*remotePath)
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Remote file sync location: %v", *remotePath)
|
|
|
|
repoExists, err := git.RepoExists(*remotePath, ctx, wsc)
|
2022-07-07 18:56:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-14 15:50:29 +00:00
|
|
|
if !repoExists {
|
|
|
|
return fmt.Errorf("repo not found, please ensure %s exists", *remotePath)
|
2022-07-07 18:56:59 +00:00
|
|
|
}
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2022-11-23 12:44:10 +00:00
|
|
|
root := prj.LocalRoot()
|
2022-09-16 13:18:46 +00:00
|
|
|
syncCallback := getRemoteSyncCallback(ctx, root, *remotePath, wsc)
|
2022-10-19 14:22:55 +00:00
|
|
|
err = spawnSyncRoutine(ctx, *interval, syncCallback, *remotePath)
|
2022-09-14 15:50:29 +00:00
|
|
|
return err
|
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
|
|
|
|
|
2022-09-19 14:47:55 +00:00
|
|
|
var persistSnapshot *bool
|
|
|
|
|
2022-07-07 18:56:59 +00:00
|
|
|
func init() {
|
|
|
|
root.RootCmd.AddCommand(syncCmd)
|
|
|
|
interval = syncCmd.Flags().Duration("interval", 1*time.Second, "project files polling interval")
|
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")
|
2022-09-19 14:47:55 +00:00
|
|
|
persistSnapshot = syncCmd.Flags().Bool("persist-snapshot", true, "whether to store local snapshots of sync state")
|
2022-07-07 18:56:59 +00:00
|
|
|
}
|