fix: AX audit round 3 — 8 violations resolved
- core.go: Result{Value: wrapped} → Result{wrapped, false} (explicit failure)
- error.go: fmt.Sprint → Sprint wrapper, removed fmt import
- fs.go: Stat/Open propagate validatePath failures (return vp)
- lock.go: Startables/Stoppables return Result
- task.go: PerformAsync returns Result
- runtime.go: updated to unwrap Result from Startables/Stoppables
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
8801e2ea10
commit
b2d0deb99b
9 changed files with 49 additions and 33 deletions
|
|
@ -65,7 +65,7 @@ func (c *Core) LogError(err error, op, msg string) Result {
|
|||
if wrapped == nil {
|
||||
return Result{OK: true}
|
||||
}
|
||||
return Result{Value: wrapped}
|
||||
return Result{wrapped, false}
|
||||
}
|
||||
|
||||
// LogWarn logs a warning and returns a Result with the wrapped error.
|
||||
|
|
@ -74,7 +74,7 @@ func (c *Core) LogWarn(err error, op, msg string) Result {
|
|||
if wrapped == nil {
|
||||
return Result{OK: true}
|
||||
}
|
||||
return Result{Value: wrapped}
|
||||
return Result{wrapped, false}
|
||||
}
|
||||
|
||||
// Must logs and panics if err is not nil.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ package core
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"iter"
|
||||
"maps"
|
||||
"os"
|
||||
|
|
@ -314,7 +313,7 @@ func (h *ErrorPanic) Recover() {
|
|||
|
||||
err, ok := r.(error)
|
||||
if !ok {
|
||||
err = NewError(fmt.Sprint("panic: ", r))
|
||||
err = NewError(Sprint("panic: ", r))
|
||||
}
|
||||
|
||||
report := CrashReport{
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ func (m *Fs) List(p string) Result {
|
|||
func (m *Fs) Stat(p string) Result {
|
||||
vp := m.validatePath(p)
|
||||
if !vp.OK {
|
||||
return Result{}
|
||||
return vp
|
||||
}
|
||||
return Result{}.Result(os.Stat(vp.Value.(string)))
|
||||
}
|
||||
|
|
@ -194,7 +194,7 @@ func (m *Fs) Stat(p string) Result {
|
|||
func (m *Fs) Open(p string) Result {
|
||||
vp := m.validatePath(p)
|
||||
if !vp.OK {
|
||||
return Result{}
|
||||
return vp
|
||||
}
|
||||
return Result{}.Result(os.Open(vp.Value.(string)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,9 +60,9 @@ func (c *Core) LockApply(name ...string) {
|
|||
}
|
||||
|
||||
// Startables returns services that have an OnStart function.
|
||||
func (c *Core) Startables() []*Service {
|
||||
func (c *Core) Startables() Result {
|
||||
if c.services == nil {
|
||||
return nil
|
||||
return Result{}
|
||||
}
|
||||
c.Lock("srv").Mu.RLock()
|
||||
defer c.Lock("srv").Mu.RUnlock()
|
||||
|
|
@ -72,13 +72,13 @@ func (c *Core) Startables() []*Service {
|
|||
out = append(out, svc)
|
||||
}
|
||||
}
|
||||
return out
|
||||
return Result{out, true}
|
||||
}
|
||||
|
||||
// Stoppables returns services that have an OnStop function.
|
||||
func (c *Core) Stoppables() []*Service {
|
||||
func (c *Core) Stoppables() Result {
|
||||
if c.services == nil {
|
||||
return nil
|
||||
return Result{}
|
||||
}
|
||||
c.Lock("srv").Mu.RLock()
|
||||
defer c.Lock("srv").Mu.RUnlock()
|
||||
|
|
@ -88,5 +88,5 @@ func (c *Core) Stoppables() []*Service {
|
|||
out = append(out, svc)
|
||||
}
|
||||
}
|
||||
return out
|
||||
return Result{out, true}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,13 +33,16 @@ func (r *ServiceRuntime[T]) Config() *Config { return r.core.Config() }
|
|||
|
||||
// ServiceStartup runs OnStart for all registered services that have one.
|
||||
func (c *Core) ServiceStartup(ctx context.Context, options any) Result {
|
||||
for _, s := range c.Startables() {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Result{err, false}
|
||||
}
|
||||
r := s.OnStart()
|
||||
if !r.OK {
|
||||
return r
|
||||
startables := c.Startables()
|
||||
if startables.OK {
|
||||
for _, s := range startables.Value.([]*Service) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Result{err, false}
|
||||
}
|
||||
r := s.OnStart()
|
||||
if !r.OK {
|
||||
return r
|
||||
}
|
||||
}
|
||||
}
|
||||
c.ACTION(ActionServiceStartup{})
|
||||
|
|
@ -50,11 +53,14 @@ func (c *Core) ServiceStartup(ctx context.Context, options any) Result {
|
|||
func (c *Core) ServiceShutdown(ctx context.Context) Result {
|
||||
c.shutdown.Store(true)
|
||||
c.ACTION(ActionServiceShutdown{})
|
||||
for _, s := range c.Stoppables() {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Result{err, false}
|
||||
stoppables := c.Stoppables()
|
||||
if stoppables.OK {
|
||||
for _, s := range stoppables.Value.([]*Service) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Result{err, false}
|
||||
}
|
||||
s.OnStop()
|
||||
}
|
||||
s.OnStop()
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ type TaskState struct {
|
|||
}
|
||||
|
||||
// PerformAsync dispatches a task in a background goroutine.
|
||||
func (c *Core) PerformAsync(t Task) string {
|
||||
func (c *Core) PerformAsync(t Task) Result {
|
||||
if c.shutdown.Load() {
|
||||
return ""
|
||||
return Result{}
|
||||
}
|
||||
taskID := Concat("task-", strconv.FormatUint(c.taskIDCounter.Add(1), 10))
|
||||
if tid, ok := t.(TaskWithID); ok {
|
||||
|
|
@ -40,7 +40,7 @@ func (c *Core) PerformAsync(t Task) string {
|
|||
}
|
||||
c.ACTION(ActionTaskCompleted{TaskID: taskID, Task: t, Result: r.Value, Error: err})
|
||||
})
|
||||
return taskID
|
||||
return Result{taskID, true}
|
||||
}
|
||||
|
||||
// Progress broadcasts a progress update for a background task.
|
||||
|
|
|
|||
|
|
@ -41,11 +41,15 @@ func TestLockEnable_Good(t *testing.T) {
|
|||
func TestStartables_Good(t *testing.T) {
|
||||
c := New()
|
||||
c.Service("s", Service{OnStart: func() Result { return Result{OK: true} }})
|
||||
assert.Len(t, c.Startables(), 1)
|
||||
r := c.Startables()
|
||||
assert.True(t, r.OK)
|
||||
assert.Len(t, r.Value.([]*Service), 1)
|
||||
}
|
||||
|
||||
func TestStoppables_Good(t *testing.T) {
|
||||
c := New()
|
||||
c.Service("s", Service{OnStop: func() Result { return Result{OK: true} }})
|
||||
assert.Len(t, c.Stoppables(), 1)
|
||||
r := c.Stoppables()
|
||||
assert.True(t, r.OK)
|
||||
assert.Len(t, r.Value.([]*Service), 1)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,12 +63,16 @@ func TestService_Lifecycle_Good(t *testing.T) {
|
|||
OnStop: func() Result { stopped = true; return Result{OK: true} },
|
||||
})
|
||||
|
||||
startables := c.Startables()
|
||||
sr := c.Startables()
|
||||
assert.True(t, sr.OK)
|
||||
startables := sr.Value.([]*Service)
|
||||
assert.Len(t, startables, 1)
|
||||
startables[0].OnStart()
|
||||
assert.True(t, started)
|
||||
|
||||
stoppables := c.Stoppables()
|
||||
tr := c.Stoppables()
|
||||
assert.True(t, tr.OK)
|
||||
stoppables := tr.Value.([]*Service)
|
||||
assert.Len(t, stoppables, 1)
|
||||
stoppables[0].OnStop()
|
||||
assert.True(t, stopped)
|
||||
|
|
|
|||
|
|
@ -20,10 +20,12 @@ func TestPerformAsync_Good(t *testing.T) {
|
|||
mu.Lock()
|
||||
result = "done"
|
||||
mu.Unlock()
|
||||
return Result{Value: "completed", OK: true}
|
||||
return Result{"completed", true}
|
||||
})
|
||||
|
||||
taskID := c.PerformAsync("work")
|
||||
r := c.PerformAsync("work")
|
||||
assert.True(t, r.OK)
|
||||
taskID := r.Value.(string)
|
||||
assert.NotEmpty(t, taskID)
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
|
@ -39,7 +41,8 @@ func TestPerformAsync_Progress_Good(t *testing.T) {
|
|||
return Result{OK: true}
|
||||
})
|
||||
|
||||
taskID := c.PerformAsync("work")
|
||||
r := c.PerformAsync("work")
|
||||
taskID := r.Value.(string)
|
||||
c.Progress(taskID, 0.5, "halfway", "work")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue