feat(builder): launch options must appear in the research sources
The Valheim draft in #13 passed -maxplayers, an option Valheim doesn't have. The server ignores unknown options, so the draft booted and passed every boot test while its MaxPlayers setting did nothing. build now reads the pages research based the variant on, in full, and the drafter rejects any launch option in the startup command that none of them, nor the image's own scripts, mention. The model gets it back to remove or spell correctly. Options of shell tools (mkdir -p, [ -n ...]) are skipped. The check is off when there are no sources (draft run by hand, no reader). Against #13's 11 real sources it flags -maxplayers and nothing else. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dd92b6be4c
commit
8b49895d5e
6 files changed
+158
-3
No files matched your search
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -248,3 +249,77 @@ func portNumber(v any) (int, bool) {
|
||||
p, err := strconv.Atoi(strings.TrimSpace(s))
|
||||
return p, err == nil && p > 0 && p < 65536
|
||||
}
|
||||
|
||||
var (
|
||||
optionToken = regexp.MustCompile(`(?:^|[\s("'])(-{1,2}[A-Za-z][A-Za-z0-9_.]*(?:-[A-Za-z0-9_.]+)*)`)
|
||||
segmentSep = regexp.MustCompile(`;|&&|\|\||\||\n`)
|
||||
testOps = map[string]bool{"eq": true, "ne": true, "lt": true, "gt": true, "le": true, "ge": true, "nt": true, "ot": true, "ef": true}
|
||||
// Options of these are the shell's and its tools', not the game's
|
||||
shellTools = map[string]bool{"mkdir": true, "cp": true, "mv": true, "ln": true, "rm": true, "chmod": true, "chown": true,
|
||||
"wget": true, "curl": true, "tar": true, "unzip": true, "test": true, "[": true, "[[": true, "echo": true, "printf": true,
|
||||
"sleep": true, "find": true, "sed": true, "grep": true, "cd": true, "export": true, "touch": true, "bash": true, "sh": true,
|
||||
"cat": true, "head": true, "tail": true, "ls": true, "set": true, "ulimit": true, "env": true, "id": true}
|
||||
)
|
||||
|
||||
// undocumentedOptions finds launch options in the startup command that none of the researched pages (nor
|
||||
// the image's own scripts) mention. Servers silently ignore options they don't know, so a made-up one
|
||||
// (Valheim has no -maxplayers) boots fine and passes every boot test while its setting does nothing.
|
||||
func undocumentedOptions(game map[string]any, docs string, facts *imageFacts) []string {
|
||||
if strings.TrimSpace(docs) == "" {
|
||||
return nil
|
||||
}
|
||||
startup, _ := game["defaultStartupCommand"].(string)
|
||||
corpus := docs
|
||||
if facts != nil {
|
||||
for _, s := range facts.Scripts {
|
||||
corpus += "\n" + s
|
||||
}
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var missing []string
|
||||
for _, opt := range launchOptions(unwrapShell(startup)) {
|
||||
name := strings.TrimLeft(opt, "-")
|
||||
key := strings.ToLower(name)
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
documented := regexp.MustCompile(`(?i)(^|[^A-Za-z0-9_])-{1,2}` + regexp.QuoteMeta(name) + `($|[^A-Za-z0-9_])`)
|
||||
if !documented.MatchString(corpus) {
|
||||
missing = append(missing, opt)
|
||||
}
|
||||
}
|
||||
if len(missing) == 0 {
|
||||
return nil
|
||||
}
|
||||
return []string{fmt.Sprintf("the startup command passes %s, which none of the pages researched for this game mention. "+
|
||||
"Servers silently ignore options they don't know, so it boots fine but does nothing: remove it (and any setting that "+
|
||||
"only feeds it), or spell it exactly as the documentation does", strings.Join(missing, ", "))}
|
||||
}
|
||||
|
||||
// launchOptions lists the -options passed to the game, skipping the shell's own (mkdir -p, [ -n ... ]).
|
||||
func launchOptions(cmd string) []string {
|
||||
var out []string
|
||||
for _, seg := range segmentSep.Split(cmd, -1) {
|
||||
fields := strings.Fields(strings.TrimLeft(strings.TrimSpace(seg), "({"))
|
||||
i := 0
|
||||
for i < len(fields) && (fields[i] == "exec" || fields[i] == "then" || fields[i] == "do" || fields[i] == "else" ||
|
||||
(assignWord.MatchString(fields[i]) && !strings.Contains(fields[i], "=("))) {
|
||||
i++
|
||||
}
|
||||
if i == len(fields) {
|
||||
continue
|
||||
}
|
||||
if word := path.Base(fields[i]); shellTools[word] {
|
||||
continue
|
||||
}
|
||||
for _, m := range optionToken.FindAllStringSubmatch(strings.Join(fields[i:], " "), -1) {
|
||||
name := strings.TrimLeft(m[1], "-")
|
||||
if len(name) < 2 || testOps[name] {
|
||||
continue
|
||||
}
|
||||
out = append(out, m[1])
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user