Skip to content

Commit 1b4efe0

Browse files
SniderVirgil
andcommitted
feat(v0.8.0): Result-native process service
- Register factory returns core.Result - OnStartup/OnShutdown return core.Result - Start/StartWithOptions/Run/RunWithOptions all return core.Result - 5 named Action handlers (process.run/start/kill/list/get) - core.ID() replaces fmt.Sprintf for process IDs - core.As replaces errors.As, core.Sprintf replaces fmt.Sprintf - handleRun returns output as Value (string) always, OK = exit 0 Co-Authored-By: Virgil <virgil@lethean.io>
1 parent 5d31665 commit 1b4efe0

7 files changed

Lines changed: 331 additions & 290 deletions

File tree

global_test.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
)
1212

1313
func TestGlobal_DefaultNotInitialized(t *testing.T) {
14-
// Reset global state for this test
1514
old := defaultService.Swap(nil)
1615
defer func() {
1716
if old != nil {
@@ -21,13 +20,13 @@ func TestGlobal_DefaultNotInitialized(t *testing.T) {
2120

2221
assert.Nil(t, Default())
2322

24-
_, err := Start(context.Background(), "echo", "test")
25-
assert.ErrorIs(t, err, ErrServiceNotInitialized)
23+
r := Start(context.Background(), "echo", "test")
24+
assert.False(t, r.OK)
2625

27-
_, err = Run(context.Background(), "echo", "test")
28-
assert.ErrorIs(t, err, ErrServiceNotInitialized)
26+
r = Run(context.Background(), "echo", "test")
27+
assert.False(t, r.OK)
2928

30-
_, err = Get("proc-1")
29+
_, err := Get("proc-1")
3130
assert.ErrorIs(t, err, ErrServiceNotInitialized)
3231

3332
assert.Nil(t, List())
@@ -36,11 +35,11 @@ func TestGlobal_DefaultNotInitialized(t *testing.T) {
3635
err = Kill("proc-1")
3736
assert.ErrorIs(t, err, ErrServiceNotInitialized)
3837

39-
_, err = StartWithOptions(context.Background(), RunOptions{Command: "echo"})
40-
assert.ErrorIs(t, err, ErrServiceNotInitialized)
38+
r = StartWithOptions(context.Background(), RunOptions{Command: "echo"})
39+
assert.False(t, r.OK)
4140

42-
_, err = RunWithOptions(context.Background(), RunOptions{Command: "echo"})
43-
assert.ErrorIs(t, err, ErrServiceNotInitialized)
41+
r = RunWithOptions(context.Background(), RunOptions{Command: "echo"})
42+
assert.False(t, r.OK)
4443
}
4544

4645
func newGlobalTestService(t *testing.T) *Service {
@@ -62,7 +61,6 @@ func TestGlobal_SetDefault(t *testing.T) {
6261
}()
6362

6463
svc := newGlobalTestService(t)
65-
6664
err := SetDefault(svc)
6765
require.NoError(t, err)
6866
assert.Equal(t, svc, Default())
@@ -83,7 +81,6 @@ func TestGlobal_ConcurrentDefault(t *testing.T) {
8381
}()
8482

8583
svc := newGlobalTestService(t)
86-
8784
err := SetDefault(svc)
8885
require.NoError(t, err)
8986

@@ -146,7 +143,6 @@ func TestGlobal_ConcurrentOperations(t *testing.T) {
146143
}()
147144

148145
svc := newGlobalTestService(t)
149-
150146
err := SetDefault(svc)
151147
require.NoError(t, err)
152148

@@ -158,10 +154,10 @@ func TestGlobal_ConcurrentOperations(t *testing.T) {
158154
wg.Add(1)
159155
go func() {
160156
defer wg.Done()
161-
proc, err := Start(context.Background(), "echo", "concurrent")
162-
if err == nil {
157+
r := Start(context.Background(), "echo", "concurrent")
158+
if r.OK {
163159
procMu.Lock()
164-
processes = append(processes, proc)
160+
processes = append(processes, r.Value.(*Process))
165161
procMu.Unlock()
166162
}
167163
}()
@@ -201,47 +197,44 @@ func TestGlobal_ConcurrentOperations(t *testing.T) {
201197

202198
func TestGlobal_StartWithOptions(t *testing.T) {
203199
svc, _ := newTestService(t)
204-
205200
old := defaultService.Swap(svc)
206201
defer func() {
207202
if old != nil {
208203
defaultService.Store(old)
209204
}
210205
}()
211206

212-
proc, err := StartWithOptions(context.Background(), RunOptions{
207+
r := StartWithOptions(context.Background(), RunOptions{
213208
Command: "echo",
214209
Args: []string{"with", "options"},
215210
})
216-
require.NoError(t, err)
211+
require.True(t, r.OK)
212+
proc := r.Value.(*Process)
217213

218214
<-proc.Done()
219-
220215
assert.Equal(t, 0, proc.ExitCode)
221216
assert.Contains(t, proc.Output(), "with options")
222217
}
223218

224219
func TestGlobal_RunWithOptions(t *testing.T) {
225220
svc, _ := newTestService(t)
226-
227221
old := defaultService.Swap(svc)
228222
defer func() {
229223
if old != nil {
230224
defaultService.Store(old)
231225
}
232226
}()
233227

234-
output, err := RunWithOptions(context.Background(), RunOptions{
228+
r := RunWithOptions(context.Background(), RunOptions{
235229
Command: "echo",
236230
Args: []string{"run", "options"},
237231
})
238-
require.NoError(t, err)
239-
assert.Contains(t, output, "run options")
232+
assert.True(t, r.OK)
233+
assert.Contains(t, r.Value.(string), "run options")
240234
}
241235

242236
func TestGlobal_Running(t *testing.T) {
243237
svc, _ := newTestService(t)
244-
245238
old := defaultService.Swap(svc)
246239
defer func() {
247240
if old != nil {
@@ -252,8 +245,9 @@ func TestGlobal_Running(t *testing.T) {
252245
ctx, cancel := context.WithCancel(context.Background())
253246
defer cancel()
254247

255-
proc, err := Start(ctx, "sleep", "60")
256-
require.NoError(t, err)
248+
r := Start(ctx, "sleep", "60")
249+
require.True(t, r.OK)
250+
proc := r.Value.(*Process)
257251

258252
running := Running()
259253
assert.Len(t, running, 1)

go.mod

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ module dappco.re/go/core/process
33
go 1.26.0
44

55
require (
6-
dappco.re/go/core v0.4.7
7-
dappco.re/go/core/io v0.1.7
8-
dappco.re/go/core/log v0.0.4
9-
dappco.re/go/core/ws v0.2.4
6+
dappco.re/go/core v0.5.0
7+
dappco.re/go/core/io v0.2.0
8+
dappco.re/go/core/log v0.1.0
9+
dappco.re/go/core/ws v0.3.0
1010
forge.lthn.ai/core/api v0.1.5
1111
github.com/gin-gonic/gin v1.12.0
1212
github.com/stretchr/testify v1.11.1
1313
)
1414

1515
require (
16+
dappco.re/go/core/api v0.2.0
17+
dappco.re/go/core/i18n v0.2.0
18+
dappco.re/go/core/process v0.3.0
19+
dappco.re/go/core/scm v0.4.0
20+
dappco.re/go/core/store v0.2.0
1621
forge.lthn.ai/core/go-io v0.1.5 // indirect
1722
forge.lthn.ai/core/go-log v0.0.4 // indirect
1823
github.com/99designs/gqlgen v0.17.88 // indirect
@@ -108,10 +113,3 @@ require (
108113
google.golang.org/protobuf v1.36.11 // indirect
109114
gopkg.in/yaml.v3 v3.0.1 // indirect
110115
)
111-
112-
replace (
113-
dappco.re/go/core => ../go
114-
dappco.re/go/core/io => ../go-io
115-
dappco.re/go/core/log => ../go-log
116-
dappco.re/go/core/ws => ../go-ws
117-
)

process_global.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func Init(c *core.Core) error {
5050
// --- Global convenience functions ---
5151

5252
// Start spawns a new process using the default service.
53-
func Start(ctx context.Context, command string, args ...string) (*Process, error) {
53+
func Start(ctx context.Context, command string, args ...string) core.Result {
5454
svc := Default()
5555
if svc == nil {
56-
return nil, ErrServiceNotInitialized
56+
return core.Result{OK: false}
5757
}
5858
return svc.Start(ctx, command, args...)
5959
}
6060

6161
// Run executes a command and waits for completion using the default service.
62-
func Run(ctx context.Context, command string, args ...string) (string, error) {
62+
func Run(ctx context.Context, command string, args ...string) core.Result {
6363
svc := Default()
6464
if svc == nil {
65-
return "", ErrServiceNotInitialized
65+
return core.Result{Value: "", OK: false}
6666
}
6767
return svc.Run(ctx, command, args...)
6868
}
@@ -95,19 +95,19 @@ func Kill(id string) error {
9595
}
9696

9797
// StartWithOptions spawns a process with full configuration using the default service.
98-
func StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error) {
98+
func StartWithOptions(ctx context.Context, opts RunOptions) core.Result {
9999
svc := Default()
100100
if svc == nil {
101-
return nil, ErrServiceNotInitialized
101+
return core.Result{OK: false}
102102
}
103103
return svc.StartWithOptions(ctx, opts)
104104
}
105105

106106
// RunWithOptions executes a command with options and waits using the default service.
107-
func RunWithOptions(ctx context.Context, opts RunOptions) (string, error) {
107+
func RunWithOptions(ctx context.Context, opts RunOptions) core.Result {
108108
svc := Default()
109109
if svc == nil {
110-
return "", ErrServiceNotInitialized
110+
return core.Result{Value: "", OK: false}
111111
}
112112
return svc.RunWithOptions(ctx, opts)
113113
}

0 commit comments

Comments
 (0)