moth/cmd/transpile/puzzle.go

256 lines
5.5 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"bytes"
2020-08-28 17:41:17 -06:00
"context"
"encoding/json"
"fmt"
"io"
2020-09-04 13:00:23 -06:00
"io/ioutil"
2020-09-01 20:12:57 -06:00
"log"
"net/mail"
2020-08-28 17:41:17 -06:00
"os/exec"
"strconv"
"strings"
2020-08-28 17:41:17 -06:00
"time"
2020-08-14 20:26:04 -06:00
"github.com/russross/blackfriday"
2020-08-28 17:41:17 -06:00
"github.com/spf13/afero"
2020-08-14 20:26:04 -06:00
"gopkg.in/yaml.v2"
)
2020-09-04 13:00:23 -06:00
// PuzzleProvider establishes the functionality required to provide one puzzle.
type PuzzleProvider interface {
// Puzzle returns a Puzzle struct for the current puzzle.
Puzzle() (Puzzle, error)
// Open returns a newly-opened file.
Open(filename string) (io.ReadCloser, error)
// Answer returns whether the provided answer is correct.
Answer(answer string) bool
}
2020-09-03 20:04:43 -06:00
// NewFsPuzzle returns a new FsPuzzle for points.
2020-09-04 13:00:23 -06:00
func NewFsPuzzle(fs afero.Fs, points int) PuzzleProvider {
pfs := NewRecursiveBasePathFs(fs, strconv.Itoa(points))
if info, err := pfs.Stat("mkpuzzle"); (err == nil) && (info.Mode()&0100 != 0) {
if command, err := pfs.RealPath(info.Name()); err != nil {
log.Println("Unable to resolve full path to", info.Name(), pfs)
} else {
return FsCommandPuzzle{
fs: pfs,
command: command,
timeout: 2 * time.Second,
}
}
2020-08-28 17:41:17 -06:00
}
2020-09-04 13:00:23 -06:00
return FsPuzzle{
fs: pfs,
}
2019-08-17 13:09:09 -06:00
}
2020-09-03 20:04:43 -06:00
// FsPuzzle is a single puzzle's directory.
type FsPuzzle struct {
fs afero.Fs
2020-09-01 20:12:57 -06:00
mkpuzzle bool
}
2020-09-03 20:04:43 -06:00
// Puzzle returns a Puzzle struct for the current puzzle.
func (fp FsPuzzle) Puzzle() (Puzzle, error) {
r, err := fp.fs.Open("puzzle.md")
2020-08-28 17:41:17 -06:00
if err != nil {
2020-08-31 16:37:51 -06:00
var err2 error
2020-09-03 20:04:43 -06:00
if r, err2 = fp.fs.Open("puzzle.moth"); err2 != nil {
2020-09-01 20:12:57 -06:00
return Puzzle{}, err
2020-08-31 16:37:51 -06:00
}
2020-08-28 17:41:17 -06:00
}
defer r.Close()
headerBuf := new(bytes.Buffer)
2020-09-01 20:12:57 -06:00
headerParser := rfc822HeaderParser
2020-08-28 17:41:17 -06:00
headerEnd := ""
scanner := bufio.NewScanner(r)
lineNo := 0
for scanner.Scan() {
line := scanner.Text()
2020-08-28 17:41:17 -06:00
lineNo++
if lineNo == 1 {
if line == "---" {
2020-09-01 20:12:57 -06:00
headerParser = yamlHeaderParser
headerEnd = "---"
continue
}
}
if line == headerEnd {
2020-08-28 17:41:17 -06:00
headerBuf.WriteRune('\n')
break
}
headerBuf.WriteString(line)
headerBuf.WriteRune('\n')
}
2019-08-17 13:09:09 -06:00
bodyBuf := new(bytes.Buffer)
for scanner.Scan() {
line := scanner.Text()
2020-08-28 17:41:17 -06:00
lineNo++
bodyBuf.WriteString(line)
bodyBuf.WriteRune('\n')
}
2019-08-17 13:09:09 -06:00
2020-09-01 20:12:57 -06:00
puzzle, err := headerParser(headerBuf)
if err != nil {
return puzzle, err
}
2020-08-14 20:26:04 -06:00
2019-08-17 16:00:15 -06:00
// Markdownify the body
2020-09-01 20:12:57 -06:00
if puzzle.Pre.Body != "" {
if bodyBuf.Len() > 0 {
return puzzle, fmt.Errorf("Puzzle body present in header and in moth body")
}
} else {
puzzle.Pre.Body = string(blackfriday.Run(bodyBuf.Bytes()))
2020-08-28 17:41:17 -06:00
}
2020-09-01 20:12:57 -06:00
return puzzle, nil
2020-08-28 17:41:17 -06:00
}
2020-09-03 20:04:43 -06:00
// Open returns a newly-opened file.
func (fp FsPuzzle) Open(name string) (io.ReadCloser, error) {
return fp.fs.Open(name)
2020-08-28 17:41:17 -06:00
}
2020-09-01 20:12:57 -06:00
func legacyAttachmentParser(val []string) []Attachment {
ret := make([]Attachment, len(val))
for idx, txt := range val {
parts := strings.SplitN(txt, " ", 3)
cur := Attachment{}
cur.FilesystemPath = parts[0]
if len(parts) > 1 {
cur.Filename = parts[1]
} else {
cur.Filename = cur.FilesystemPath
}
if (len(parts) > 2) && (parts[2] == "hidden") {
cur.Listed = false
} else {
cur.Listed = true
}
ret[idx] = cur
}
return ret
}
func yamlHeaderParser(r io.Reader) (Puzzle, error) {
p := Puzzle{}
decoder := yaml.NewDecoder(r)
decoder.SetStrict(true)
err := decoder.Decode(&p)
return p, err
}
func rfc822HeaderParser(r io.Reader) (Puzzle, error) {
p := Puzzle{}
m, err := mail.ReadMessage(r)
if err != nil {
return p, fmt.Errorf("Parsing RFC822 headers: %v", err)
}
for key, val := range m.Header {
key = strings.ToLower(key)
switch key {
case "author":
p.Pre.Authors = val
case "pattern":
p.Pre.AnswerPattern = val[0]
case "script":
p.Pre.Scripts = legacyAttachmentParser(val)
case "file":
p.Pre.Attachments = legacyAttachmentParser(val)
case "answer":
p.Answers = val
case "summary":
p.Debug.Summary = val[0]
case "hint":
p.Debug.Hints = val
case "ksa":
p.Post.KSAs = val
default:
return p, fmt.Errorf("Unknown header field: %s", key)
}
}
return p, nil
}
2020-09-03 20:04:43 -06:00
2020-09-04 13:00:23 -06:00
// Answer checks whether the given answer is correct.
2020-09-03 20:04:43 -06:00
func (fp FsPuzzle) Answer(answer string) bool {
return false
}
2020-09-04 13:00:23 -06:00
// FsCommandPuzzle provides an FsPuzzle backed by running a command.
2020-09-03 20:04:43 -06:00
type FsCommandPuzzle struct {
2020-09-04 13:00:23 -06:00
fs afero.Fs
command string
timeout time.Duration
2020-09-03 20:04:43 -06:00
}
2020-09-04 13:00:23 -06:00
// Puzzle returns a Puzzle struct for the current puzzle.
2020-09-03 20:04:43 -06:00
func (fp FsCommandPuzzle) Puzzle() (Puzzle, error) {
2020-09-04 13:00:23 -06:00
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
2020-09-03 20:04:43 -06:00
defer cancel()
2020-09-04 13:00:23 -06:00
cmd := exec.CommandContext(ctx, fp.command)
2020-09-03 20:04:43 -06:00
stdout, err := cmd.Output()
if err != nil {
return Puzzle{}, err
}
jsdec := json.NewDecoder(bytes.NewReader(stdout))
jsdec.DisallowUnknownFields()
puzzle := Puzzle{}
if err := jsdec.Decode(&puzzle); err != nil {
return Puzzle{}, err
}
return puzzle, nil
}
2020-09-04 13:00:23 -06:00
// Open returns a newly-opened file.
2020-09-03 20:04:43 -06:00
func (fp FsCommandPuzzle) Open(filename string) (io.ReadCloser, error) {
2020-09-04 13:00:23 -06:00
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
defer cancel()
cmd := exec.CommandContext(ctx, fp.command, "-file", filename)
// BUG(neale): FsCommandPuzzle.Open() reads everything into memory, and will suck for large files.
out, err := cmd.Output()
2020-09-04 15:29:06 -06:00
buf := ioutil.NopCloser(bytes.NewBuffer(out))
2020-09-04 13:00:23 -06:00
if err != nil {
2020-09-04 15:29:06 -06:00
return buf, err
2020-09-04 13:00:23 -06:00
}
2020-09-04 15:29:06 -06:00
return buf, nil
2020-09-03 20:04:43 -06:00
}
2020-09-04 13:00:23 -06:00
// Answer checks whether the given answer is correct.
2020-09-03 20:04:43 -06:00
func (fp FsCommandPuzzle) Answer(answer string) bool {
2020-09-04 13:00:23 -06:00
ctx, cancel := context.WithTimeout(context.Background(), fp.timeout)
defer cancel()
cmd := exec.CommandContext(ctx, fp.command, "-answer", answer)
out, err := cmd.Output()
if err != nil {
2020-09-04 15:29:06 -06:00
log.Printf("ERROR: checking answer: %s", err)
2020-09-04 13:00:23 -06:00
return false
}
switch strings.TrimSpace(string(out)) {
case "correct":
return true
}
2020-09-03 20:04:43 -06:00
return false
}