Add a little clarity

This commit is contained in:
Neale Pickett 2020-09-16 17:54:29 -06:00
parent 7f326ed90b
commit 0a60a649a1
3 changed files with 23 additions and 8 deletions

View File

@ -32,17 +32,21 @@ func nothing() error {
return nil return nil
} }
func usage(w io.Writer) {
fmt.Fprintln(w, "Usage: transpile COMMAND [flags]")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " mothball: Compile a mothball")
fmt.Fprintln(w, " inventory: Show category inventory")
fmt.Fprintln(w, " open: Open a file for a puzzle")
fmt.Fprintln(w, " answer: Check correctness of an answer")
}
// ParseArgs parses arguments and runs the appropriate action. // ParseArgs parses arguments and runs the appropriate action.
func (t *T) ParseArgs() (Command, error) { func (t *T) ParseArgs() (Command, error) {
var cmd Command var cmd Command
if len(t.Args) == 1 { if len(t.Args) == 1 {
fmt.Fprintln(t.Stderr, "Usage: transpile COMMAND [flags]") usage(t.Stderr)
fmt.Fprintln(t.Stderr, "")
fmt.Fprintln(t.Stderr, " mothball: Compile a mothball")
fmt.Fprintln(t.Stderr, " inventory: Show category inventory")
fmt.Fprintln(t.Stderr, " open: Open a file for a puzzle")
fmt.Fprintln(t.Stderr, " answer: Check correctness of an answer")
return nothing, nil return nothing, nil
} }
@ -60,7 +64,11 @@ func (t *T) ParseArgs() (Command, error) {
case "answer": case "answer":
flags.StringVar(&t.answer, "answer", "", "Answer to check") flags.StringVar(&t.answer, "answer", "", "Answer to check")
cmd = t.CheckAnswer cmd = t.CheckAnswer
case "help":
usage(t.Stderr)
return nothing, nil
default: default:
usage(t.Stderr)
return nothing, fmt.Errorf("%s is not a valid command", t.Args[1]) return nothing, fmt.Errorf("%s is not a valid command", t.Args[1])
} }
@ -69,6 +77,7 @@ func (t *T) ParseArgs() (Command, error) {
return nothing, err return nothing, err
} }
if *directory != "" { if *directory != "" {
log.Println(*directory)
t.fs = afero.NewBasePathFs(t.BaseFs, *directory) t.fs = afero.NewBasePathFs(t.BaseFs, *directory)
} else { } else {
t.fs = t.BaseFs t.fs = t.BaseFs
@ -101,6 +110,7 @@ func (t *T) PrintInventory() error {
} }
// DumpFile writes a file to the writer. // DumpFile writes a file to the writer.
// BUG(neale): The "open" and "answer" actions don't work on categories with an "mkcategory" executable.
func (t *T) DumpFile() error { func (t *T) DumpFile() error {
puzzle := transpile.NewFsPuzzle(t.fs) puzzle := transpile.NewFsPuzzle(t.fs)
@ -160,6 +170,7 @@ func main() {
Stdout: os.Stdout, Stdout: os.Stdout,
Stderr: os.Stderr, Stderr: os.Stderr,
Args: os.Args, Args: os.Args,
BaseFs: afero.NewOsFs(),
} }
cmd, err := t.ParseArgs() cmd, err := t.ParseArgs()
if err != nil { if err != nil {

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"os/exec" "os/exec"
"path" "path"
@ -134,7 +135,9 @@ func (c FsCommandCategory) run(command string, args ...string) ([]byte, error) {
// Inventory returns a list of point values for this category. // Inventory returns a list of point values for this category.
func (c FsCommandCategory) Inventory() ([]int, error) { func (c FsCommandCategory) Inventory() ([]int, error) {
stdout, err := c.run("inventory") stdout, err := c.run("inventory")
if err != nil { if exerr, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("inventory: %s: %s", err, string(exerr.Stderr))
} else if err != nil {
return nil, err return nil, err
} }

View File

@ -29,7 +29,8 @@ func FsInventory(fs afero.Fs) (Inventory, error) {
c := NewFsCategory(fs, name) c := NewFsCategory(fs, name)
puzzles, err := c.Inventory() puzzles, err := c.Inventory()
if err != nil { if err != nil {
return nil, err log.Printf("Inventory: %s: %s", name, err)
continue
} }
sort.Ints(puzzles) sort.Ints(puzzles)
inv[name] = puzzles inv[name] = puzzles