-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
96 lines (86 loc) · 3.13 KB
/
process.go
File metadata and controls
96 lines (86 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: EUPL-1.2
// Process is the Core primitive for managed execution.
// Methods emit via named Actions — actual execution is handled by
// whichever service registers the "process.*" actions (typically go-process).
//
// If go-process is NOT registered, all methods return Result{OK: false}.
// This is permission-by-registration: no handler = no capability.
//
// Usage:
//
// r := c.Process().Run(ctx, "git", "log", "--oneline")
// if r.OK { output := r.Value.(string) }
//
// r := c.Process().RunIn(ctx, "/path/to/repo", "go", "test", "./...")
//
// Permission model:
//
// // Full Core — process registered:
// c := core.New(core.WithService(process.Register))
// c.Process().Run(ctx, "git", "log") // works
//
// // Sandboxed Core — no process:
// c := core.New()
// c.Process().Run(ctx, "git", "log") // Result{OK: false}
package core
import "context"
// Process is the Core primitive for process management.
// Zero dependencies — delegates to named Actions.
type Process struct {
core *Core
}
// Process returns the process management primitive.
//
// c.Process().Run(ctx, "git", "log")
func (c *Core) Process() *Process {
return &Process{core: c}
}
// Run executes a command synchronously and returns the output.
//
// r := c.Process().Run(ctx, "git", "log", "--oneline")
// if r.OK { output := r.Value.(string) }
func (p *Process) Run(ctx context.Context, command string, args ...string) Result {
return p.core.Action("process.run").Run(ctx, NewOptions(
Option{Key: "command", Value: command},
Option{Key: "args", Value: args},
))
}
// RunIn executes a command in a specific directory.
//
// r := c.Process().RunIn(ctx, "/repo", "go", "test", "./...")
func (p *Process) RunIn(ctx context.Context, dir string, command string, args ...string) Result {
return p.core.Action("process.run").Run(ctx, NewOptions(
Option{Key: "command", Value: command},
Option{Key: "args", Value: args},
Option{Key: "dir", Value: dir},
))
}
// RunWithEnv executes with additional environment variables.
//
// r := c.Process().RunWithEnv(ctx, dir, []string{"GOWORK=off"}, "go", "test")
func (p *Process) RunWithEnv(ctx context.Context, dir string, env []string, command string, args ...string) Result {
return p.core.Action("process.run").Run(ctx, NewOptions(
Option{Key: "command", Value: command},
Option{Key: "args", Value: args},
Option{Key: "dir", Value: dir},
Option{Key: "env", Value: env},
))
}
// Start spawns a detached/background process.
//
// r := c.Process().Start(ctx, ProcessStartOptions{Command: "docker", Args: []string{"run", "..."}})
func (p *Process) Start(ctx context.Context, opts Options) Result {
return p.core.Action("process.start").Run(ctx, opts)
}
// Kill terminates a managed process by ID or PID.
//
// c.Process().Kill(ctx, core.NewOptions(core.Option{Key: "id", Value: processID}))
func (p *Process) Kill(ctx context.Context, opts Options) Result {
return p.core.Action("process.kill").Run(ctx, opts)
}
// Exists returns true if any process execution capability is registered.
//
// if c.Process().Exists() { /* can run commands */ }
func (p *Process) Exists() bool {
return p.core.Action("process.run").Exists()
}