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.
112 lines
3.5 KiB
Go
112 lines
3.5 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
|
|
)
|
|
|
|
// forgeToken names the env var a forge type's token is stored under. Used
|
|
// by both the config edit (forge.token = {engine="env", key=...}) and the
|
|
// secret sink, for the same reason cloudProviders is a single table in
|
|
// step_provider.go: the installer's original bug was these two writes
|
|
// disagreeing.
|
|
var forgeToken = map[string]string{
|
|
"github": "ARCHIE_GITHUB_TOKEN",
|
|
"gitea": "ARCHIE_GITEA_TOKEN",
|
|
}
|
|
|
|
func stepForge(ctx context.Context, p Prompter, secrets SecretSink, existingHost string, params Params) (tableEdits, error) {
|
|
var choice int
|
|
if strings.TrimSpace(params.ForgeType) != "" {
|
|
var err error
|
|
choice, err = forgeChoice(params.ForgeType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: forge: %w", err)
|
|
}
|
|
} else {
|
|
var err error
|
|
choice, err = p.Select(ctx, "Code forge:", []string{"GitHub", "Gitea", "None (standalone mode)"})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: forge: %w", err)
|
|
}
|
|
}
|
|
|
|
switch choice {
|
|
case 0:
|
|
host := params.ForgeHost
|
|
if strings.TrimSpace(host) == "" {
|
|
host = "https://github.com"
|
|
}
|
|
return stepForgeWithToken(ctx, p, secrets, "github", host, "GitHub token (leave blank to configure later): ", params.ForgeToken)
|
|
case 1:
|
|
defaultHost := existingHost
|
|
if defaultHost == "" {
|
|
defaultHost = "https://gitea.example.com"
|
|
}
|
|
host := params.ForgeHost
|
|
if strings.TrimSpace(host) == "" {
|
|
var err error
|
|
host, err = p.ReadLine(ctx, "Gitea base URL: ", defaultHost)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: gitea host: %w", err)
|
|
}
|
|
}
|
|
return stepForgeWithToken(ctx, p, secrets, "gitea", host, "Gitea token (leave blank to configure later): ", params.ForgeToken)
|
|
default:
|
|
// The template's default [forge] block is active (type = "github"
|
|
// with a host and token already set). Selecting "none" must
|
|
// overwrite host and token too, not just type -- otherwise a
|
|
// disabled forge still shows a token reference nobody configured
|
|
// and that resolveForge never reads, which is exactly the kind of
|
|
// stale/misleading value this feature exists to stop shipping.
|
|
return tableEdits{"forge": {
|
|
"type": tomlwrite.String("none"),
|
|
"host": tomlwrite.String(""),
|
|
"token": tomlwrite.Ref("", ""),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
// forgeChoice maps a typed Params.ForgeType to the same select index the
|
|
// interactive prompt uses, so a supplied forge type and a prompted one land
|
|
// in exactly the same step logic.
|
|
func forgeChoice(name string) (int, error) {
|
|
switch strings.ToLower(strings.TrimSpace(name)) {
|
|
case "github":
|
|
return 0, nil
|
|
case "gitea":
|
|
return 1, nil
|
|
case "none":
|
|
return 2, nil
|
|
default:
|
|
return -1, fmt.Errorf("unknown forge type %q (want \"github\", \"gitea\", or \"none\")", name)
|
|
}
|
|
}
|
|
|
|
func stepForgeWithToken(ctx context.Context, p Prompter, secrets SecretSink, forgeType, host, tokenPrompt, tokenParam string) (tableEdits, error) {
|
|
edits := tableEdits{"forge": {
|
|
"type": tomlwrite.String(forgeType),
|
|
"host": tomlwrite.String(host),
|
|
}}
|
|
token := tokenParam
|
|
if token == "" {
|
|
var err error
|
|
token, err = p.ReadSecret(ctx, tokenPrompt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: %s token: %w", forgeType, err)
|
|
}
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return edits, nil
|
|
}
|
|
envKey := forgeToken[forgeType]
|
|
if err := secrets.Put("env", envKey, token); err != nil {
|
|
return nil, fmt.Errorf("setup: store %s token: %w", forgeType, err)
|
|
}
|
|
edits["forge"]["token"] = tomlwrite.Ref("env", envKey)
|
|
return edits, nil
|
|
}
|