Files
archie/internal/app/archied/setup.go
T
sam ef0d61782b feat(configuration): add archied setup, the caller the first-run flow was built for
docs/architecture/configuration.md decides that "install.sh must stop generating
config.toml; archied setup owns it", and gives the reason: the installer
hand-rolling TOML already drifted from the schema once, and "the durable fix is
that the code writing the config is the code that reads it". setup.Run,
tomlwrite, configtemplate.Example and terminalprompt were all built for that and
had no production caller between them. This adds the caller.

setup.Run's own doc comment states the obligations, and they are the shape of
installValidatedConfig: render the edits, prove the result loads through a real
configuration.Loader, and only then install the file and commit the secrets. A
validation failure returns before either write, so a bad config leaves neither a
config nor a secret on disk. The rendered bytes are proven from a temp file, so
a load failure cannot have already installed anything. The config is written
0600 via same-directory temp and rename.

Parameterisation is typed, not matched against prompt text. A flag-to-prompt
mapping would have to match strings, which is the brittleness removed in
1f3f54b, so setup.Params is consulted before each question instead and the
prompts are only reached for answers nothing supplied. Run stays a wrapper
passing the zero value, so the package's existing tests keep passing untouched.
No environment variable is consulted anywhere: flags and prompts are the only
two ways an answer arrives.

--defaults is a known-valid unattended baseline, not "each prompt's own default".
That cannot work, and the test proves it: the provider step's first option is
OpenAI, whose model prompt hard-errors on an empty answer. The baseline therefore
covers every required answer and leaves optional ones to answer themselves as
"skip" -- ollama is the only keyless provider, so an unattended install boots
without needing a secret.

Two gaps closed after review rather than left as accepted work:

- A cloud provider was not configurable without a terminal, because the provider
  API key had no typed parameter: unattended setup could only ever pick the
  keyless self-hosted provider. Params.ProviderAPIKey and -provider-api-key fix
  that. The key goes to the env file and TOML carries only the secret reference.
- The write order was unpinned. Inverting installValidatedConfig's two writes --
  committing secrets before the config -- left every test green, so nothing held
  the invariant the SecretSink contract states. The new
  TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret fails on that
  inversion; the existing validation-failure test does not cover it.

Also fixed three lint failures in the first pass (gocyclo on RunSetup, and two
ineffectual `choice` initialisers): RunSetup's flag surface, the flag-to-Params
mapping, the prompter choice and the existing-config read are now separate
functions, which is the real fix for the complexity rather than a suppression.

The setup flow no longer hand-rolls its own answer format: -telegram-user-ids
reuses setup.ParseTelegramUserIDs, the same parser the prompt path uses, so the
allowlist cannot be parsed two ways.

Found by an adversarial package sweep (temp tracker: config-1,
repo-root-deployment-2, .local/issue-tracker/). Plan of record:
.local/issue-tracker/design-archied-setup.md. install.sh still generates its own
config; that is phase 2 and is not claimed here.
2026-09-15 02:52:24 +10:00

300 lines
10 KiB
Go

