feat(setup): one guided flow; ask for PostgreSQL and generate the bundled database's password
This commit is contained in:
7 files changed
+91
-271
No files matched your search
@@ -52,6 +52,7 @@ func IsSetupArgs(args []string) bool {
|
||||
// environment variable is consulted.
|
||||
type setupFlags struct {
|
||||
cfgPath string
|
||||
databaseURL string
|
||||
defaults bool
|
||||
botUser string
|
||||
operator string
|
||||
@@ -69,6 +70,7 @@ func bindSetupFlags(fs *flag.FlagSet) *setupFlags {
|
||||
f := &setupFlags{}
|
||||
fs.StringVar(&f.cfgPath, "config", configuration.DefaultConfigPath(), "path to write config.toml")
|
||||
fs.BoolVar(&f.defaults, "defaults", false, "write a known-valid unattended baseline config without prompting")
|
||||
fs.StringVar(&f.databaseURL, "database-url", "", "PostgreSQL 18 connection URL")
|
||||
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")
|
||||
@@ -92,6 +94,9 @@ func (f *setupFlags) params() (setup.Params, error) {
|
||||
if f.defaults {
|
||||
params = defaultsParams()
|
||||
}
|
||||
if f.databaseURL != "" {
|
||||
params.DatabaseURL = f.databaseURL
|
||||
}
|
||||
if f.botUser != "" {
|
||||
params.BotUser = f.botUser
|
||||
}
|
||||
@@ -168,9 +173,10 @@ func existingConfig(loader *configuration.Loader, path string) ([]byte, setup.Ex
|
||||
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,
|
||||
BotUser: doc.Config.BotUser,
|
||||
Operator: doc.Config.Chat.Operator,
|
||||
ForgeHost: doc.Config.Forge.Host,
|
||||
DatabaseURL: doc.Config.DatabaseURL,
|
||||
}
|
||||
}
|
||||
return base, existing, nil
|
||||
|
||||
@@ -4,6 +4,8 @@ package setup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -32,9 +34,10 @@ type ModelDiscovery interface {
|
||||
// ExistingValues prefills prompts on a re-run. The zero value is correct
|
||||
// for a fresh install.
|
||||
type ExistingValues struct {
|
||||
BotUser string
|
||||
Operator string
|
||||
ForgeHost string
|
||||
BotUser string
|
||||
Operator string
|
||||
ForgeHost string
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
// tableEdits groups pending edits by table before Run flattens them into
|
||||
@@ -45,12 +48,13 @@ type tableEdits = map[string]map[string]string
|
||||
// Params pre-answers setup questions; a step prompts only for unset fields.
|
||||
// Provider is a config class; Model is the bare model name.
|
||||
type Params struct {
|
||||
BotUser string
|
||||
Operator string
|
||||
ForgeType string // github | gitea | none
|
||||
ForgeHost string
|
||||
Provider string // openai | anthropic | openrouter | gemini | groq | deepseek | mistral | ollama
|
||||
Model string // bare model name; the step adds the "class/" prefix
|
||||
DatabaseURL string
|
||||
BotUser string
|
||||
Operator string
|
||||
ForgeType string // github | gitea | none
|
||||
ForgeHost string
|
||||
Provider string // openai | anthropic | openrouter | gemini | groq | deepseek | mistral | ollama
|
||||
Model string // bare model name; the step adds the "class/" prefix
|
||||
|
||||
// TelegramUserIDs non-empty configures Telegram. The reference fields name
|
||||
// already-stored secrets as engine:key; when set, the step writes the
|
||||
@@ -84,6 +88,12 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret
|
||||
}
|
||||
}
|
||||
|
||||
dbURL, err := stepDatabase(ctx, p, secrets, existing.DatabaseURL, params.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
add(tableEdits{"": {"database_url": tomlwrite.String(dbURL)}})
|
||||
|
||||
botUser := params.BotUser
|
||||
if strings.TrimSpace(botUser) == "" {
|
||||
var err error
|
||||
@@ -132,3 +142,32 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret
|
||||
|
||||
return edits, nil
|
||||
}
|
||||
|
||||
// stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install
|
||||
// means the bundled Compose database with a generated password, which goes to
|
||||
// the env file as POSTGRES_PASSWORD for Compose to initialise it with.
|
||||
func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) {
|
||||
if strings.TrimSpace(param) != "" {
|
||||
return param, nil
|
||||
}
|
||||
prompt := "PostgreSQL 18 URL (blank for the bundled database with a generated password): "
|
||||
if existing != "" {
|
||||
prompt = "PostgreSQL 18 URL: "
|
||||
}
|
||||
url, err := p.ReadLine(ctx, prompt, existing)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("setup: database url: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(url) != "" {
|
||||
return url, nil
|
||||
}
|
||||
pw := make([]byte, 24)
|
||||
if _, err := rand.Read(pw); err != nil {
|
||||
return "", fmt.Errorf("setup: generate database password: %w", err)
|
||||
}
|
||||
password := hex.EncodeToString(pw)
|
||||
if err := secrets.Put("env", "POSTGRES_PASSWORD", password); err != nil {
|
||||
return "", fmt.Errorf("setup: store database password: %w", err)
|
||||
}
|
||||
return "postgres://archie:" + password + "@127.0.0.1:5432/archie?sslmode=disable", nil
|
||||
}
|
||||
@@ -28,10 +28,6 @@ var cloudProviders = []cloudProvider{
|
||||
{name: "Mistral", class: "mistral", apiKeyEnv: "MISTRAL_API_KEY"},
|
||||
}
|
||||
|
||||
// templateDefaultActiveProvider is the provider config.example.toml enables
|
||||
// by default. Setup replaces that table unless OpenAI is chosen with a key.
|
||||
const templateDefaultActiveProvider = "openai"
|
||||
|
||||
func stepProvider(ctx context.Context, p Prompter, discovery ModelDiscovery, secrets SecretSink, params Params) (tableEdits, string, error) {
|
||||
options := make([]string, 0, len(cloudProviders)+1)
|
||||
for _, cp := range cloudProviders {
|
||||
@@ -54,26 +50,13 @@ func stepProvider(ctx context.Context, p Prompter, discovery ModelDiscovery, sec
|
||||
}
|
||||
}
|
||||
|
||||
var edits tableEdits
|
||||
var model string
|
||||
var err error
|
||||
if choice == len(cloudProviders) {
|
||||
if params.ProviderAPIKeyRef != (config.SecretRef{}) {
|
||||
return nil, "", fmt.Errorf("setup: a provider key reference was given, but the self-hosted provider is keyless, so nothing would use it")
|
||||
}
|
||||
edits, model, err = stepSelfHostedModel(ctx, p, discovery, params.Model)
|
||||
} else {
|
||||
edits, model, err = stepCloudProvider(ctx, p, secrets, cloudProviders[choice], params.Model, params.ProviderAPIKeyRef)
|
||||
return stepSelfHostedModel(ctx, p, discovery, params.Model)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
openaiTable := "providers." + templateDefaultActiveProvider
|
||||
if _, chosenOpenAI := edits[openaiTable]; !chosenOpenAI {
|
||||
edits[openaiTable] = map[string]string{"api_key": tomlwrite.Ref("", "")}
|
||||
}
|
||||
return edits, model, nil
|
||||
return stepCloudProvider(ctx, p, secrets, cloudProviders[choice], params.Model, params.ProviderAPIKeyRef)
|
||||
}
|
||||
|
||||
// providerChoice maps a typed Params.Provider class to the same select index
|
||||
@@ -150,13 +133,6 @@ func cloudKeyEdit(ctx context.Context, p Prompter, secrets SecretSink, cp cloudP
|
||||
return "", fmt.Errorf("setup: %s api key: %w", cp.name, err)
|
||||
}
|
||||
if strings.TrimSpace(key) == "" {
|
||||
if cp.class == templateDefaultActiveProvider {
|
||||
// Skipping the key normally means "configure later" and leaves
|
||||
// api_key untouched -- but for openai specifically that would leave
|
||||
// the template's own unresolvable bws default active. See
|
||||
// templateDefaultActiveProvider.
|
||||
return tomlwrite.Ref("", ""), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
if err := secrets.Put("env", cp.apiKeyEnv, key); err != nil {
|
||||
|
||||
Reference in new issue
Block a user