package main import ( "errors" "fmt" "io" "os" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/provider" ) // enrolReport tells the person how enrolment is going and keeps the same // lines in the install log (log; "" keeps none): one quiet line while it // waits for approval, each failure it waits out, and at the end what went // wrong in words. Details carry no secrets (runner.EnrolDetail). An // installer that introduces the link names its line in link, said just // before the link and only when one follows, and the line for a kept // enrolment in kept ("" says nothing). type enrolReport struct { errout io.Writer log string link, kept string } // macEnrolReport is the Mac installer's: "Open this link and add this // Mac to your team:" right before the link, "Already added to your // team." when the enrolment it has is kept. func macEnrolReport(errout io.Writer, log string) enrolReport { return enrolReport{errout: errout, log: log, link: "runner.mac.tap", kept: "runner.mac.already"} } func (r enrolReport) say(key string, a ...any) { line := fmt.Sprintf(equipo.RunnerSetupCopy(key), a...) fmt.Fprintln(r.errout, line) if r.log == "" { return } if f, err := os.OpenFile(r.log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600); err == nil { fmt.Fprintf(f, "%s %s\n", time.Now().UTC().Format(time.RFC3339), line) f.Close() } } func (r enrolReport) notify(n runner.EnrolNotice) { if n.Kept && r.kept != "" { r.say(r.kept) } if n.Link && r.link != "" { r.say(r.link) } if n.Waiting { r.say("runner.enrol.waiting") } if n.Retry != nil { r.say("runner.enrol.retrying", runner.EnrolDetail(n.Retry)) } } // failed says why enrolment ended; "Runner installation is unavailable." // never comes alone. func (r enrolReport) failed(err error) { switch { case errors.Is(err, runner.ErrEnrolmentExpired): r.say("runner.enrol.expired") case errors.Is(err, runner.ErrEnrolmentRefused): r.say("runner.enrol.refused") case errors.Is(err, provider.ErrIdentity): // A saved enrolment this command does not match, in words, not // as its code (provider_identity_mismatch, 2026-11-28). r.say("runner.enrol.identity") default: detail := runner.EnrolDetail(err) if detail == "" { detail = "unknown" } r.say("runner.enrol.failed", detail) r.say("runner.install.unavailable") } } // enrolledElsewhere is the line for a runner state the installer must // not touch: otherKey with the other Equipo's address when it is enrolled // with another Equipo than origin and instance, runner.enrol.identity // when the state does not say which (or says it in something other than // a plain web address, which is never printed), and runner.enrol.failed // when the state cannot be read, so whose it is is not known. "" when // the install can go ahead. func enrolledElsewhere(state, origin, instance, otherKey string) (string, []any) { other, elsewhere, err := runner.EnrolledElsewhere(state, origin, instance) switch { case err != nil: detail := runner.EnrolDetail(err) if detail == "" { detail = "unknown" } return "runner.enrol.failed", []any{detail} case !elsewhere: return "", nil case other == "" || !release.InstallOrigin(other): return "runner.enrol.identity", nil } return otherKey, []any{other} }