Skip to content

Commit 130ca65

Browse files
aybabtmeAntoine Grondin
authored andcommitted
fix tests after refactor
1 parent 2ba4ea1 commit 130ca65

File tree

17 files changed

+529
-496
lines changed

17 files changed

+529
-496
lines changed

cmd/humanlog/main.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
"os/signal"
88

99
"github.com/aybabtme/rgbterm"
10-
"github.com/fatih/color"
1110
"github.com/humanlogio/humanlog"
1211
"github.com/humanlogio/humanlog/internal/pkg/config"
13-
"github.com/humanlogio/humanlog/internal/pkg/sink"
12+
"github.com/humanlogio/humanlog/internal/pkg/sink/stdiosink"
1413
"github.com/mattn/go-colorable"
1514
"github.com/urfave/cli"
1615
)
@@ -82,7 +81,7 @@ func newApp() *cli.App {
8281
colorFlag := cli.StringFlag{
8382
Name: "color",
8483
Usage: "specify color mode: auto, on/force, off",
85-
Value: *config.DefaultConfig.ColorFlag,
84+
Value: stdiosink.DefaultStdioOpts.ColorFlag,
8685
}
8786

8887
lightBg := cli.BoolFlag{
@@ -93,7 +92,7 @@ func newApp() *cli.App {
9392
timeFormat := cli.StringFlag{
9493
Name: "time-format",
9594
Usage: "output time format, see https://golang.org/pkg/time/ for details",
96-
Value: sink.DefaultStdioOpts.TimeFormat,
95+
Value: stdiosink.DefaultStdioOpts.TimeFormat,
9796
}
9897

9998
ignoreInterrupts := cli.BoolFlag{
@@ -178,7 +177,7 @@ func newApp() *cli.App {
178177
cfg.TimeFormat = ptr(c.String(timeFormat.Name))
179178
}
180179
if c.IsSet(colorFlag.Name) {
181-
cfg.ColorFlag = ptr(c.String(colorFlag.Name))
180+
cfg.ColorMode = ptr(c.String(colorFlag.Name))
182181
}
183182
if c.IsSet(skipFlag.Name) {
184183
cfg.Skip = ptr([]string(skip))
@@ -207,31 +206,17 @@ func newApp() *cli.App {
207206
signal.Ignore(os.Interrupt)
208207
}
209208

210-
colorMode, err := config.GrokColorMode(*cfg.ColorFlag)
211-
if err != nil {
212-
return fmt.Errorf("invalid --color=%q: %v", *cfg.ColorFlag, err)
213-
}
214-
switch colorMode {
215-
case config.ColorModeOff:
216-
color.NoColor = true
217-
case config.ColorModeOn:
218-
color.NoColor = false
219-
default:
220-
// 'Auto' default is applied as a global variable initializer function, so nothing
221-
// to do here.
222-
}
223-
224209
if len(*cfg.Skip) > 0 && len(*cfg.Keep) > 0 {
225210
fatalf(c, "can only use one of %q and %q", skipFlag.Name, keepFlag.Name)
226211
}
227212

228-
sinkOpts, errs := sink.StdioOptsFrom(*cfg)
213+
sinkOpts, errs := stdiosink.StdioOptsFrom(*cfg)
229214
if len(errs) > 0 {
230215
for _, err := range errs {
231216
log.Printf("config error: %v", err)
232217
}
233218
}
234-
sink := sink.NewStdio(colorable.NewColorableStdout(), sinkOpts)
219+
sink := stdiosink.NewStdio(colorable.NewColorableStdout(), sinkOpts)
235220
handlerOpts := humanlog.HandlerOptionsFrom(*cfg)
236221

237222
log.Print("reading stdin...")

e2e_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/humanlogio/humanlog/internal/pkg/config"
12-
"github.com/humanlogio/humanlog/internal/pkg/sink"
12+
"github.com/humanlogio/humanlog/internal/pkg/sink/stdiosink"
1313
)
1414

1515
func TestHarness(t *testing.T) {
@@ -42,11 +42,11 @@ func TestHarness(t *testing.T) {
4242
t.Fatalf("unmarshaling config: %v", err)
4343
}
4444
gotw := bytes.NewBuffer(nil)
45-
sinkOpts, errs := sink.StdioOptsFrom(cfg)
45+
sinkOpts, errs := stdiosink.StdioOptsFrom(cfg)
4646
if len(errs) > 1 {
4747
t.Fatalf("errs=%v", errs)
4848
}
49-
s := sink.NewStdio(gotw, sinkOpts)
49+
s := stdiosink.NewStdio(gotw, sinkOpts)
5050
err = Scanner(bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
5151
if err != nil {
5252
t.Fatalf("scanning input: %v", err)

internal/pkg/config/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var DefaultConfig = Config{
2222
SkipUnchanged: ptr(true),
2323
Truncates: ptr(true),
2424
LightBg: ptr(false),
25-
ColorFlag: ptr("auto"),
25+
ColorMode: ptr("auto"),
2626
TruncateLength: ptr(15),
2727
TimeFormat: ptr(time.Stamp),
2828
Interrupt: ptr(false),
@@ -94,7 +94,7 @@ type Config struct {
9494
SkipUnchanged *bool `json:"skip-unchanged"`
9595
Truncates *bool `json:"truncates"`
9696
LightBg *bool `json:"light-bg"`
97-
ColorFlag *string `json:"color-mode"`
97+
ColorMode *string `json:"color-mode"`
9898
TruncateLength *int `json:"truncate-length"`
9999
TimeFormat *string `json:"time-format"`
100100
Palette *TextPalette `json:"palette"`
@@ -130,8 +130,8 @@ func (cfg Config) populateEmpty(other *Config) *Config {
130130
if out.LightBg == nil && other.LightBg != nil {
131131
out.LightBg = other.LightBg
132132
}
133-
if out.ColorFlag == nil && other.ColorFlag != nil {
134-
out.ColorFlag = other.ColorFlag
133+
if out.ColorMode == nil && other.ColorMode != nil {
134+
out.ColorMode = other.ColorMode
135135
}
136136
if out.TruncateLength == nil && other.TruncateLength != nil {
137137
out.TruncateLength = other.TruncateLength
Lines changed: 1 addition & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -1,240 +1,12 @@
1-
package sink
1+
package stdiosink
22

33
import (
4-
"bytes"
54
"fmt"
6-
"io"
7-
"sort"
8-
"strings"
9-
"text/tabwriter"
10-
"time"
115

126
"github.com/fatih/color"
137
"github.com/humanlogio/humanlog/internal/pkg/config"
14-
"github.com/humanlogio/humanlog/internal/pkg/model"
158
)
169

17-
var (
18-
eol = [...]byte{'\n'}
19-
)
20-
21-
type Stdio struct {
22-
w io.Writer
23-
opts StdioOpts
24-
25-
lastEvent *model.Event
26-
lastKVs map[string]string
27-
}
28-
29-
type StdioOpts struct {
30-
Skip map[string]struct{}
31-
Keep map[string]struct{}
32-
SkipUnchanged bool
33-
SortLongest bool
34-
TimeFormat string
35-
Truncates bool
36-
TruncateLength int
37-
38-
LightBg bool
39-
Palette Palette
40-
}
41-
42-
var DefaultStdioOpts = StdioOpts{
43-
SkipUnchanged: true,
44-
SortLongest: true,
45-
Truncates: true,
46-
LightBg: false,
47-
TruncateLength: 15,
48-
TimeFormat: time.Stamp,
49-
50-
Palette: DefaultPalette,
51-
}
52-
53-
func StdioOptsFrom(cfg config.Config) (StdioOpts, []error) {
54-
var errs []error
55-
opts := DefaultStdioOpts
56-
if cfg.Skip != nil {
57-
opts.Skip = sliceToSet(cfg.Skip)
58-
}
59-
if cfg.Keep != nil {
60-
opts.Keep = sliceToSet(cfg.Keep)
61-
}
62-
if cfg.SortLongest != nil {
63-
opts.SortLongest = *cfg.SortLongest
64-
}
65-
if cfg.SkipUnchanged != nil {
66-
opts.SkipUnchanged = *cfg.SkipUnchanged
67-
}
68-
if cfg.Truncates != nil {
69-
opts.Truncates = *cfg.Truncates
70-
}
71-
if cfg.LightBg != nil {
72-
opts.LightBg = *cfg.LightBg
73-
}
74-
if cfg.TruncateLength != nil {
75-
opts.TruncateLength = *cfg.TruncateLength
76-
}
77-
if cfg.TimeFormat != nil {
78-
opts.TimeFormat = *cfg.TimeFormat
79-
}
80-
if cfg.Palette != nil {
81-
pl, err := PaletteFrom(*cfg.Palette)
82-
if err != nil {
83-
errs = append(errs, fmt.Errorf("invalid palette, using default one: %v", err))
84-
} else {
85-
opts.Palette = *pl
86-
}
87-
}
88-
return opts, errs
89-
}
90-
91-
var _ Sink = (*Stdio)(nil)
92-
93-
func NewStdio(w io.Writer, opts StdioOpts) *Stdio {
94-
return &Stdio{
95-
w: w,
96-
opts: opts,
97-
}
98-
}
99-
100-
func (std *Stdio) Receive(ev *model.Event) error {
101-
if ev.Structured == nil {
102-
if _, err := std.w.Write(ev.Raw); err != nil {
103-
return err
104-
}
105-
return nil
106-
}
107-
data := ev.Structured
108-
109-
buf := bytes.NewBuffer(nil)
110-
out := tabwriter.NewWriter(buf, 0, 1, 0, '\t', 0)
111-
112-
var (
113-
msgColor *color.Color
114-
msgAbsentColor *color.Color
115-
)
116-
if std.opts.LightBg {
117-
msgColor = std.opts.Palette.MsgLightBgColor
118-
msgAbsentColor = std.opts.Palette.MsgAbsentLightBgColor
119-
} else {
120-
msgColor = std.opts.Palette.MsgDarkBgColor
121-
msgAbsentColor = std.opts.Palette.MsgAbsentDarkBgColor
122-
}
123-
var msg string
124-
if data.Msg == "" {
125-
msg = msgAbsentColor.Sprint("<no msg>")
126-
} else {
127-
msg = msgColor.Sprint(data.Msg)
128-
}
129-
130-
lvl := strings.ToUpper(data.Level)[:imin(4, len(data.Level))]
131-
var level string
132-
switch data.Level {
133-
case "debug":
134-
level = std.opts.Palette.DebugLevelColor.Sprint(lvl)
135-
case "info":
136-
level = std.opts.Palette.InfoLevelColor.Sprint(lvl)
137-
case "warn", "warning":
138-
level = std.opts.Palette.WarnLevelColor.Sprint(lvl)
139-
case "error":
140-
level = std.opts.Palette.ErrorLevelColor.Sprint(lvl)
141-
case "fatal", "panic":
142-
level = std.opts.Palette.FatalLevelColor.Sprint(lvl)
143-
default:
144-
level = std.opts.Palette.UnknownLevelColor.Sprint(lvl)
145-
}
146-
147-
var timeColor *color.Color
148-
if std.opts.LightBg {
149-
timeColor = std.opts.Palette.TimeLightBgColor
150-
} else {
151-
timeColor = std.opts.Palette.TimeDarkBgColor
152-
}
153-
154-
_, _ = fmt.Fprintf(out, "%s |%s| %s\t %s",
155-
timeColor.Sprint(data.Time.Format(std.opts.TimeFormat)),
156-
level,
157-
msg,
158-
strings.Join(std.joinKVs(data, "="), "\t "),
159-
)
160-
161-
if err := out.Flush(); err != nil {
162-
return err
163-
}
164-
165-
buf.Write(eol[:])
166-
167-
if _, err := buf.WriteTo(std.w); err != nil {
168-
return err
169-
}
170-
171-
kvs := make(map[string]string, len(data.KVs))
172-
for _, kv := range data.KVs {
173-
kvs[kv.Key] = kv.Value
174-
}
175-
std.lastEvent = ev
176-
std.lastKVs = kvs
177-
return nil
178-
}
179-
180-
func (std *Stdio) joinKVs(data *model.Structured, sep string) []string {
181-
182-
kv := make([]string, 0, len(data.KVs))
183-
for _, pair := range data.KVs {
184-
k, v := pair.Key, pair.Value
185-
if !std.opts.shouldShowKey(k) {
186-
continue
187-
}
188-
189-
if std.opts.SkipUnchanged {
190-
if lastV, ok := std.lastKVs[k]; ok && lastV == v && !std.opts.shouldShowUnchanged(k) {
191-
continue
192-
}
193-
}
194-
kstr := std.opts.Palette.KeyColor.Sprint(k)
195-
196-
var vstr string
197-
if std.opts.Truncates && len(v) > std.opts.TruncateLength {
198-
vstr = v[:std.opts.TruncateLength] + "..."
199-
} else {
200-
vstr = v
201-
}
202-
vstr = std.opts.Palette.ValColor.Sprint(vstr)
203-
kv = append(kv, kstr+sep+vstr)
204-
}
205-
206-
sort.Strings(kv)
207-
208-
if std.opts.SortLongest {
209-
sort.Stable(byLongest(kv))
210-
}
211-
212-
return kv
213-
}
214-
215-
func (opts *StdioOpts) shouldShowKey(key string) bool {
216-
if len(opts.Keep) != 0 {
217-
if _, keep := opts.Keep[key]; keep {
218-
return true
219-
}
220-
}
221-
if len(opts.Skip) != 0 {
222-
if _, skip := opts.Skip[key]; skip {
223-
return false
224-
}
225-
}
226-
return true
227-
}
228-
229-
func (opts *StdioOpts) shouldShowUnchanged(key string) bool {
230-
if len(opts.Keep) != 0 {
231-
if _, keep := opts.Keep[key]; keep {
232-
return true
233-
}
234-
}
235-
return false
236-
}
237-
23810
var DefaultPalette = Palette{
23911
KeyColor: color.New(color.FgGreen),
24012
ValColor: color.New(color.FgHiWhite),
@@ -383,27 +155,3 @@ var colorAttributeIndex = map[string]color.Attribute{
383155
"bg_hi_cyan": color.BgHiCyan,
384156
"bg_hi_white": color.BgHiWhite,
385157
}
386-
387-
type byLongest []string
388-
389-
func (s byLongest) Len() int { return len(s) }
390-
func (s byLongest) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
391-
func (s byLongest) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
392-
393-
func imin(a, b int) int {
394-
if a < b {
395-
return a
396-
}
397-
return b
398-
}
399-
400-
func sliceToSet(arr *[]string) map[string]struct{} {
401-
if arr == nil {
402-
return nil
403-
}
404-
out := make(map[string]struct{})
405-
for _, key := range *arr {
406-
out[key] = struct{}{}
407-
}
408-
return out
409-
}

0 commit comments

Comments
 (0)