Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697package loginservice
import ( "context" "fmt" "os" "path/filepath" "runtime")
// The default home may be served by either the CLI registration or a stable// declarative registration. Custom homes keep their existing per-home names.func (s DaemonUnit) companion() (DaemonUnit, bool) { userHome, err := os.UserHomeDir() if err != nil || s.home != filepath.Join(userHome, ".albedo") { return DaemonUnit{}, false } owned, err := ForDaemon(s.home) if err != nil { return DaemonUnit{}, false } if s.declarative { return owned, true } declared := owned declared.declarative = true if runtime.GOOS == "darwin" { declared.name = "systems.albedo.daemon" declared.path = filepath.Join(filepath.Dir(owned.path), declared.name+".plist") } else { declared.name = "albedo.service" declared.path = filepath.Join(filepath.Dir(owned.path), declared.name) } return declared, true}
func hasRegistration(observation Observation) bool { if observation.File != AbsentFile { return true } if observation.Launchd != nil { return observation.Launchd.Loaded } state := observation.Properties["LoadState"] return state != "" && state != "not-found"}
// FindDaemon observes both names, including loaded registrations whose files// were removed. It never chooses between two registrations for the same home.func FindDaemon(ctx context.Context, home string) (DaemonUnit, Observation, error) { unit, err := ForDaemon(home) if err != nil { return unit, Observation{}, err } observation, err := unit.observe(ctx) if err != nil { return unit, observation, err } other, exists := unit.companion() if !exists { return unit, observation, nil } alternate, err := other.observe(ctx) if err != nil { return other, alternate, err } if !hasRegistration(alternate) { return unit, observation, nil } if hasRegistration(observation) { observation.Problem = fmt.Sprintf("both %s and %s are registered; reconcile them through their owners", unit.name, other.name) return unit, observation, nil } return other, alternate, nil}
// Observe keeps the selected name fixed during an operation and checks that a// competing registration has not appeared since selection.func (s DaemonUnit) Observe(ctx context.Context) (Observation, error) { observation, err := s.observe(ctx) if err != nil { return observation, err } other, exists := s.companion() if !exists { return observation, nil } alternate, err := other.observe(ctx) if err != nil { return observation, err } if hasRegistration(alternate) { observation.Problem = fmt.Sprintf("competing daemon registration %s; no mutation permitted", other.name) } return observation, nil}