2020-08-27 19:29:54 -06:00
|
|
|
// Provides a Puzzle interface that runs a command for each request
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-10-16 14:18:44 -06:00
|
|
|
"encoding/json"
|
2020-09-15 15:58:21 -06:00
|
|
|
"fmt"
|
2020-08-27 19:29:54 -06:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-08-28 14:05:32 -06:00
|
|
|
"sort"
|
2020-08-27 19:29:54 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-10-16 14:18:44 -06:00
|
|
|
|
|
|
|
"github.com/dirtbags/moth/pkg/transpile"
|
2020-08-27 19:29:54 -06:00
|
|
|
)
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
// ProviderCommand specifies a command to run for the puzzle API
|
|
|
|
type ProviderCommand struct {
|
2020-08-27 19:29:54 -06:00
|
|
|
Path string
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inventory runs with "action=inventory", and parses the output into a category list.
|
2020-09-08 17:49:02 -06:00
|
|
|
func (pc ProviderCommand) Inventory() (inv []Category) {
|
2020-08-27 19:29:54 -06:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
|
|
|
cmd.Env = os.Environ()
|
2020-08-28 14:05:32 -06:00
|
|
|
cmd.Env = append(cmd.Env, "ACTION=inventory")
|
2020-08-27 19:29:54 -06:00
|
|
|
|
|
|
|
stdout, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(string(stdout), "\n") {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(line, " ")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
log.Println("Skipping misformatted line:", line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := parts[0]
|
|
|
|
puzzles := make([]int, 0, 10)
|
|
|
|
for _, pointsString := range parts[1:] {
|
|
|
|
points, err := strconv.Atoi(pointsString)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
puzzles = append(puzzles, points)
|
|
|
|
}
|
2020-08-28 14:05:32 -06:00
|
|
|
sort.Ints(puzzles)
|
2020-08-27 19:29:54 -06:00
|
|
|
inv = append(inv, Category{name, puzzles})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
// NullReadSeekCloser wraps a no-op Close method around an io.ReadSeeker.
|
2020-08-27 19:29:54 -06:00
|
|
|
type NullReadSeekCloser struct {
|
|
|
|
io.ReadSeeker
|
|
|
|
}
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
// Close does nothing.
|
2020-08-27 19:29:54 -06:00
|
|
|
func (f NullReadSeekCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open passes its arguments to the command with "action=open".
|
2020-09-08 17:49:02 -06:00
|
|
|
func (pc ProviderCommand) Open(cat string, points int, path string) (ReadSeekCloser, time.Time, error) {
|
2020-08-27 19:29:54 -06:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
|
|
|
cmd.Env = os.Environ()
|
2020-08-28 14:05:32 -06:00
|
|
|
cmd.Env = append(cmd.Env, "ACTION=open")
|
|
|
|
cmd.Env = append(cmd.Env, "CAT="+cat)
|
|
|
|
cmd.Env = append(cmd.Env, "POINTS="+strconv.Itoa(points))
|
|
|
|
cmd.Env = append(cmd.Env, "FILENAME="+path)
|
2020-08-27 19:29:54 -06:00
|
|
|
|
|
|
|
stdoutBytes, err := cmd.Output()
|
|
|
|
stdout := NullReadSeekCloser{bytes.NewReader(stdoutBytes)}
|
|
|
|
now := time.Now()
|
|
|
|
return stdout, now, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckAnswer passes its arguments to the command with "action=answer".
|
|
|
|
// If the command exits successfully and sends "correct" to stdout,
|
|
|
|
// nil is returned.
|
2020-09-08 17:49:02 -06:00
|
|
|
func (pc ProviderCommand) CheckAnswer(cat string, points int, answer string) (bool, error) {
|
2020-08-27 19:29:54 -06:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
|
|
|
cmd.Env = os.Environ()
|
2020-08-28 14:05:32 -06:00
|
|
|
cmd.Env = append(cmd.Env, "ACTION=answer")
|
|
|
|
cmd.Env = append(cmd.Env, "CAT="+cat)
|
|
|
|
cmd.Env = append(cmd.Env, "POINTS="+strconv.Itoa(points))
|
|
|
|
cmd.Env = append(cmd.Env, "ANSWER="+answer)
|
2020-08-27 19:29:54 -06:00
|
|
|
|
|
|
|
stdout, err := cmd.Output()
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
2020-09-17 19:48:17 -06:00
|
|
|
log.Printf("WARNING: %s: %s", pc.Path, string(ee.Stderr))
|
2020-09-08 17:49:02 -06:00
|
|
|
return false, err
|
2020-08-27 19:29:54 -06:00
|
|
|
} else if err != nil {
|
2020-09-08 17:49:02 -06:00
|
|
|
return false, err
|
2020-08-27 19:29:54 -06:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:18:44 -06:00
|
|
|
ans := transpile.AnswerResponse{}
|
|
|
|
if err := json.Unmarshal(stdout, &ans); err != nil {
|
|
|
|
return false, err
|
2020-08-27 19:29:54 -06:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:18:44 -06:00
|
|
|
return ans.Correct, nil
|
2020-08-27 19:29:54 -06:00
|
|
|
}
|
|
|
|
|
2020-09-15 15:58:21 -06:00
|
|
|
// Mothball just returns an error
|
|
|
|
func (pc ProviderCommand) Mothball(cat string) (*bytes.Reader, error) {
|
|
|
|
return nil, fmt.Errorf("Can't package a command-generated category")
|
|
|
|
}
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
// Maintain does nothing: a command puzzle ProviderCommand has no housekeeping
|
|
|
|
func (pc ProviderCommand) Maintain(updateInterval time.Duration) {
|
2020-08-27 19:29:54 -06:00
|
|
|
}
|