sort puzzlecmd inventory

This commit is contained in:
Neale Pickett 2020-08-28 14:05:32 -06:00
parent 8aea668af7
commit 97301fb222
3 changed files with 49 additions and 33 deletions

View File

@ -9,6 +9,7 @@ import (
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
@ -27,7 +28,7 @@ func (pc PuzzleCommand) Inventory() (inv []Category) {
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "action=inventory")
cmd.Env = append(cmd.Env, "ACTION=inventory")
stdout, err := cmd.Output()
if err != nil {
@ -55,6 +56,7 @@ func (pc PuzzleCommand) Inventory() (inv []Category) {
}
puzzles = append(puzzles, points)
}
sort.Ints(puzzles)
inv = append(inv, Category{name, puzzles})
}
return
@ -75,10 +77,10 @@ func (pc PuzzleCommand) Open(cat string, points int, path string) (ReadSeekClose
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
cmd.Env = os.Environ()
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, "path="+path)
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)
stdoutBytes, err := cmd.Output()
stdout := NullReadSeekCloser{bytes.NewReader(stdoutBytes)}
@ -95,10 +97,10 @@ func (pc PuzzleCommand) CheckAnswer(cat string, points int, answer string) error
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
cmd.Env = os.Environ()
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)
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)
stdout, err := cmd.Output()
if ee, ok := err.(*exec.ExitError); ok {

View File

@ -28,6 +28,9 @@ func TestPuzzleCommand(t *testing.T) {
if len(cat.Puzzles) != 3 {
t.Errorf("nealegoy wrong number of puzzles: %d", len(cat.Puzzles))
}
if cat.Puzzles[2] != 3 {
t.Errorf("out of order point values were not sorted")
}
}
}
@ -59,4 +62,9 @@ func TestPuzzleCommand(t *testing.T) {
} else {
f.Close()
}
if f, _, err := pc.Open("pategory", 1, "not.there"); err == nil {
f.Close()
t.Errorf("Non-existent file didn't return error: %#v", f)
}
}

View File

@ -1,30 +1,36 @@
#! /bin/sh -e
case "$action:$cat:$points" in
inventory::)
fail () {
echo "$@" 1>&2
exit 1
}
case "$ACTION:$CAT:$POINTS" in
inventory::)
echo "pategory 1 2 3 4 5 10 20 300"
echo "nealegory 1 2 3"
echo "nealegory 1 3 2"
;;
open:*:*)
if [ "$path" = "moo.txt" ]; then
open:*:*)
case "$CAT:$POINTS:$FILENAME" in
*:*:moo.txt)
echo "Moo."
else
cat $cat_$points_$path
fi
;;
answer:pategory:1)
if [ "$answer" = "answer" ]; then
*)
fail "Cannot open: $FILENAME"
;;
esac
;;
answer:pategory:1)
if [ "$ANSWER" = "answer" ]; then
echo "correct"
else
echo "Sorry, wrong answer."
fi
;;
answer:pategory:2)
echo "Internal error" 1>&2
exit 1
answer:pategory:2)
fail "Internal error"
;;
*)
echo "ERROR: Unknown action: $action" 1>&2
exit 1
*)
fail "ERROR: Unknown action: $action"
;;
esac