62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
|
|
package bdd
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"go-doc/storage"
|
||
|
|
)
|
||
|
|
|
||
|
|
func errf(format string, args ...any) error {
|
||
|
|
return fmt.Errorf(format, args...)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Badge assertions shared by the feedback features, keyed to the role -> badge
|
||
|
|
// contract mandated by the specification: User 🟢, Operator 🔵, Developer 🔴.
|
||
|
|
var badgeContract = map[storage.Role]string{
|
||
|
|
storage.RoleUser: "🟢",
|
||
|
|
storage.RoleOperator: "🔵",
|
||
|
|
storage.RoleDeveloper: "🔴",
|
||
|
|
}
|
||
|
|
|
||
|
|
// Hex colours the frontend mirrors when it renders the badge.
|
||
|
|
var badgeHex = map[storage.Role]string{
|
||
|
|
storage.RoleUser: "#22c55e",
|
||
|
|
storage.RoleOperator: "#3b82f6",
|
||
|
|
storage.RoleDeveloper: "#ef4444",
|
||
|
|
}
|
||
|
|
|
||
|
|
// JSONArrayPrefix is the first byte of the on-disk feedback file. The spec
|
||
|
|
// requires the file to *be* the JSON array, so every write starts with "[".
|
||
|
|
const JSONArrayPrefix = `[`
|
||
|
|
|
||
|
|
func appendComment(c *Context, slug string, role storage.Role, author, content string) (storage.Comment, error) {
|
||
|
|
return c.Engine.AppendComment(slug, storage.CommentInput{
|
||
|
|
Author: author,
|
||
|
|
Role: string(role),
|
||
|
|
Content: content,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// listDir returns the entry names of a directory, for temp-file leak checks.
|
||
|
|
func listDir(dir string) ([]string, error) {
|
||
|
|
entries, err := os.ReadDir(dir)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
names := make([]string, 0, len(entries))
|
||
|
|
for _, entry := range entries {
|
||
|
|
names = append(names, entry.Name())
|
||
|
|
}
|
||
|
|
return names, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// truncate shortens a document for readable failure output.
|
||
|
|
func truncate(text string) string {
|
||
|
|
const limit = 120
|
||
|
|
if len(text) <= limit {
|
||
|
|
return text
|
||
|
|
}
|
||
|
|
return text[:limit] + "…"
|
||
|
|
}
|