From 6cc2571add76aa5f9024fa581e4db0d7fdd09c01 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 6 Jun 2023 00:07:21 +0200 Subject: [PATCH] address some comments --- cmd/workspace/workspace/import_dir.go | 4 +-- cmd/workspace/workspace/import_events.go | 45 ++++++++++++------------ libs/sync/event.go | 13 ------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/cmd/workspace/workspace/import_dir.go b/cmd/workspace/workspace/import_dir.go index c2c08bc39..5445cc393 100644 --- a/cmd/workspace/workspace/import_dir.go +++ b/cmd/workspace/workspace/import_dir.go @@ -4,7 +4,6 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/sync" - "github.com/databricks/databricks-sdk-go" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" ) @@ -21,6 +20,7 @@ var importDirCmd = &cobra.Command{ `, Annotations: map[string]string{ + // TODO: use render with template at individual call sites for these events. "template": cmdio.Heredoc(` {{if eq .Type "IMPORT_STARTED"}}Import started {{else if eq .Type "UPLOAD_COMPLETE"}}Uploaded {{.SourcePath}} -> {{.TargetPath}} @@ -41,7 +41,7 @@ var importDirCmd = &cobra.Command{ LocalPath: sourcePath, RemotePath: targetPath, Full: true, - WorkspaceClient: databricks.Must(databricks.NewWorkspaceClient()), + WorkspaceClient: root.WorkspaceClient(ctx), AllowOverwrites: importDirOverwrite, PersistSnapshot: false, diff --git a/cmd/workspace/workspace/import_events.go b/cmd/workspace/workspace/import_events.go index 74aebee22..4110fe07b 100644 --- a/cmd/workspace/workspace/import_events.go +++ b/cmd/workspace/workspace/import_events.go @@ -7,31 +7,38 @@ import ( "github.com/databricks/cli/libs/sync" ) +// TODO: do not emit target directory in upload complete events. + type fileIOEvent struct { SourcePath string `json:"source_path,omitempty"` TargetPath string `json:"target_path,omitempty"` Type string `json:"type"` } +const ( + EventTypeImportStarted = "IMPORT_STARTED" + EventTypeImportComplete = "IMPORT_COMPLETE" + EventTypeUploadComplete = "UPLOAD_COMPLETE" +) + func newImportStartedEvent(sourcePath, targetPath string) fileIOEvent { return fileIOEvent{ SourcePath: sourcePath, TargetPath: targetPath, - Type: "IMPORT_STARTED", + Type: EventTypeImportStarted, } } func newImportCompleteEvent(sourcePath, targetPath string) fileIOEvent { return fileIOEvent{ - Type: "IMPORT_COMPLETE", + Type: EventTypeImportComplete, } } -func newUploadCompleteEvent(sourcePath, targetPath string) fileIOEvent { +func newUploadCompleteEvent(sourcePath string) fileIOEvent { return fileIOEvent{ SourcePath: sourcePath, - TargetPath: targetPath, - Type: "UPLOAD_COMPLETE", + Type: EventTypeUploadComplete, } } @@ -44,25 +51,17 @@ func renderSyncEvents(ctx context.Context, eventChannel <-chan sync.Event, synce if !ok { return nil } - - // We parse progress events from the sync to track when file uploads - // are complete and emit the corresponding events - if e.String() != "" && e.Type() == sync.EventTypeProgress { - progressEvent := e.(*sync.EventSyncProgress) - if progressEvent.Progress < 1 { - return nil - } - // TODO: test this works with windows paths - // remotePath, err := syncer.RemotePath(progressEvent.Path) - // if err != nil { - // return err - // } - remotePath := "TODO" - err := cmdio.Render(ctx, newUploadCompleteEvent(progressEvent.Path, remotePath)) - if err != nil { - return err - } + if e.String() == "" { + return nil } + switch v := e.(type) { + case *sync.EventSyncProgress: + // TODO: only emit this event if the the sync event has progress 1.o0 + // File upload has been completed. This renders the event for that + // on the console + return cmdio.Render(ctx, newUploadCompleteEvent(v.Path)) + } + } } } diff --git a/libs/sync/event.go b/libs/sync/event.go index dcba22327..8e5c0efa2 100644 --- a/libs/sync/event.go +++ b/libs/sync/event.go @@ -24,7 +24,6 @@ const ( type Event interface { fmt.Stringer - Type() EventType } type EventBase struct { @@ -74,10 +73,6 @@ func (e *EventStart) String() string { return fmt.Sprintf("Action: %s", e.EventChanges.String()) } -func (e *EventStart) Type() EventType { - return EventTypeStart -} - func newEventStart(seq int, put []string, delete []string) Event { return &EventStart{ EventBase: newEventBase(seq, EventTypeStart), @@ -111,10 +106,6 @@ func (e *EventSyncProgress) String() string { } } -func (e *EventSyncProgress) Type() EventType { - return EventTypeProgress -} - func newEventProgress(seq int, action EventAction, path string, progress float32) Event { return &EventSyncProgress{ EventBase: newEventBase(seq, EventTypeProgress), @@ -142,10 +133,6 @@ func (e *EventSyncComplete) String() string { return "Complete" } -func (e *EventSyncComplete) Type() EventType { - return EventTypeComplete -} - func newEventComplete(seq int, put []string, delete []string) Event { return &EventSyncComplete{ EventBase: newEventBase(seq, EventTypeComplete),