Repository navigation
Make sync command work in bundle context; reorder args #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1d2cfe
Make sync command work in bundle context; reorder args
pietern 024e7b3
Whitespace
pietern 40949cb
Use Var functions from pflag
pietern 2543caa
Temporarily split out bundle aware sync to separate command
pietern 7c0afe0
Remove superfluous args check
pietern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package bundle | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "github.com/databricks/bricks/bundle" | ||
| "github.com/databricks/bricks/bundle/phases" | ||
| "github.com/databricks/bricks/cmd/root" | ||
| "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, | ||
| RemotePath: b.Config.Workspace.FilePath.Workspace, | ||
| 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, | ||
|
|
||
| PreRunE: root.MustConfigureBundle, | ||
| 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 | ||
| } | ||
|
|
||
| log.Printf("[INFO] Remote file sync location: %v", opts.RemotePath) | ||
|
|
||
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package sync | ||
|
|
||
| import ( | ||
| "flag" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/bricks/bundle" | ||
| "github.com/databricks/bricks/bundle/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSyncOptionsFromBundle(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Path: tempDir, | ||
|
|
||
| Bundle: config.Bundle{ | ||
| Environment: "default", | ||
| }, | ||
|
|
||
| Workspace: config.Workspace{ | ||
| FilePath: config.PathLike{ | ||
| Workspace: "/Users/jane@doe.com/path", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| opts, err := syncOptionsFromBundle(syncCmd, []string{}, b) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tempDir, opts.LocalPath) | ||
| assert.Equal(t, "/Users/jane@doe.com/path", opts.RemotePath) | ||
| assert.Equal(t, filepath.Join(tempDir, ".databricks", "bundle", "default"), opts.SnapshotBasePath) | ||
| assert.NotNil(t, opts.WorkspaceClient) | ||
| } | ||
|
|
||
| func TestSyncOptionsFromArgsRequiredTwoArgs(t *testing.T) { | ||
| var err error | ||
| _, err = syncOptionsFromArgs(syncCmd, []string{}) | ||
| require.ErrorIs(t, err, flag.ErrHelp) | ||
| _, err = syncOptionsFromArgs(syncCmd, []string{"foo"}) | ||
| require.ErrorIs(t, err, flag.ErrHelp) | ||
| _, err = syncOptionsFromArgs(syncCmd, []string{"foo", "bar", "qux"}) | ||
| require.ErrorIs(t, err, flag.ErrHelp) | ||
| } | ||
|
|
||
| func TestSyncOptionsFromArgs(t *testing.T) { | ||
| opts, err := syncOptionsFromArgs(syncCmd, []string{"/local", "/remote"}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "/local", opts.LocalPath) | ||
| assert.Equal(t, "/remote", opts.RemotePath) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.