Skip to content

Commit cb84afb

Browse files
authored
Merge pull request #141 from gruntwork-io/josh-padnick/m4-mutating-streaming-services
[Wails] M4: Forward PR #139 into feat/wails-rewrite
2 parents aa470b4 + cdf9831 commit cb84afb

8 files changed

Lines changed: 999 additions & 272 deletions

File tree

services/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewServices(initialPath string, emitter ports.Emitter) (*Services, error) {
6464
Boilerplate: &BoilerplateService{servers: servers},
6565
Tf: &TfService{servers: servers},
6666
GeneratedFiles: &GeneratedFilesService{servers: servers},
67-
Workspace: &WorkspaceService{},
67+
Workspace: &WorkspaceService{servers: servers},
6868
Exec: NewExecService(servers, emitter),
6969
Aws: NewAwsService(servers, adapters.NewSdkAwsClient()),
7070
GitHub: NewGitHubService(servers, adapters.NewHttpGitHubClient()),

services/workspace_service.go

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package services
22

33
import (
4+
"fmt"
5+
46
"github.com/gruntwork-io/runbooks/api"
57
)
68

7-
// WorkspaceService exposes the read-only workspace introspection
8-
// endpoints to the frontend over Wails IPC. These are used by the
9-
// Workspace, RepositoryFileBrowser, ChangedFilesView, DirPicker and
10-
// GitHubPullRequest components.
11-
//
12-
// Stateful actions (register/set-active worktree) stay on the HTTP
13-
// path through M3 because they mutate SessionManager; they'll move
14-
// over in M4 alongside the exec migration.
15-
type WorkspaceService struct{}
9+
// WorkspaceService exposes workspace introspection + worktree
10+
// registration to the frontend over Wails IPC. Read-only methods
11+
// (Tree, Dirs, File, Changes) are session-agnostic and take only a
12+
// path; mutating methods (Register, SetActive) require a session
13+
// token so they match the HTTP path's SessionAuthMiddleware
14+
// semantics.
15+
type WorkspaceService struct {
16+
servers *serverManager
17+
}
1618

1719
// ServiceName satisfies the optional application.ServiceName interface.
1820
func (s *WorkspaceService) ServiceName() string {
@@ -44,3 +46,51 @@ func (s *WorkspaceService) File(absPath string) (*api.WorkspaceFileResponse, err
4446
func (s *WorkspaceService) Changes(absPath, singleFile string) (*api.WorkspaceChangesResponse, error) {
4547
return api.GetWorkspaceChanges(absPath, singleFile)
4648
}
49+
50+
// Register adds a worktree path to the session's registered worktree
51+
// list. Matches POST /api/workspace/register — used by
52+
// GitWorkTreeContext when a clone completes or a worktree is discovered
53+
// on session bootstrap.
54+
func (s *WorkspaceService) Register(sessionID, path string) error {
55+
sessions, err := s.authed(sessionID)
56+
if err != nil {
57+
return err
58+
}
59+
if path == "" {
60+
return fmt.Errorf("path is required")
61+
}
62+
sessions.RegisterWorkTreePath(path)
63+
return nil
64+
}
65+
66+
// SetActive sets the explicitly selected active worktree path.
67+
// Matches POST /api/workspace/set-active — used when the user
68+
// switches worktrees in the UI so that target="worktree" templates
69+
// and REPO_FILES resolve against the selected repo.
70+
func (s *WorkspaceService) SetActive(sessionID, path string) error {
71+
sessions, err := s.authed(sessionID)
72+
if err != nil {
73+
return err
74+
}
75+
if path == "" {
76+
return fmt.Errorf("path is required")
77+
}
78+
sessions.SetActiveWorkTreePath(path)
79+
return nil
80+
}
81+
82+
// authed mirrors SessionService.authed: resolve the gruntbook's
83+
// SessionManager and verify the caller's token before any mutation.
84+
func (s *WorkspaceService) authed(sessionID string) (*api.SessionManager, error) {
85+
sessions := s.servers.Sessions()
86+
if sessions == nil {
87+
return nil, fmt.Errorf("no gruntbook is open")
88+
}
89+
if sessionID == "" {
90+
return nil, fmt.Errorf("missing session token")
91+
}
92+
if _, ok := sessions.ValidateToken(sessionID); !ok {
93+
return nil, fmt.Errorf("invalid or expired session token")
94+
}
95+
return sessions, nil
96+
}

web/src/bindings/github.com/gruntwork-io/runbooks/services/workspaceservice.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// This file is automatically generated. DO NOT EDIT
33

44
/**
5-
* WorkspaceService exposes the read-only workspace introspection
6-
* endpoints to the frontend over Wails IPC. These are used by the
7-
* Workspace, RepositoryFileBrowser, ChangedFilesView, DirPicker and
8-
* GitHubPullRequest components.
9-
*
10-
* Stateful actions (register/set-active worktree) stay on the HTTP
11-
* path through M3 because they mutate SessionManager; they'll move
12-
* over in M4 alongside the exec migration.
5+
* WorkspaceService exposes workspace introspection + worktree
6+
* registration to the frontend over Wails IPC. Read-only methods
7+
* (Tree, Dirs, File, Changes) are session-agnostic and take only a
8+
* path; mutating methods (Register, SetActive) require a session
9+
* token so they match the HTTP path's SessionAuthMiddleware
10+
* semantics.
1311
* @module
1412
*/
1513

@@ -52,6 +50,26 @@ export function File(absPath: string): $CancellablePromise<api$0.WorkspaceFileRe
5250
});
5351
}
5452

53+
/**
54+
* Register adds a worktree path to the session's registered worktree
55+
* list. Matches POST /api/workspace/register — used by
56+
* GitWorkTreeContext when a clone completes or a worktree is discovered
57+
* on session bootstrap.
58+
*/
59+
export function Register(sessionID: string, path: string): $CancellablePromise<void> {
60+
return $Call.ByID(1188286724, sessionID, path);
61+
}
62+
63+
/**
64+
* SetActive sets the explicitly selected active worktree path.
65+
* Matches POST /api/workspace/set-active — used when the user
66+
* switches worktrees in the UI so that target="worktree" templates
67+
* and REPO_FILES resolve against the selected repo.
68+
*/
69+
export function SetActive(sessionID: string, path: string): $CancellablePromise<void> {
70+
return $Call.ByID(1521614913, sessionID, path);
71+
}
72+
5573
/**
5674
* Tree returns the structure-only file tree (plus git metadata) for
5775
* an absolute workspace path. Callers must pass an absolute path —

0 commit comments

Comments
 (0)