Some checks failed
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m15s
Test / Hakurei (push) Successful in 3m7s
Test / Hpkg (push) Failing after 4m0s
Test / Sandbox (race detector) (push) Successful in 4m14s
Test / Hakurei (race detector) (push) Successful in 4m51s
Test / Flake checks (push) Has been skipped
This is simultaneously more efficient and less error-prone. This change caused minor API changes in multiple other packages. Signed-off-by: Ophestra <cat@gensokyo.uk>
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"hakurei.app/hst"
|
|
"hakurei.app/internal/app/state"
|
|
"hakurei.app/internal/sys"
|
|
)
|
|
|
|
func New(ctx context.Context, os sys.State) (App, error) {
|
|
a := new(app)
|
|
a.sys = os
|
|
a.ctx = ctx
|
|
|
|
id := new(state.ID)
|
|
err := state.NewAppID(id)
|
|
a.id = newID(id)
|
|
|
|
return a, err
|
|
}
|
|
|
|
type app struct {
|
|
id *stringPair[state.ID]
|
|
sys sys.State
|
|
ctx context.Context
|
|
|
|
*outcome
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (a *app) ID() state.ID { a.mu.RLock(); defer a.mu.RUnlock(); return a.id.unwrap() }
|
|
|
|
func (a *app) String() string {
|
|
if a == nil {
|
|
return "(invalid app)"
|
|
}
|
|
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
|
|
if a.outcome != nil {
|
|
if a.outcome.user.uid == nil {
|
|
return fmt.Sprintf("(sealed app %s with invalid uid)", a.id)
|
|
}
|
|
return fmt.Sprintf("(sealed app %s as uid %s)", a.id, a.outcome.user.uid)
|
|
}
|
|
|
|
return fmt.Sprintf("(unsealed app %s)", a.id)
|
|
}
|
|
|
|
func (a *app) Seal(config *hst.Config) (SealedApp, error) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
|
|
if a.outcome != nil {
|
|
panic("app sealed twice")
|
|
}
|
|
|
|
seal := new(outcome)
|
|
seal.id = a.id
|
|
err := seal.finalise(a.ctx, a.sys, config)
|
|
if err == nil {
|
|
a.outcome = seal
|
|
}
|
|
return seal, err
|
|
}
|