File: main.go

package info (click to toggle)
golang-github-mgutz-logxi 1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: makefile: 7
file content (236 lines) | stat: -rw-r--r-- 5,444 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"time"

	"github.com/mattn/go-colorable"
	"github.com/mgutz/ansi"
	do "gopkg.in/godo.v2"
)

type pair struct {
	description string
	command     string
}

var stdout io.Writer

var promptColor = ansi.ColorCode("cyan+h")
var commentColor = ansi.ColorCode("yellow+h")
var titleColor = ansi.ColorCode("green+h")
var subtitleColor = ansi.ColorCode("black+h")
var normal = ansi.DefaultFG
var wd string

func init() {
	wd, _ = os.Getwd()
	stdout = colorable.NewColorableStdout()
}

func clear() {
	do.Bash("clear")
	// leave a single line at top so the window
	// overlay doesn't have to be exact
	fmt.Fprintln(stdout, "")
}

func pseudoType(s string, color string) {
	if color != "" {
		fmt.Fprint(stdout, color)
	}
	for _, r := range s {
		fmt.Fprint(stdout, string(r))
		time.Sleep(50 * time.Millisecond)
	}
	if color != "" {
		fmt.Fprint(stdout, ansi.Reset)
	}
}

func pseudoTypeln(s string, color string) {
	pseudoType(s, color)
	fmt.Fprint(stdout, "\n")
}

func pseudoPrompt(prompt, s string) {
	pseudoType(prompt, promptColor)
	//fmt.Fprint(stdout, promptFn(prompt))
	pseudoType(s, normal)
}

func intro(title, subtitle string, delay time.Duration) {
	clear()
	pseudoType("\n\n\t"+title+"\n\n", titleColor)
	pseudoType("\t"+subtitle, subtitleColor)
	time.Sleep(delay)
}

func typeCommand(description, commandStr string) {
	clear()
	pseudoTypeln("# "+description, commentColor)
	pseudoType("> ", promptColor)
	pseudoType(commandStr, normal)
	time.Sleep(200 * time.Millisecond)
	fmt.Fprintln(stdout, "")
}

var version = "v1"

func relv(p string) string {
	return filepath.Join(version, p)
}
func absv(p string) string {
	return filepath.Join(wd, version, p)
}

func tasks(p *do.Project) {
	p.Task("bench", nil, func(c *do.Context) {
		c.Run("LOGXI=* go test -bench . -benchmem", do.M{"$in": "v1/bench"})
	})

	p.Task("build", nil, func(c *do.Context) {
		c.Run("go build", do.M{"$in": "v1/cmd/demo"})
	})

	p.Task("linux-build", nil, func(c *do.Context) {
		c.Bash(`
			set -e
			GOOS=linux GOARCH=amd64 go build
			scp -F ~/projects/provision/matcherino/ssh.vagrant.config demo devmaster1:~/.
		`, do.M{"$in": "v1/cmd/demo"})
	})

	p.Task("etcd-set", nil, func(c *do.Context) {
		kv := c.Args.NonFlags()
		if len(kv) != 2 {
			do.Halt(fmt.Errorf("godo etcd-set -- KEY VALUE"))
		}

		c.Run(
			`curl -L http://127.0.0.1:4001/v2/keys/{{.key}} -XPUT -d value="{{.value}}"`,
			do.M{"key": kv[0], "value": kv[1]},
		)
	})

	p.Task("etcd-del", nil, func(c *do.Context) {
		kv := c.Args.Leftover()
		if len(kv) != 1 {
			do.Halt(fmt.Errorf("godo etcd-del -- KEY"))
		}
		c.Run(
			`curl -L http://127.0.0.1:4001/v2/keys/{{.key}} -XDELETE`,
			do.M{"key": kv[0]},
		)
	})

	p.Task("demo", nil, func(c *do.Context) {
		c.Run("go run main.go", do.M{"$in": "v1/cmd/demo"})
	})

	p.Task("demo2", nil, func(c *do.Context) {
		c.Run("go run main.go", do.M{"$in": "v1/cmd/demo2"})
	})

	p.Task("filter", do.S{"build"}, func(c *do.Context) {
		c.Run("go build", do.M{"$in": "v1/cmd/filter"})
		c.Bash("LOGXI=* ../demo/demo | ./filter", do.M{"$in": "v1/cmd/filter"})
	})

	p.Task("gifcast", do.S{"build"}, func(*do.Context) {
		commands := []pair{
			{
				`create a simple app demo`,
				`cat main.ansi`,
			},
			{
				`running demo displays only warnings and errors with context`,
				`demo`,
			},
			{
				`show all log levels`,
				`LOGXI=* demo`,
			},
			{
				`enable/disable loggers with level`,
				`LOGXI=*=ERR,models demo`,
			},
			{
				`create custom 256 colors colorscheme, pink==200`,
				`LOGXI_COLORS=*=black+h,ERR=200+b,key=blue+h demo`,
			},
			{
				`put keys on newline, set time format, less context`,
				`LOGXI=* LOGXI_FORMAT=pretty,maxcol=80,t=04:05.000,context=0 demo`,
			},
			{
				`logxi defaults to fast, unadorned JSON in production`,
				`demo | cat`,
			},
		}

		// setup time for ecorder, user presses enter when ready
		clear()
		do.Prompt("")

		intro(
			"log XI",
			"structured. faster. friendlier.\n\n\n\n\t::mgutz",
			1*time.Second,
		)

		for _, cmd := range commands {
			typeCommand(cmd.description, cmd.command)
			do.Bash(cmd.command, do.M{"$in": "v1/cmd/demo"})
			time.Sleep(3500 * time.Millisecond)
		}

		clear()
		do.Prompt("")
	})

	p.Task("demo-gif", nil, func(c *do.Context) {
		c.Bash(`cp ~/Desktop/demo.gif images`)
	})

	p.Task("bench-allocs", nil, func(c *do.Context) {
		c.Bash(`go test -bench . -benchmem -run=none | grep "allocs\|^Bench"`, do.M{"$in": "v1/bench"})
	}).Description("Runs benchmarks with allocs")

	p.Task("benchjson", nil, func(c *do.Context) {
		c.Bash("go test -bench=BenchmarkLoggerJSON -benchmem", do.M{"$in": "v1/bench"})
	})

	p.Task("test", nil, func(c *do.Context) {
		c.Run("LOGXI=* go test", do.M{"$in": "v1"})
		//Run("LOGXI=* go test -run=TestColors", M{"$in": "v1"})
	})

	p.Task("isolate", do.S{"build"}, func(c *do.Context) {
		c.Bash("LOGXI=* LOGXI_FORMAT=fit,maxcol=80,t=04:05.000,context=2 demo", do.M{"$in": "v1/cmd/demo"})
	})

	p.Task("install", nil, func(c *do.Context) {
		packages := []string{
			"github.com/mattn/go-colorable",
			"github.com/mattn/go-isatty",
			"github.com/mgutz/ansi",
			"github.com/stretchr/testify/assert",

			// needed for benchmarks in bench/
			"github.com/Sirupsen/logrus",
			"gopkg.in/inconshreveable/log15.v2",
		}
		for _, pkg := range packages {
			c.Run("go get -u " + pkg)
		}
	}).Description("Installs dependencies")

}

func main() {
	do.Godo(tasks)
}