package archied
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"syscall"
configtemplate "github.com/samcharles93/archie-core"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
"github.com/samcharles93/archie-core/internal/infrastructure/terminalprompt"
)
// setupCommand is the argv[1] token that switches archied out of daemon
// mode and into the first-run configuration generator.
const setupCommand = "setup"
// defaultsParams is the known-valid unattended baseline --defaults supplies.
// Answering every prompt with its own default does NOT produce a loadable
// config (the provider step's first option is OpenAI, whose model prompt
// hard-errors on an empty answer), so this covers every required answer and
// leaves the optional ones to answer themselves as "skip": ollama is the
// only keyless provider, so an unattended install boots without a secret.
func defaultsParams() setup.Params {
return setup.Params{
BotUser: "archie-bot",
ForgeType: "github",
ForgeHost: "https://github.com",
Provider: "ollama",
Model: "llama3",
}
}
// IsSetupArgs reports whether args (os.Args[1:]) selects setup rather than
// the daemon.
func IsSetupArgs(args []string) bool {
return len(args) > 0 && args[0] == setupCommand
}
// setupFlags is the parameterised answer surface, kept apart from RunSetup so
// the flag definitions, the mapping into setup.Params and the flow stay
// independently readable. Nothing here reads a terminal or the environment:
// parameters and prompts are the only two ways an answer arrives, and no
// environment variable is consulted.
type setupFlags struct {
cfgPath string
defaults bool
botUser string
operator string
forgeType string
forgeHost string
forgeToken string
provider string
model string
providerAPIKey string
telegramToken string
telegramUserIDs string
}
func bindSetupFlags(fs *flag.FlagSet) *setupFlags {
f := &setupFlags{}
fs.StringVar(&f.cfgPath, "config", filepath.Join(configHome(), "archie", "config.toml"), "path to write config.toml")
fs.BoolVar(&f.defaults, "defaults", false, "write a known-valid unattended baseline config without prompting")
fs.StringVar(&f.botUser, "bot-user", "", "forge username for archied's commits and API calls")
fs.StringVar(&f.operator, "operator", "", "operator display name (optional)")
fs.StringVar(&f.forgeType, "forge-type", "", "forge type: github | gitea | none")
fs.StringVar(&f.forgeHost, "forge-host", "", "forge base URL")
fs.StringVar(&f.forgeToken, "forge-token", "", "forge API token, stored in the env file beside the config")
fs.StringVar(&f.provider, "provider", "", "LLM provider class: openai | anthropic | openrouter | gemini | groq | deepseek | mistral | ollama")
fs.StringVar(&f.model, "model", "", "bare model name; the provider class prefix is added automatically")
fs.StringVar(&f.providerAPIKey, "provider-api-key", "", "cloud LLM provider API key, stored in the env file beside the config")
fs.StringVar(&f.telegramToken, "telegram-token", "", "Telegram bot token, stored in the env file beside the config")
fs.StringVar(&f.telegramUserIDs, "telegram-user-ids", "", "comma-separated allowed Telegram user IDs; configuring Telegram requires at least one")
return f
}
// params folds the flags over the baseline. A flag always wins; without
// -defaults the baseline is empty, so only the flags pre-answer anything and
// every other question is prompted for.
func (f *setupFlags) params() (setup.Params, error) {
var params setup.Params
if f.defaults {
params = defaultsParams()
}
if f.botUser != "" {
params.BotUser = f.botUser
}
if f.operator != "" {
params.Operator = f.operator
}
if f.forgeType != "" {
params.ForgeType = f.forgeType
}
if f.forgeHost != "" {
params.ForgeHost = f.forgeHost
}
if f.forgeToken != "" {
params.ForgeToken = f.forgeToken
}
if f.provider != "" {
params.Provider = f.provider
}
if f.model != "" {
params.Model = f.model
}
if f.providerAPIKey != "" {
params.ProviderAPIKey = f.providerAPIKey
}
if f.telegramToken != "" {
params.TelegramToken = f.telegramToken
}
if f.telegramUserIDs != "" {
ids, err := setup.ParseTelegramUserIDs(f.telegramUserIDs)
if err != nil {
return setup.Params{}, fmt.Errorf("-telegram-user-ids: %w", err)
}
params.TelegramUserIDs = ids
}
return params, nil
}
// setupPrompter picks the answer surface. -defaults answers every remaining
// question with its own default and never touches a terminal; otherwise stdin
// must be a real terminal, and terminalprompt refuses it otherwise rather than
// silently generating a config nobody chose.
func setupPrompter(defaults bool, stdin *os.File, stdout io.Writer) (setup.Prompter, error) {
if defaults {
return setup.DefaultPrompter{}, nil
}
return terminalprompt.New(stdin, stdout)
}
// existingConfig reads the config being replaced, returning the bytes to apply
// edits to and the values worth prefilling prompts with. A missing file is a
// fresh install, not an error. A file that exists but does not load is still
// edited in place: tomlwrite only rewrites the lines it owns, and refusing
// would strand an operator whose config is malformed.
func existingConfig(loader *configuration.Loader, path string) ([]byte, setup.ExistingValues, error) {
base, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return nil, setup.ExistingValues{}, nil
}
if err != nil {
return nil, setup.ExistingValues{}, fmt.Errorf("read existing config: %w", err)
}
var existing setup.ExistingValues
if doc, err := loader.File(path); err == nil {
existing = setup.ExistingValues{
BotUser: doc.Config.BotUser,
Operator: doc.Config.Chat.Operator,
ForgeHost: doc.Config.Forge.Host,
}
}
return base, existing, nil
}
// RunSetup generates (or re-generates) a config.toml, validates it through a
// real configuration.Loader, and only then writes it atomically with mode
// 0600 and commits any secrets to the env file beside it. --defaults uses a
// known-valid unattended baseline and never touches a terminal; without it,
// stdin must be a real TTY and the guided flow runs.
func RunSetup(args []string, stdin *os.File, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet(setupCommand, flag.ContinueOnError)
fs.SetOutput(stderr)
flags := bindSetupFlags(fs)
if err := fs.Parse(args); err != nil {
return 2
}
if fs.NArg() > 0 {
fmt.Fprintf(stderr, "setup: unexpected argument %q\n", fs.Arg(0))
return 2
}
params, err := flags.params()
if err != nil {
fmt.Fprintf(stderr, "setup: %v\n", err)
return 2
}
p, err := setupPrompter(flags.defaults, stdin, stdout)
if err != nil {
fmt.Fprintf(stderr, "setup: %v\n", err)
return 1
}
cfgDir := filepath.Dir(flags.cfgPath)
if err := os.MkdirAll(cfgDir, 0o700); err != nil {
fmt.Fprintf(stderr, "setup: create config directory: %v\n", err)
return 1
}
sink := setup.NewEnvFileSink(filepath.Join(cfgDir, "env"))
loader := configuration.New(nil)
base, existing, err := existingConfig(loader, flags.cfgPath)
if err != nil {
fmt.Fprintf(stderr, "setup: %v\n", err)
return 1
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
edits, err := setup.RunParams(ctx, p, nil, sink, existing, params)
if err != nil {
fmt.Fprintf(stderr, "setup: %v\n", err)
return 1
}
var rendered []byte
if base != nil {
rendered, err = tomlwrite.Apply(base, edits)
} else {
rendered, err = tomlwrite.Generate(configtemplate.Example, edits)
}
if err != nil {
fmt.Fprintf(stderr, "setup: render config: %v\n", err)
return 1
}
if err := installValidatedConfig(loader, flags.cfgPath, sink, rendered); err != nil {
fmt.Fprintf(stderr, "setup: %v\n", err)
return 1
}
fmt.Fprintf(stdout, "setup: wrote %s\n", flags.cfgPath)
return 0
}
// installValidatedConfig proves rendered loads through a real Loader, then
// writes the config atomically with mode 0600 and only then commits the
// secret sink. A validation failure returns before either write, so a bad
// generated config never leaves a config file or a secret on disk, and a
// secret is never committed pointing at a config that was never written.
func installValidatedConfig(loader *configuration.Loader, path string, sink setup.SecretSink, rendered []byte) error {
if err := proveLoadable(loader, rendered); err != nil {
return err
}
if err := writeFileAtomicMode(path, rendered, 0o600); err != nil {
return fmt.Errorf("write config: %w", err)
}
if err := sink.Commit(); err != nil {
return fmt.Errorf("commit secrets: %w", err)
}
return nil
}
// proveLoadable decodes and validates rendered through a real
// configuration.Loader, the same check setup.Run's doc comment makes the
// caller's responsibility. The rendered bytes are staged in a temp file
// rather than the destination, so a load failure can never have already
// installed anything.
func proveLoadable(loader *configuration.Loader, rendered []byte) error {
tmp, err := os.CreateTemp("", "archie-setup-*.toml")
if err != nil {
return fmt.Errorf("prove config loads: %w", err)
}
name := tmp.Name()
defer os.Remove(name)
if err := tmp.Close(); err != nil {
return fmt.Errorf("prove config loads: %w", err)
}
if err := os.WriteFile(name, rendered, 0o600); err != nil {
return fmt.Errorf("prove config loads: %w", err)
}
if _, err := loader.File(name); err != nil {
return fmt.Errorf("generated config does not load: %w", err)
}
return nil
}
// writeFileAtomicMode writes data to path via a same-directory temp file and
// rename, so a concurrent reader never observes a truncated config.
func writeFileAtomicMode(path string, data []byte, mode os.FileMode) error {
tmp, err := os.CreateTemp(filepath.Dir(path), ".config-*.toml")
if err != nil {
return err
}
name := tmp.Name()
defer os.Remove(name) // no-op after a successful rename
if err := tmp.Chmod(mode); err != nil {
tmp.Close()
return err
}
if _, err := tmp.Write(data); err != nil {
tmp.Close()
return err
}
if err := tmp.Sync(); err != nil {
tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
return os.Rename(name, path)
}