Skip to content

Commit

Permalink
fix(cli): synchronize deleted pro workspace with remote state
Browse files Browse the repository at this point in the history
by comparing local response vs remote instances

Cleans up orphaned local workspace files if necessary
  • Loading branch information
pascalbreuninger committed Jan 20, 2025
1 parent 0c6b3c3 commit 8209c3b
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions pkg/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
)

func List(ctx context.Context, devPodConfig *config.Config, skipPro bool, log log.Logger) ([]*providerpkg.Workspace, error) {
// Set indexed by UID for deduplication
workspaces := map[string]*providerpkg.Workspace{}

// list local workspaces
localWorkspaces, err := ListLocalWorkspaces(devPodConfig.DefaultContext, skipPro, log)
if err != nil {
Expand All @@ -37,7 +34,32 @@ func List(ctx context.Context, devPodConfig *config.Config, skipPro bool, log lo
if err != nil {
return nil, err
}
proWorkspacesByUID := map[string]*providerpkg.Workspace{}
for _, w := range proWorkspaces {
proWorkspacesByUID[w.UID] = w
}

// Check if every local file based workspace has a remote counterpart
// If not, mark `exists` as false and allow consumers of this function to take necessary measures
cleanedLocalWorkspaces := []*providerpkg.Workspace{}
for _, localWorkspace := range localWorkspaces {
if localWorkspace.IsPro() {
if _, ok := proWorkspacesByUID[localWorkspace.UID]; !ok {
err = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, localWorkspace.ID, "", log)
if err != nil {
log.Debugf("failed to delete local workspace %s: %v", localWorkspace.ID, err)
}
continue
}
}

cleanedLocalWorkspaces = append(cleanedLocalWorkspaces, localWorkspaces...)
}
localWorkspaces = cleanedLocalWorkspaces
}

// Set indexed by UID for deduplication
workspaces := map[string]*providerpkg.Workspace{}
// merge pro into local with pro taking precedence if UID matches
for _, workspace := range append(localWorkspaces, proWorkspaces...) {
workspaces[workspace.UID] = workspace
Expand Down

0 comments on commit 8209c3b

Please sign in to comment.