moth/cmd/transpile/main.go

203 lines
4.0 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
2020-08-28 17:41:17 -06:00
"flag"
"fmt"
2020-08-28 17:41:17 -06:00
"io"
"log"
"os"
"sort"
2020-09-08 17:49:02 -06:00
"github.com/dirtbags/moth/pkg/transpile"
2020-08-28 17:41:17 -06:00
"github.com/spf13/afero"
)
2020-09-11 13:03:19 -06:00
// T represents the state of things
2020-08-28 17:41:17 -06:00
type T struct {
2020-10-12 17:44:44 -06:00
Stdout io.Writer
Stderr io.Writer
Args []string
BaseFs afero.Fs
fs afero.Fs
// Arguments
2020-09-11 13:03:19 -06:00
filename string
answer string
}
2020-09-11 13:03:19 -06:00
// Command is a function invoked by the user
type Command func() error
func nothing() error {
return nil
2020-08-28 17:41:17 -06:00
}
2020-09-16 17:54:29 -06:00
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, " puzzle: Print puzzle JSON")
fmt.Fprintln(w, " file: Open a file for a puzzle")
2020-09-16 17:54:29 -06:00
fmt.Fprintln(w, " answer: Check correctness of an answer")
}
2020-09-11 13:03:19 -06:00
// ParseArgs parses arguments and runs the appropriate action.
func (t *T) ParseArgs() (Command, error) {
var cmd Command
if len(t.Args) == 1 {
2020-09-16 17:54:29 -06:00
usage(t.Stderr)
2020-09-11 13:03:19 -06:00
return nothing, nil
}
flags := flag.NewFlagSet(t.Args[1], flag.ContinueOnError)
2020-10-12 17:44:44 -06:00
flags.SetOutput(t.Stderr)
2020-09-11 13:03:19 -06:00
directory := flags.String("dir", "", "Work directory")
switch t.Args[1] {
case "mothball":
cmd = t.DumpMothball
2020-10-12 17:44:44 -06:00
flags.StringVar(&t.filename, "out", "", "Path to create mothball (empty for stdout)")
2020-08-28 17:41:17 -06:00
case "inventory":
2020-09-11 13:03:19 -06:00
cmd = t.PrintInventory
case "puzzle":
cmd = t.DumpPuzzle
case "file":
2020-09-11 13:03:19 -06:00
cmd = t.DumpFile
2020-10-12 17:44:44 -06:00
flags.StringVar(&t.filename, "file", "puzzle.json", "Filename to open")
2020-09-11 13:03:19 -06:00
case "answer":
cmd = t.CheckAnswer
2020-10-12 17:44:44 -06:00
flags.StringVar(&t.answer, "answer", "", "Answer to check")
2020-09-16 17:54:29 -06:00
case "help":
usage(t.Stderr)
return nothing, nil
2020-08-28 17:41:17 -06:00
default:
2020-09-18 12:52:04 -06:00
fmt.Fprintln(t.Stderr, "ERROR:", t.Args[1], "is not a valid command")
2020-09-16 17:54:29 -06:00
usage(t.Stderr)
2020-09-18 12:52:04 -06:00
return nothing, fmt.Errorf("Invalid command")
2020-09-11 13:03:19 -06:00
}
if err := flags.Parse(t.Args[2:]); err != nil {
return nothing, err
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if *directory != "" {
t.fs = afero.NewBasePathFs(t.BaseFs, *directory)
} else {
t.fs = t.BaseFs
}
return cmd, nil
2020-08-28 17:41:17 -06:00
}
// PrintInventory prints a puzzle inventory to stdout
func (t *T) PrintInventory() error {
2020-09-11 13:03:19 -06:00
inv, err := transpile.FsInventory(t.fs)
2020-08-28 17:41:17 -06:00
if err != nil {
return err
}
2020-09-04 15:29:06 -06:00
2020-09-11 13:03:19 -06:00
cats := make([]string, 0, len(inv))
for cat := range inv {
cats = append(cats, cat)
}
sort.Strings(cats)
for _, cat := range cats {
puzzles := inv[cat]
fmt.Fprint(t.Stdout, cat)
for _, p := range puzzles {
fmt.Fprint(t.Stdout, " ", p)
}
fmt.Fprintln(t.Stdout)
2020-09-04 15:29:06 -06:00
}
2020-08-28 17:41:17 -06:00
return nil
}
// DumpPuzzle writes a puzzle's JSON to the writer.
func (t *T) DumpPuzzle() error {
puzzle := transpile.NewFsPuzzle(t.fs)
p, err := puzzle.Puzzle()
if err != nil {
return err
}
jp, err := json.Marshal(p)
if err != nil {
return err
}
t.Stdout.Write(jp)
return nil
}
2020-09-11 13:03:19 -06:00
// DumpFile writes a file to the writer.
func (t *T) DumpFile() error {
puzzle := transpile.NewFsPuzzle(t.fs)
2020-08-28 17:41:17 -06:00
f, err := puzzle.Open(t.filename)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(t.Stdout, f); err != nil {
return err
2020-08-28 17:41:17 -06:00
}
return nil
}
2020-10-12 17:44:44 -06:00
// DumpMothball writes a mothball to the writer, or an output file if specified.
2020-09-11 13:03:19 -06:00
func (t *T) DumpMothball() error {
2020-10-12 17:44:44 -06:00
var w io.Writer
2020-09-11 13:03:19 -06:00
c := transpile.NewFsCategory(t.fs, "")
removeOnError := false
switch t.filename {
case "", "-":
2020-10-12 17:44:44 -06:00
w = t.Stdout
default:
removeOnError = true
log.Println("Writing mothball to", t.filename)
2020-10-12 17:44:44 -06:00
outf, err := t.BaseFs.Create(t.filename)
if err != nil {
return err
}
defer outf.Close()
w = outf
2020-09-04 18:28:23 -06:00
}
2020-10-12 17:44:44 -06:00
if err := transpile.Mothball(c, w); err != nil {
if removeOnError {
t.BaseFs.Remove(t.filename)
}
2020-09-04 18:28:23 -06:00
return err
}
return nil
}
2020-09-11 13:03:19 -06:00
// CheckAnswer prints whether an answer is correct.
func (t *T) CheckAnswer() error {
c := transpile.NewFsPuzzle(t.fs)
log.Print(c.Puzzle())
log.Print(t.answer)
_, err := fmt.Fprintf(t.Stdout, `{"Correct":%v}`, c.Answer(t.answer))
return err
2020-09-03 20:04:43 -06:00
}
2020-08-28 17:41:17 -06:00
func main() {
t := &T{
2020-09-11 13:03:19 -06:00
Stdout: os.Stdout,
Stderr: os.Stderr,
Args: os.Args,
2020-09-16 17:54:29 -06:00
BaseFs: afero.NewOsFs(),
2020-09-11 13:03:19 -06:00
}
cmd, err := t.ParseArgs()
if err != nil {
log.Fatal(err)
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if err := cmd(); err != nil {
2020-08-28 17:41:17 -06:00
log.Fatal(err)
}
}