-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd_stub.go
102 lines (76 loc) · 2.15 KB
/
cmd_stub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/bobziuchkovski/writ"
"github.com/jkomoros/boardgame/boardgame-util/lib/stub"
)
//stubCmd is named that instead of stub to avoid conflicting with the stub
//package.
type stubCmd struct {
baseSubCommand
Dir string
Fast bool
}
func (s *stubCmd) Run(p writ.Path, positional []string) {
b := s.Base()
if len(positional) != 1 {
p.Last().ExitHelp(errors.New("No gamename provided"))
}
gameName := positional[0]
opt := &stub.Options{
Name: gameName,
}
if !s.Fast {
opt = stub.InteractiveOptions(nil, nil, gameName)
}
files, err := stub.Generate(opt)
if err != nil {
b.errAndQuit("Couldn't generate stubs: " + err.Error())
}
if err := files.Save(s.Dir, false); err != nil {
b.errAndQuit("Couldn't save generated files: " + err.Error())
}
if _, err := os.Stat(gameName); os.IsNotExist(err) {
b.errAndQuit("Unexpected error: game directory didn't exist after saving")
}
cmd := exec.Command("go", "generate")
cmd.Dir = gameName
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
b.errAndQuit("Go generate failed: " + err.Error())
}
fmt.Println("Generated game in " + gameName)
}
func (s *stubCmd) Name() string {
return "stub"
}
func (s *stubCmd) Description() string {
return "Generate starter game with reasonable boilerplate"
}
func (s *stubCmd) Usage() string {
return "GAMENAME"
}
func (s *stubCmd) HelpText() string {
return s.Name() +
` generates a starter game stub based on options provided interactively at the command prompt.
GAMENAME is the base name of the game. It should be short, unique, and be a valid go package name, for example "checkers", "tictactoe".`
}
func (s *stubCmd) WritOptions() []*writ.Option {
return []*writ.Option{
{
Names: []string{"dir", "d"},
Description: "The directory to save the generated game folder in. Defaults to '.'",
Decoder: writ.NewOptionDecoder(&s.Dir),
},
{
Names: []string{"fast", "f"},
Description: "If provided, skips the interactive prompts and just uses the defaults.",
Decoder: writ.NewFlagDecoder(&s.Fast),
Flag: true,
},
}
}