mirror of https://github.com/dirtbags/moth.git
Make transpile usage mirror mkpuzzle usage
This commit is contained in:
parent
ff99f131ea
commit
7595f36100
|
@ -22,10 +22,6 @@ type T struct {
|
||||||
Args []string
|
Args []string
|
||||||
BaseFs afero.Fs
|
BaseFs afero.Fs
|
||||||
fs afero.Fs
|
fs afero.Fs
|
||||||
|
|
||||||
// Arguments
|
|
||||||
filename string
|
|
||||||
answer string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command is a function invoked by the user
|
// Command is a function invoked by the user
|
||||||
|
@ -36,14 +32,21 @@ func nothing() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage(w io.Writer) {
|
func usage(w io.Writer) {
|
||||||
fmt.Fprintln(w, "Usage: transpile COMMAND [flags]")
|
fmt.Fprintln(w, " Usage: transpile mothball [FLAGS] [MOTHBALL]")
|
||||||
|
fmt.Fprintln(w, " Compile a mothball")
|
||||||
|
fmt.Fprintln(w, " Usage: inventory [FLAGS]")
|
||||||
|
fmt.Fprintln(w, " Show category inventory")
|
||||||
|
fmt.Fprintln(w, " Usage: puzzle [FLAGS]")
|
||||||
|
fmt.Fprintln(w, " Print puzzle JSON")
|
||||||
|
fmt.Fprintln(w, " Usage: file [FLAGS] FILENAME")
|
||||||
|
fmt.Fprintln(w, " Open a file for a puzzle")
|
||||||
|
fmt.Fprintln(w, " Usage: answer [FLAGS] ANSWER")
|
||||||
|
fmt.Fprintln(w, " Check correctness of an answer")
|
||||||
|
fmt.Fprintln(w, " Usage: markdown [FLAGS]")
|
||||||
|
fmt.Fprintln(w, " Format stdin with markdown")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, " mothball: Compile a mothball")
|
fmt.Fprintln(w, "-dir DIRECTORY")
|
||||||
fmt.Fprintln(w, " inventory: Show category inventory")
|
fmt.Fprintln(w, " Use puzzle in DIRECTORY")
|
||||||
fmt.Fprintln(w, " puzzle: Print puzzle JSON")
|
|
||||||
fmt.Fprintln(w, " file: Open a file for a puzzle")
|
|
||||||
fmt.Fprintln(w, " answer: Check correctness of an answer")
|
|
||||||
fmt.Fprintln(w, " markdown: Format stdin with markdown")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseArgs parses arguments and runs the appropriate action.
|
// ParseArgs parses arguments and runs the appropriate action.
|
||||||
|
@ -62,17 +65,14 @@ func (t *T) ParseArgs() (Command, error) {
|
||||||
switch t.Args[1] {
|
switch t.Args[1] {
|
||||||
case "mothball":
|
case "mothball":
|
||||||
cmd = t.DumpMothball
|
cmd = t.DumpMothball
|
||||||
flags.StringVar(&t.filename, "out", "", "Path to create mothball (empty for stdout)")
|
|
||||||
case "inventory":
|
case "inventory":
|
||||||
cmd = t.PrintInventory
|
cmd = t.PrintInventory
|
||||||
case "puzzle":
|
case "puzzle":
|
||||||
cmd = t.DumpPuzzle
|
cmd = t.DumpPuzzle
|
||||||
case "file":
|
case "file":
|
||||||
cmd = t.DumpFile
|
cmd = t.DumpFile
|
||||||
flags.StringVar(&t.filename, "file", "puzzle.json", "Filename to open")
|
|
||||||
case "answer":
|
case "answer":
|
||||||
cmd = t.CheckAnswer
|
cmd = t.CheckAnswer
|
||||||
flags.StringVar(&t.answer, "answer", "", "Answer to check")
|
|
||||||
case "markdown":
|
case "markdown":
|
||||||
cmd = t.Markdown
|
cmd = t.Markdown
|
||||||
case "help":
|
case "help":
|
||||||
|
@ -92,6 +92,7 @@ func (t *T) ParseArgs() (Command, error) {
|
||||||
} else {
|
} else {
|
||||||
t.fs = t.BaseFs
|
t.fs = t.BaseFs
|
||||||
}
|
}
|
||||||
|
t.Args = flags.Args()
|
||||||
|
|
||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
@ -137,9 +138,14 @@ func (t *T) DumpPuzzle() error {
|
||||||
|
|
||||||
// DumpFile writes a file to the writer.
|
// DumpFile writes a file to the writer.
|
||||||
func (t *T) DumpFile() error {
|
func (t *T) DumpFile() error {
|
||||||
|
filename := "puzzle.json"
|
||||||
|
if len(t.Args) > 0 {
|
||||||
|
filename = t.Args[0]
|
||||||
|
}
|
||||||
|
|
||||||
puzzle := transpile.NewFsPuzzle(t.fs)
|
puzzle := transpile.NewFsPuzzle(t.fs)
|
||||||
|
|
||||||
f, err := puzzle.Open(t.filename)
|
f, err := puzzle.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -154,26 +160,25 @@ func (t *T) DumpFile() error {
|
||||||
// DumpMothball writes a mothball to the writer, or an output file if specified.
|
// DumpMothball writes a mothball to the writer, or an output file if specified.
|
||||||
func (t *T) DumpMothball() error {
|
func (t *T) DumpMothball() error {
|
||||||
var w io.Writer
|
var w io.Writer
|
||||||
|
|
||||||
c := transpile.NewFsCategory(t.fs, "")
|
c := transpile.NewFsCategory(t.fs, "")
|
||||||
|
|
||||||
removeOnError := false
|
filename := ""
|
||||||
switch t.filename {
|
if len(t.Args) == 0 {
|
||||||
case "", "-":
|
|
||||||
w = t.Stdout
|
w = t.Stdout
|
||||||
default:
|
} else {
|
||||||
removeOnError = true
|
filename = t.Args[0]
|
||||||
log.Println("Writing mothball to", t.filename)
|
outf, err := t.BaseFs.Create(filename)
|
||||||
outf, err := t.BaseFs.Create(t.filename)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer outf.Close()
|
defer outf.Close()
|
||||||
w = outf
|
w = outf
|
||||||
|
log.Println("Writing mothball to", filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := transpile.Mothball(c, w); err != nil {
|
if err := transpile.Mothball(c, w); err != nil {
|
||||||
if removeOnError {
|
if filename != "" {
|
||||||
t.BaseFs.Remove(t.filename)
|
t.BaseFs.Remove(filename)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -182,10 +187,12 @@ func (t *T) DumpMothball() error {
|
||||||
|
|
||||||
// CheckAnswer prints whether an answer is correct.
|
// CheckAnswer prints whether an answer is correct.
|
||||||
func (t *T) CheckAnswer() error {
|
func (t *T) CheckAnswer() error {
|
||||||
|
answer := ""
|
||||||
|
if len(t.Args) > 0 {
|
||||||
|
answer = t.Args[0]
|
||||||
|
}
|
||||||
c := transpile.NewFsPuzzle(t.fs)
|
c := transpile.NewFsPuzzle(t.fs)
|
||||||
log.Print(c.Puzzle())
|
_, err := fmt.Fprintf(t.Stdout, `{"Correct":%v}`, c.Answer(answer))
|
||||||
log.Print(t.answer)
|
|
||||||
_, err := fmt.Fprintf(t.Stdout, `{"Correct":%v}`, c.Answer(t.answer))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ func TestTranspilerEverything(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.Reset()
|
stdout.Reset()
|
||||||
if err := tp.Run("file", "-dir=cat0/1", "-file=moo.txt"); err != nil {
|
if err := tp.Run("file", "-dir=cat0/1", "moo.txt"); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if stdout.String() != "Moo." {
|
if stdout.String() != "Moo." {
|
||||||
|
@ -89,7 +89,7 @@ func TestTranspilerEverything(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.Reset()
|
stdout.Reset()
|
||||||
if err := tp.Run("answer", "-dir=cat0/1", "-answer=YAML answer"); err != nil {
|
if err := tp.Run("answer", "-dir=cat0/1", "YAML answer"); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if stdout.String() != `{"Correct":true}` {
|
if stdout.String() != `{"Correct":true}` {
|
||||||
|
@ -117,7 +117,7 @@ func TestMothballs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.Reset()
|
stdout.Reset()
|
||||||
if err := tp.Run("mothball", "-dir=unbroken", "-out=unbroken.mb"); err != nil {
|
if err := tp.Run("mothball", "-dir=unbroken", "unbroken.mb"); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -195,7 +195,7 @@ func TestFilesystem(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.Reset()
|
stdout.Reset()
|
||||||
if err := tp.Run("file", "-dir=testdata/cat1/1", "-file=moo.txt"); err != nil {
|
if err := tp.Run("file", "-dir=testdata/cat1/1", "moo.txt"); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stdout.String(), "Moo.") {
|
if !strings.Contains(stdout.String(), "Moo.") {
|
||||||
|
|
Loading…
Reference in New Issue