// Command alta-adopt is the existing-team adoption runner (Home // implementation/existing-team-adoption.md): a local operator command // that adopts the canonical Oficina apps into the Home directory team // labelled oficina. It is never built into or deployed with alta. // // Home's half runs in Alta's own engine, on Alta's one operator signer, // stream and journal; CARLOS's half is package adopt, over an Admin // credential that lives in this process's memory only. Without --apply, // every command shows what it would do and writes nothing anywhere: not // Home, not CARLOS, not the adoption journal. package main import ( "bufio" "context" "database/sql" "errors" "flag" "fmt" "io" "log/slog" "net/http" "os" "slices" "strconv" "strings" "time" "amadan.net/oficina/alta/adopt" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/console" "amadan.net/oficina/alta/homeop" "amadan.net/oficina/alta/provision" ) // version is stamped by make adopt. var version = "dev" // env is what a run reaches beyond its arguments. main fills the first // three; tests fill the rest, to reach fakes. type env struct { stdin io.Reader stdout, stderr io.Writer // now is the clock (Home's windows and plans are measured by it). now func() time.Time // public reaches the apps' own origins (the delivery probes). public *http.Client // homeURL is where each Home role is reached, when not its origin. homeURL map[string]string // tokenRead sees the buffer the CARLOS token was read into, so a // test can check it was zeroed. tokenRead func([]byte) } func main() { os.Exit(run(context.Background(), os.Args[1:], env{stdin: os.Stdin, stdout: os.Stdout, stderr: os.Stderr})) } // command is one subcommand: its flags, and whether it writes (and so // takes --apply). type command struct { writes bool flags func(fs *flag.FlagSet, o *opts) do func(r *runner, ctx context.Context) error } // opts are every command's flags. type opts struct { config string tokenFD int apply bool phase string app string apps string run int64 } var commands = map[string]command{ "home-preflight": {writes: true, flags: func(fs *flag.FlagSet, o *opts) { fs.StringVar(&o.phase, "phase", "", "") }, do: (*runner).homePreflight}, "preflight": {flags: func(fs *flag.FlagSet, o *opts) { fs.StringVar(&o.app, "app", "", "") }, do: (*runner).preflight}, "initialize": {writes: true, do: (*runner).initialize}, "plan-a": {writes: true, do: (*runner).planA}, "advance": {writes: true, flags: func(fs *flag.FlagSet, o *opts) { fs.Int64Var(&o.run, "run", 0, "") }, do: (*runner).advance}, "configure": {writes: true, flags: appFlag, do: (*runner).configure}, "activate": {writes: true, flags: func(fs *flag.FlagSet, o *opts) { fs.StringVar(&o.apps, "apps", "", "") }, do: (*runner).activate}, "rollback-config": {writes: true, flags: appFlag, do: (*runner).rollback}, "disable": {writes: true, flags: appFlag, do: (*runner).disable}, "status": {do: (*runner).status}, } func appFlag(fs *flag.FlagSet, o *opts) { fs.StringVar(&o.app, "app", "", "") } // run is the command. It answers the exit code: 0 done, 1 refused or // failed, 2 a usage error. func run(ctx context.Context, args []string, e env) int { if e.now == nil { e.now = time.Now } if len(args) == 0 || args[0] == "help" || args[0] == "-h" || args[0] == "--help" { fmt.Fprint(e.stdout, adopt.Usage) return 2 } if args[0] == "version" { fmt.Fprintln(e.stdout, version) return 0 } cmd, ok := commands[args[0]] if !ok { fmt.Fprint(e.stderr, adopt.Usage) return 2 } o := &opts{tokenFD: -1} fs := flag.NewFlagSet("alta-adopt "+args[0], flag.ContinueOnError) fs.SetOutput(io.Discard) fs.StringVar(&o.config, "config", "", "") fs.IntVar(&o.tokenFD, "carlos-token-fd", -1, "") if cmd.writes { fs.BoolVar(&o.apply, "apply", false, "") } if cmd.flags != nil { cmd.flags(fs, o) } if err := fs.Parse(args[1:]); err != nil || fs.NArg() > 0 { // The flag package's message can quote an argument: only the // usage is printed. fmt.Fprintln(e.stderr, "alta-adopt: "+args[0]+": "+adopt.RefuseFlag) fmt.Fprint(e.stderr, adopt.Usage) return 2 } r := &runner{e: e, o: o, in: bufio.NewReader(e.stdin)} defer r.close() if err := r.open(ctx); err != nil { fmt.Fprintln(e.stderr, "alta-adopt: "+err.Error()) return 1 } if err := cmd.do(r, ctx); err != nil { fmt.Fprintln(e.stderr, "alta-adopt: "+err.Error()) if h := hintFor(err); h != "" { fmt.Fprintln(e.stderr, h) } return 1 } return 0 } // refusal is a fixed code the command refuses with, and what comes first. type refusal struct{ code, hint string } func (r *refusal) Error() string { return "refused: " + r.code } func refuse(code, hint string) error { return &refusal{code, hint} } func hintFor(err error) string { var r *refusal if errors.As(err, &r) { return r.hint } return "" } // runner is one command's process: its config, stores and clients. The // CARLOS token is in memory only, and zeroed by close. type runner struct { e env o *opts in *bufio.Reader cfg *config db *sql.DB ps *provision.Store js *adopt.Store home *homeop.Client buf []byte // the whole buffer the token was read into token []byte // the token: a slice of buf, never a copy carlos *carlosop.Client public *http.Client } // open reads the token first, so its descriptor is closed whatever // follows, then the config, then the state. func (r *runner) open(ctx context.Context) error { if r.o.tokenFD != -1 { if err := r.readToken(r.o.tokenFD); err != nil { return err } } cfg, err := loadConfig(r.o.config) if err != nil { return err } r.cfg = cfg if r.o.apply { r.db, err = provision.OpenDB(cfg.state) } else { // A dry run (and every read-only command) works on a copy in // memory: the state file is never opened by SQLite, so nothing // in it moves, not its WAL mode, a pending migration or its // mtime. With no state yet, the copy is an empty database. r.db, err = snapshot(ctx, cfg.state) } if err != nil { return errors.New(adopt.RefuseStateOpen) } if r.ps, err = provision.NewStore(ctx, r.db, cfg.dek, r.e.now, adopt.Schema); err != nil { return err } if r.js, err = adopt.NewStore(r.ps); err != nil { return err } httpc := &http.Client{Timeout: 30 * time.Second} roles := map[string]homeop.Role{} for name, origin := range map[string]string{homeop.Credential: cfg.issuer, homeop.Directory: cfg.directory} { roles[name] = homeop.Role{Name: name, Origin: origin, URL: r.e.homeURL[name], Installation: cfg.installation, Registry: cfg.registry, Device: cfg.dev} } r.home = &homeop.Client{HTTP: httpc, Key: cfg.key, Prefilter: cfg.prefilter, Roles: roles, Now: r.e.now} r.public = r.e.public if r.public == nil { r.public = &http.Client{Timeout: 15 * time.Second} } if r.token != nil { r.carlos = carlosop.New(cfg.console, cfg.account, r.token) } return nil } // tokenLimit bounds the token: a CARLOS Admin token is under 100 bytes. const tokenLimit = 4096 // readToken reads the CARLOS token from descriptor fd into one buffer, // never copied: carlosop takes a slice of it, and close zeroes it all. // The standard streams are refused: the terminal answers prompts. func (r *runner) readToken(fd int) error { if fd < 3 { return errors.New(adopt.RefuseTokenFD) } f := os.NewFile(uintptr(fd), "carlos-token") if f == nil { return errors.New(adopt.RefuseTokenFDClosed) } defer f.Close() r.buf = make([]byte, tokenLimit) if r.e.tokenRead != nil { r.e.tokenRead(r.buf) } n := 0 for n < len(r.buf) { m, err := f.Read(r.buf[n:]) n += m if errors.Is(err, io.EOF) { break } if err != nil { return errors.New(adopt.RefuseTokenFDRead) } } if n == len(r.buf) { return errors.New(adopt.RefuseTokenLong) } r.token = trimSpace(r.buf[:n:n]) if len(r.token) == 0 { return errors.New(adopt.RefuseTokenEmpty) } return nil } // trimSpace is bytes.TrimSpace for ASCII space, as a subslice: the token // is never copied. func trimSpace(b []byte) []byte { for len(b) > 0 && strings.ContainsRune(" \t\r\n", rune(b[0])) { b = b[1:] } for len(b) > 0 && strings.ContainsRune(" \t\r\n", rune(b[len(b)-1])) { b = b[:len(b)-1] } return b } // close zeroes the token and closes everything open. func (r *runner) close() { if r.carlos != nil { r.carlos.Close() } clear(r.buf) if r.db != nil { r.db.Close() } } func (r *runner) say(lines ...string) { for _, l := range lines { fmt.Fprintln(r.e.stdout, l) } } // ask prints prompt and reads one line from the terminal, without its // line ending. func (r *runner) ask(prompt string) (string, error) { fmt.Fprint(r.e.stdout, prompt) line, err := r.in.ReadString('\n') if err != nil && (!errors.Is(err, io.EOF) || line == "") { return "", refuse("no-answer", "") } return strings.TrimRight(line, "\r\n"), nil } // needCarlos is the console client, or a refusal naming the flag. func (r *runner) needCarlos() (*carlosop.Client, error) { if r.carlos == nil { return nil, refuse("carlos-token-needed", adopt.HintToken) } return r.carlos, nil } // offline answers nothing: the console and the apps as Alta's engine // meets them in an adoption, which never touches either. type offline struct{} func (offline) RoundTrip(*http.Request) (*http.Response, error) { return nil, errors.New("alta-adopt: " + adopt.RefuseConsoleReached) } // engine is Alta's engine as alta-adopt runs it: Alta's one signer and // stream, no console credential and no network beyond Home, and obs as // the activation gate (nil for none). func (r *runner) engine(obs provision.Observer) *provision.Engine { none := &http.Client{Transport: offline{}} cfg := provision.Config{ Store: r.ps, Home: r.home, Console: &console.Client{HTTP: none, Base: "https://console.invalid", Account: r.cfg.account, Tokens: map[string]string{}}, Public: none, HomeCfg: provision.HomeConfig{Issuer: r.cfg.issuer, Directory: r.cfg.directory, Provisioner: r.cfg.provisioner}, Account: r.cfg.account, Now: r.e.now, Logger: slog.New(slog.NewTextHandler(r.e.stderr, nil)), } if obs != nil { cfg.Observer = obs } return provision.New(cfg) } // observer is the activation gate over the console client. func (r *runner) observer() (*adopt.Observer, error) { c, err := r.needCarlos() if err != nil { return nil, err } return &adopt.Observer{C: c, H: r.public, S: r.js, Artifacts: r.cfg.artifacts, Now: r.e.now}, nil } func (r *runner) app() (provision.App, error) { a, ok := provision.AppBySlug(r.o.app) if !ok { return provision.App{}, refuse("unknown-app", "") } return a, nil } // team is the adoption team's SQID, once seeded. func (r *runner) team(ctx context.Context) (string, error) { used, err := r.ps.LabelUsed(ctx, provision.AdoptionLabel) if err != nil { return "", err } if !used { return "", refuse("team-not-seeded", "") } t, err := r.ps.Team(ctx, provision.AdoptionLabel) if err != nil { return "", err } if t.TeamSQID == "" { return "", refuse("team-not-seeded", "") } return t.TeamSQID, nil } func (r *runner) needPreflight(ctx context.Context, phase, hint string) error { _, ok, err := r.js.HomePreflight(ctx, phase) if err != nil { return err } if !ok { return refuse("home-preflight-missing", hint) } return nil } // ---- the commands ---- // homePreflight prints Home's read-only queries, takes the counts Paul // pastes back, inspects both roles and checks it all against the phase. // Only with --apply, and only when nothing is refused, is it recorded. func (r *runner) homePreflight(ctx context.Context) error { phase := r.o.phase if phase != adopt.PhasePreInitialize && phase != adopt.PhasePrePlanA { return refuse("unknown-phase", "") } r.say(adopt.SayHomeQueries) counts := map[string]int64{} for _, q := range adopt.HomeQueries() { line, err := r.ask(adopt.SayHomeQuery(q)) if err != nil { return err } n, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64) if err != nil || n < 0 { return refuse("count-not-a-number", "") } counts[q[strings.LastIndex(q, " ")+1:]] = n } rec, codes, err := adopt.CheckHome(ctx, r.home, r.ps, counts, phase) if err != nil { return err } if len(codes) > 0 { r.say(adopt.SayHomeRefused(codes)) return refuse("home-preflight-refused", "") } r.say(adopt.SayHomeReady) if !r.o.apply { r.say(adopt.SayDryRun) return nil } if err := r.js.SaveHomePreflight(ctx, rec); err != nil { return err } r.say(adopt.SayDone) return nil } // preflight is CARLOS preflight for each app (or one), read-only. func (r *runner) preflight(ctx context.Context) error { c, err := r.needCarlos() if err != nil { return err } apps := provision.Apps if r.o.app != "" { a, err := r.app() if err != nil { return err } apps = []provision.App{a} } refused := false for _, a := range apps { var mine map[string]string if w, ok, err := r.js.Write(ctx, a.Slug); err != nil { return err } else if ok { mine = w.Sent } snap, codes, err := adopt.Preflight(ctx, c, r.public, a, mine) if err != nil { return err } if code := adopt.ReviewedRelease(snap.Delivery, r.cfg.artifacts[a.Slug]); code != "" { codes = append(codes, code) } refused = refused || len(codes) > 0 r.say(adopt.SayActivateApp(a.Slug, codes)) } if refused { return refuse("preflight-refused", "") } return nil } // initialize opens Home's directory initialize plan, once the // pre-initialize Home preflight is recorded. func (r *runner) initialize(ctx context.Context) error { if err := r.needPreflight(ctx, adopt.PhasePreInitialize, adopt.HintPreInitialize); err != nil { return err } if !r.o.apply { r.say(adopt.SayInitializeWould, provision.InitializeAttestation, adopt.SayDryRun) return nil } r.say(adopt.SayInitializeType, provision.InitializeAttestation) typed, err := r.ask("> ") if err != nil { return err } id, err := r.engine(nil).StartInitialize(ctx, typed) if err != nil { return err } r.say(adopt.SayConfirm(id, []string{homeop.Directory})) return nil } // planA opens Plan A, once the pre-plan-a Home preflight is recorded. A // Plan A whose window closed after the seed continues under add-apps // (ContinueAdoption); one whose plan lapsed before the seed is signed // again (Resubmit). func (r *runner) planA(ctx context.Context) error { if err := r.needPreflight(ctx, adopt.PhasePrePlanA, adopt.HintPrePlanA); err != nil { return err } used, err := r.ps.LabelUsed(ctx, provision.AdoptionLabel) if err != nil { return err } both := []string{homeop.Directory, homeop.Credential} e := r.engine(nil) if !used { if !r.o.apply { r.say(adopt.SayPlanAWould, adopt.SayDryRun) return nil } owner, err := r.ask(adopt.SayPlanAOwner + " ") if err != nil { return err } id, err := e.StartAdoption(ctx, "Oficina", strings.TrimSpace(owner)) if err != nil { return err } r.say(adopt.SayConfirm(id, both)) return nil } t, err := r.ps.Team(ctx, provision.AdoptionLabel) if err != nil { return err } if t.TeamSQID != "" { var missing []string for _, a := range provision.Slugs() { if _, ok, err := r.ps.Done(ctx, provision.AdoptionLabel, a, provision.StepRegister); err != nil { return err } else if !ok { missing = append(missing, a) } } if len(missing) == 0 { return refuse("plan-a-complete", "") } r.say(adopt.SayPlanAContinue(missing)) if !r.o.apply { r.say(adopt.SayDryRun) return nil } id, err := e.ContinueAdoption(ctx) if err != nil { return err } r.say(adopt.SayConfirm(id, both)) return nil } runs, err := r.ps.Runs(ctx, provision.AdoptionLabel) if err != nil { return err } for _, run := range runs { if run.Adoption && run.Kind == homeop.KindCreateTeam { r.say(adopt.SayPlanAResubmit) if !r.o.apply { r.say(adopt.SayDryRun) return nil } if err := e.Resubmit(ctx, run.ID); err != nil { return err } r.say(adopt.SayConfirm(run.ID, both)) return nil } } return refuse("no-plan-a", "") } // advancePasses bounds one advance: each pass goes as far as Home allows // now, so a run that stops moving is left for the next advance. const advancePasses = 10 // advance takes an initialize or adoption run as far as it can go now. // Plans B and C need the console: their activation re-observes each app. func (r *runner) advance(ctx context.Context) error { run, err := r.ps.Run(ctx, r.o.run) if err != nil { return refuse("no-such-run", "") } if !run.Adoption && run.Kind != homeop.KindInitialize { return refuse("not-an-adoption-run", "") } var obs provision.Observer if run.Adoption && run.Kind == homeop.KindAddApps { o, err := r.observer() if err != nil { return err } obs = o } r.say(adopt.SayRun(run.ID, run.State, run.Code)) if !r.o.apply { r.say(adopt.SayAdvanceWould, adopt.SayDryRun) return nil } e := r.engine(obs) for range advancePasses { if err := e.Advance(ctx, run.ID); err != nil { var fl *provision.Fail if !errors.As(err, &fl) { return err } } next, err := r.ps.Run(ctx, run.ID) if err != nil { return err } moved := next.State != run.State || next.Code != run.Code run = next if run.State == "done" || run.State == "failed" || !moved { break } } r.say(adopt.SayRun(run.ID, run.State, run.Code)) if run.State == "failed" { return refuse(run.Code, "") } return nil } // configure writes app's two agent keys, restarts its canonical host // until it serves them, captures the after snapshot (Task 9's order) and // records the delivery check (Paul, 2026-11-17: the runner's check in // place of his witnessed sign-in). A resume continues from the journal: // an app already captured is not restarted again, and its delivery check // is made again, so a lost or voided check is redone by running // configure again. func (r *runner) configure(ctx context.Context) error { a, err := r.app() if err != nil { return err } c, err := r.needCarlos() if err != nil { return err } sqid, err := r.team(ctx) if err != nil { return err } summary, err := adopt.WriteAgentKeys(ctx, c, r.public, r.js, a, sqid, r.cfg.artifacts[a.Slug], r.o.apply) if err != nil { return err } r.say(summary) host := adopt.CanonicalHost(a) if !r.o.apply { // The restart's dry run: the scoped door must resolve the // canonical host alone. id, err := r.identity(ctx, c, a) if err != nil { return err } if _, err := adopt.RestartAndWait(ctx, c, r.public, a, id, func(adopt.Delivery) error { return nil }, false); err != nil { return err } r.say(adopt.SayConfigureWould(host), adopt.SayDryRun) return nil } if _, ok, err := r.js.After(ctx, a.Slug); err != nil { return err } else if !ok { base, ok, err := r.js.Baseline(ctx, a.Slug) if err != nil { return err } if !ok { return errors.New("adopt: " + adopt.RefuseNoJournalBase) } until := func(d adopt.Delivery) error { return adopt.Delivered(a, base.Delivery, d) } if _, err := adopt.RestartAndWait(ctx, c, r.public, a, base.Identity, until, true); err != nil { return err } if _, err := adopt.CaptureAfter(ctx, c, r.public, r.js, a); err != nil { return err } } if err := adopt.CheckDelivery(ctx, r.js, c, r.public, a, r.cfg.artifacts[a.Slug]); err != nil { return err } r.say(adopt.SayConfigured(host)) return nil } // identity is the canonical record's identity: the baseline's once there // is one, otherwise a fresh read's. func (r *runner) identity(ctx context.Context, c *carlosop.Client, a provision.App) (adopt.Identity, error) { if base, ok, err := r.js.Baseline(ctx, a.Slug); err != nil || ok { return base.Identity, err } snap, err := adopt.Observe(ctx, c, r.public, a) return snap.Identity, err } // activate opens Plan B or C. Every app but the canary must pass Plan // C's gate, the canary activated by Plan B (Store.CanaryGate, the // Observer's own rule), and every app must pass the Observer now, before // a window is asked for; the engine observes again, at activation. func (r *runner) activate(ctx context.Context) error { var apps []string for _, a := range strings.Split(r.o.apps, ",") { if a = strings.TrimSpace(a); a != "" { apps = append(apps, a) } } sorted := provision.SortApps(apps) if len(apps) == 0 || len(sorted) != len(apps) { return refuse("unknown-app", "") } for _, a := range sorted { if err := r.js.CanaryGate(ctx, a); errors.Is(err, adopt.ErrCanaryNotActive) { return refuse(err.Error(), adopt.HintCanary) } else if err != nil { return err } } obs, err := r.observer() if err != nil { return err } sqid, err := r.team(ctx) if err != nil { return err } refused := false for _, a := range sorted { var codes []string if _, err := obs.Observe(ctx, a, sqid); err != nil { var rf *adopt.Refusal if !errors.As(err, &rf) { return err } codes = rf.Codes } refused = refused || len(codes) > 0 r.say(adopt.SayActivateApp(a, codes)) } if refused { return refuse("activate-refused", "") } if !r.o.apply { r.say(adopt.SayActivateWould(sorted), adopt.SayDryRun) return nil } id, err := r.engine(obs).ActivateAdopted(ctx, sorted) if err != nil { return err } r.say(adopt.SayConfirm(id, []string{homeop.Directory, homeop.Credential})) return nil } // rollback removes the two agent keys configure wrote and restarts the // app on its baseline. func (r *runner) rollback(ctx context.Context) error { a, err := r.app() if err != nil { return err } c, err := r.needCarlos() if err != nil { return err } if err := adopt.RollbackAgentKeys(ctx, c, r.public, r.js, a, r.o.apply); err != nil { return err } if !r.o.apply { preview, err := r.rollbackPreview(ctx, c, a) if err != nil { return err } r.say(preview, adopt.SayDryRun) return nil } r.say(adopt.SayRolledBack(adopt.CanonicalHost(a))) return nil } // rollbackPreview says what a rollback would do now, from the journal // and a fresh read of the write layer, classified as RollbackAgentKeys // classifies it. It writes nothing. RollbackAgentKeys's own dry run has // already refused every stop. func (r *runner) rollbackPreview(ctx context.Context, c *carlosop.Client, a provision.App) (string, error) { keys, host := adopt.AgentKeys(a), adopt.CanonicalHost(a) rb, have, err := r.js.Rollback(ctx, a.Slug) if err != nil { return "", err } switch { case have && rb.State == "restarted": return adopt.SayRollbackNothing(host), nil case have && rb.State == "unset": return adopt.SayRollbackRestartOnly(keys, host), nil } base, okBase, err := r.js.Baseline(ctx, a.Slug) if err != nil { return "", err } w, okWrite, err := r.js.Write(ctx, a.Slug) if err != nil { return "", err } if !okBase || !okWrite { return "", refuse("rollback-nothing-written", "") } read, err := c.Config(ctx, a.Slug, base.WriteLayer) if err != nil { return "", err } switch adopt.ClassifyRollback(base.Layer, read, w.Sent) { case adopt.RetryUnset: return adopt.SayRollbackWould(keys, base.Layer.Bundle, host), nil case adopt.RolledBack: return adopt.SayRollbackRestartOnly(keys, host), nil } return "", refuse("rollback-refused", "") } // disable opens a directory-only plan disabling app's team binding. The // label is typed on the terminal. func (r *runner) disable(ctx context.Context) error { a, err := r.app() if err != nil { return err } if _, err := r.team(ctx); err != nil { return err } if !r.o.apply { r.say(adopt.SayDisableWould(a.Slug), adopt.SayDisableAsk, adopt.SayDryRun) return nil } typed, err := r.ask(adopt.SayDisableType(a.Slug) + " ") if err != nil { return err } id, err := r.engine(nil).DisableAdopted(ctx, a.Slug, strings.TrimSpace(typed)) if err != nil { return err } r.say(adopt.SayConfirm(id, []string{homeop.Directory})) return nil } // status shows where the adoption is: Home's preflights, the team, the // runs, and each app's Home steps and journal. func (r *runner) status(ctx context.Context) error { for _, phase := range []string{adopt.PhasePreInitialize, adopt.PhasePrePlanA} { _, ok, err := r.js.HomePreflight(ctx, phase) if err != nil { return err } r.say(adopt.SayStatusPreflight(phase, ok)) } _, err := r.team(ctx) var rf *refusal if err != nil && !errors.As(err, &rf) { return err } r.say(adopt.SayStatusTeam(err == nil)) var runs []provision.Run for _, label := range []string{"", provision.AdoptionLabel} { rs, err := r.ps.Runs(ctx, label) if err != nil { return err } runs = append(runs, rs...) } slices.SortFunc(runs, func(a, b provision.Run) int { return int(a.ID - b.ID) }) for _, run := range runs { r.say(adopt.SayRun(run.ID, run.Kind+" "+run.State, run.Code)) } steps, err := r.ps.Steps(ctx, provision.AdoptionLabel) if err != nil { return err } for _, a := range provision.Slugs() { var parts []string for _, st := range steps { if st.App == a { parts = append(parts, strings.TrimSpace(st.Name+" "+st.State+" "+st.Code)) } } journal, err := r.journal(ctx, a) if err != nil { return err } parts = append(parts, journal...) if len(parts) == 0 { parts = []string{adopt.SayStatusNothing} } r.say(adopt.SayStatusApp(a, parts)) } return nil } // journal is app's adoption journal as states and codes. func (r *runner) journal(ctx context.Context, app string) ([]string, error) { var out []string if _, ok, err := r.js.Baseline(ctx, app); err != nil { return nil, err } else if ok { out = append(out, "baseline") } if w, ok, err := r.js.Write(ctx, app); err != nil { return nil, err } else if ok { out = append(out, strings.TrimSpace("write "+w.State+" "+w.Code)) } if _, ok, err := r.js.After(ctx, app); err != nil { return nil, err } else if ok { out = append(out, "after") } if _, ok, err := r.js.DeliveryCheck(ctx, app); err != nil { return nil, err } else if ok { out = append(out, adopt.SayStatusDeliveryCheck) } if rb, ok, err := r.js.Rollback(ctx, app); err != nil { return nil, err } else if ok { out = append(out, strings.TrimSpace("rollback "+rb.State+" "+rb.Code)) } return out, nil }