Debian Codegraph @

Gododir/main.go

raw
1 package main
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "path/filepath"
8 "time"
9
10 "github.com/mattn/go-colorable"
11 "github.com/mgutz/ansi"
12 do "gopkg.in/godo.v2"
13 )
14
15 type pair struct {
16 description string
17 command string
18 }
19
20 var stdout io.Writer
21
22 var promptColor = ansi.ColorCode("cyan+h")
23 var commentColor = ansi.ColorCode("yellow+h")
24 var titleColor = ansi.ColorCode("green+h")
25 var subtitleColor = ansi.ColorCode("black+h")
26 var normal = ansi.DefaultFG
27 var wd string
28
29 func init() {
30 wd, _ = os.Getwd()
31 stdout = colorable.NewColorableStdout()
32 }
33
34 func clear() {
35 do.Bash("clear")
36 // leave a single line at top so the window
37 // overlay doesn't have to be exact
38 fmt.Fprintln(stdout, "")
39 }
40
41 func pseudoType(s string, color string) {
42 if color != "" {
43 fmt.Fprint(stdout, color)
44 }
45 for _, r := range s {
46 fmt.Fprint(stdout, string(r))
47 time.Sleep(50 * time.Millisecond)
48 }
49 if color != "" {
50 fmt.Fprint(stdout, ansi.Reset)
51 }
52 }
53
54 func pseudoTypeln(s string, color string) {
55 pseudoType(s, color)
56 fmt.Fprint(stdout, "\n")
57 }
58
59 func pseudoPrompt(prompt, s string) {
60 pseudoType(prompt, promptColor)
61 //fmt.Fprint(stdout, promptFn(prompt))
62 pseudoType(s, normal)
63 }
64
65 func intro(title, subtitle string, delay time.Duration) {
66 clear()
67 pseudoType("\n\n\t"+title+"\n\n", titleColor)
68 pseudoType("\t"+subtitle, subtitleColor)
69 time.Sleep(delay)
70 }
71
72 func typeCommand(description, commandStr string) {
73 clear()
74 pseudoTypeln("# "+description, commentColor)
75 pseudoType("> ", promptColor)
76 pseudoType(commandStr, normal)
77 time.Sleep(200 * time.Millisecond)
78 fmt.Fprintln(stdout, "")
79 }
80
81 var version = "v1"
82
83 func relv(p string) string {
84 return filepath.Join(version, p)
85 }
86 func absv(p string) string {
87 return filepath.Join(wd, version, p)
88 }
89
90 func tasks(p *do.Project) {
91 p.Task("bench", nil, func(c *do.Context) {
92 c.Run("LOGXI=* go test -bench . -benchmem", do.M{"$in": "v1/bench"})
93 })
94
95 p.Task("build", nil, func(c *do.Context) {
96 c.Run("go build", do.M{"$in": "v1/cmd/demo"})
97 })
98
99 p.Task("linux-build", nil, func(c *do.Context) {
100 c.Bash(`
101 set -e
102 GOOS=linux GOARCH=amd64 go build
103 scp -F ~/projects/provision/matcherino/ssh.vagrant.config demo devmaster1:~/.
104 `, do.M{"$in": "v1/cmd/demo"})
105 })
106
107 p.Task("etcd-set", nil, func(c *do.Context) {
108 kv := c.Args.NonFlags()
109 if len(kv) != 2 {
110 do.Halt(fmt.Errorf("godo etcd-set -- KEY VALUE"))
111 }
112
113 c.Run(
114 `curl -L http://127.0.0.1:4001/v2/keys/{{.key}} -XPUT -d value="{{.value}}"`,
115 do.M{"key": kv[0], "value": kv[1]},
116 )
117 })
118
119 p.Task("etcd-del", nil, func(c *do.Context) {
120 kv := c.Args.Leftover()
121 if len(kv) != 1 {
122 do.Halt(fmt.Errorf("godo etcd-del -- KEY"))
123 }
124 c.Run(
125 `curl -L http://127.0.0.1:4001/v2/keys/{{.key}} -XDELETE`,
126 do.M{"key": kv[0]},
127 )
128 })
129
130 p.Task("demo", nil, func(c *do.Context) {
131 c.Run("go run main.go", do.M{"$in": "v1/cmd/demo"})
132 })
133
134 p.Task("demo2", nil, func(c *do.Context) {
135 c.Run("go run main.go", do.M{"$in": "v1/cmd/demo2"})
136 })
137
138 p.Task("filter", do.S{"build"}, func(c *do.Context) {
139 c.Run("go build", do.M{"$in": "v1/cmd/filter"})
140 c.Bash("LOGXI=* ../demo/demo | ./filter", do.M{"$in": "v1/cmd/filter"})
141 })
142
143 p.Task("gifcast", do.S{"build"}, func(*do.Context) {
144 commands := []pair{
145 {
146 `create a simple app demo`,
147 `cat main.ansi`,
148 },
149 {
150 `running demo displays only warnings and errors with context`,
151 `demo`,
152 },
153 {
154 `show all log levels`,
155 `LOGXI=* demo`,
156 },
157 {
158 `enable/disable loggers with level`,
159 `LOGXI=*=ERR,models demo`,
160 },
161 {
162 `create custom 256 colors colorscheme, pink==200`,
163 `LOGXI_COLORS=*=black+h,ERR=200+b,key=blue+h demo`,
164 },
165 {
166 `put keys on newline, set time format, less context`,
167 `LOGXI=* LOGXI_FORMAT=pretty,maxcol=80,t=04:05.000,context=0 demo`,
168 },
169 {
170 `logxi defaults to fast, unadorned JSON in production`,
171 `demo | cat`,
172 },
173 }
174
175 // setup time for ecorder, user presses enter when ready
176 clear()
177 do.Prompt("")
178
179 intro(
180 "log XI",
181 "structured. faster. friendlier.\n\n\n\n\t::mgutz",
182 1*time.Second,
183 )
184
185 for _, cmd := range commands {
186 typeCommand(cmd.description, cmd.command)
187 do.Bash(cmd.command, do.M{"$in": "v1/cmd/demo"})
188 time.Sleep(3500 * time.Millisecond)
189 }
190
191 clear()
192 do.Prompt("")
193 })
194
195 p.Task("demo-gif", nil, func(c *do.Context) {
196 c.Bash(`cp ~/Desktop/demo.gif images`)
197 })
198
199 p.Task("bench-allocs", nil, func(c *do.Context) {
200 c.Bash(`go test -bench . -benchmem -run=none | grep "allocs\|^Bench"`, do.M{"$in": "v1/bench"})
201 }).Description("Runs benchmarks with allocs")
202
203 p.Task("benchjson", nil, func(c *do.Context) {
204 c.Bash("go test -bench=BenchmarkLoggerJSON -benchmem", do.M{"$in": "v1/bench"})
205 })
206
207 p.Task("test", nil, func(c *do.Context) {
208 c.Run("LOGXI=* go test", do.M{"$in": "v1"})
209 //Run("LOGXI=* go test -run=TestColors", M{"$in": "v1"})
210 })
211
212 p.Task("isolate", do.S{"build"}, func(c *do.Context) {
213 c.Bash("LOGXI=* LOGXI_FORMAT=fit,maxcol=80,t=04:05.000,context=2 demo", do.M{"$in": "v1/cmd/demo"})
214 })
215
216 p.Task("install", nil, func(c *do.Context) {
217 packages := []string{
218 "github.com/mattn/go-colorable",
219 "github.com/mattn/go-isatty",
220 "github.com/mgutz/ansi",
221 "github.com/stretchr/testify/assert",
222
223 // needed for benchmarks in bench/
224 "github.com/Sirupsen/logrus",
225 "gopkg.in/inconshreveable/log15.v2",
226 }
227 for _, pkg := range packages {
228 c.Run("go get -u " + pkg)
229 }
230 }).Description("Installs dependencies")
231
232 }
233
234 func main() {
235 do.Godo(tasks)
236 }