Skip to content

Commit

Permalink
make the tests work by doing config properly
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme authored and Antoine Grondin committed Sep 15, 2022
1 parent 59a991c commit 651e674
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 198 deletions.
27 changes: 16 additions & 11 deletions cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"os"
"os/signal"

"github.com/aybabtme/humanlog"
"github.com/aybabtme/rgbterm"
"github.com/fatih/color"
"github.com/humanlogio/humanlog"
"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -74,13 +75,13 @@ func newApp() *cli.App {
truncateLength := cli.IntFlag{
Name: "truncate-length",
Usage: "truncate values that are longer than this length",
Value: *defaultConfig.TruncateLength,
Value: *config.DefaultConfig.TruncateLength,
}

colorFlag := cli.StringFlag{
Name: "color",
Usage: "specify color mode: auto, on/force, off",
Value: *defaultConfig.ColorFlag,
Value: *config.DefaultConfig.ColorFlag,
}

lightBg := cli.BoolFlag{
Expand Down Expand Up @@ -134,21 +135,21 @@ func newApp() *cli.App {

app.Action = func(c *cli.Context) error {

configFilepath, err := getDefaultConfigFilepath()
configFilepath, err := config.GetDefaultConfigFilepath()
if err != nil {
return fmt.Errorf("looking up config file path: %v", err)
}
// read config
var cfg *Config
var cfg *config.Config
if c.IsSet(configFlag.Name) {
configFilepath = c.String(configFlag.Name)
cfgFromFlag, err := readConfigFile(configFilepath, defaultConfig)
cfgFromFlag, err := config.ReadConfigFile(configFilepath, &config.DefaultConfig)
if err != nil {
return fmt.Errorf("reading --config file %q: %v", configFilepath, err)
}
cfg = cfgFromFlag
} else {
cfgFromDir, err := readConfigFile(configFilepath, defaultConfig)
cfgFromDir, err := config.ReadConfigFile(configFilepath, &config.DefaultConfig)
if err != nil {
return fmt.Errorf("reading default config file: %v", err)
}
Expand Down Expand Up @@ -205,14 +206,14 @@ func newApp() *cli.App {
signal.Ignore(os.Interrupt)
}

colorMode, err := GrokColorMode(*cfg.ColorFlag)
colorMode, err := config.GrokColorMode(*cfg.ColorFlag)
if err != nil {
return fmt.Errorf("invalid --color=%q: %v", *cfg.ColorFlag, err)
}
switch colorMode {
case ColorModeOff:
case config.ColorModeOff:
color.NoColor = true
case ColorModeOn:
case config.ColorModeOn:
color.NoColor = false
default:
// 'Auto' default is applied as a global variable initializer function, so nothing
Expand All @@ -223,7 +224,7 @@ func newApp() *cli.App {
fatalf(c, "can only use one of %q and %q", skipFlag.Name, keepFlag.Name)
}

opts := cfg.toHandlerOptions()
opts := humanlog.HandlerOptionsFrom(*cfg)

log.Print("reading stdin...")
if err := humanlog.Scanner(os.Stdin, colorable.NewColorableStdout(), opts); err != nil {
Expand All @@ -233,3 +234,7 @@ func newApp() *cli.App {
}
return app
}

func ptr[T any](v T) *T {
return &v
}
116 changes: 116 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package humanlog

import (
"fmt"

"github.com/fatih/color"
"github.com/humanlogio/humanlog/internal/pkg/config"
)

var DefaultPalette = Palette{
Expand Down Expand Up @@ -39,3 +42,116 @@ type Palette struct {
FatalLevelColor *color.Color
UnknownLevelColor *color.Color
}

func PaletteFrom(pl config.TextPalette) (*Palette, error) {
var err error
out := &Palette{}
out.KeyColor, err = attributesToColor(pl.KeyColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "key", err)
}
out.ValColor, err = attributesToColor(pl.ValColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "val", err)
}
out.TimeLightBgColor, err = attributesToColor(pl.TimeLightBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "time_light_bg", err)
}
out.TimeDarkBgColor, err = attributesToColor(pl.TimeDarkBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "time_dark_bg", err)
}
out.MsgLightBgColor, err = attributesToColor(pl.MsgLightBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "msg_light_bg", err)
}
out.MsgAbsentLightBgColor, err = attributesToColor(pl.MsgAbsentLightBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "msg_absent_light_bg", err)
}
out.MsgDarkBgColor, err = attributesToColor(pl.MsgDarkBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "msg_dark_bg", err)
}
out.MsgAbsentDarkBgColor, err = attributesToColor(pl.MsgAbsentDarkBgColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "msg_absent_dark_bg", err)
}
out.DebugLevelColor, err = attributesToColor(pl.DebugLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "debug_level", err)
}
out.InfoLevelColor, err = attributesToColor(pl.InfoLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "info_level", err)
}
out.WarnLevelColor, err = attributesToColor(pl.WarnLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "warn_level", err)
}
out.ErrorLevelColor, err = attributesToColor(pl.ErrorLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "error_level", err)
}
out.PanicLevelColor, err = attributesToColor(pl.PanicLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "panic_level", err)
}
out.FatalLevelColor, err = attributesToColor(pl.FatalLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "fatal_level", err)
}
out.UnknownLevelColor, err = attributesToColor(pl.UnknownLevelColor)
if err != nil {
return nil, fmt.Errorf("in palette key %q, %v", "unknown_level", err)
}
return out, err
}

func attributesToColor(names []string) (*color.Color, error) {
attrs := make([]color.Attribute, 0, len(names))
for _, name := range names {
attr, ok := colorAttributeIndex[name]
if !ok {
return nil, fmt.Errorf("color %q isn't supported", name)
}
attrs = append(attrs, attr)
}
return color.New(attrs...), nil
}

var colorAttributeIndex = map[string]color.Attribute{
"fg_black": color.FgBlack,
"fg_red": color.FgRed,
"fg_green": color.FgGreen,
"fg_yellow": color.FgYellow,
"fg_blue": color.FgBlue,
"fg_magenta": color.FgMagenta,
"fg_cyan": color.FgCyan,
"fg_white": color.FgWhite,
"fg_hi_black": color.FgHiBlack,
"fg_hi_red": color.FgHiRed,
"fg_hi_green": color.FgHiGreen,
"fg_hi_yellow": color.FgHiYellow,
"fg_hi_blue": color.FgHiBlue,
"fg_hi_magenta": color.FgHiMagenta,
"fg_hi_cyan": color.FgHiCyan,
"fg_hi_white": color.FgHiWhite,
"bg_black": color.BgBlack,
"bg_red": color.BgRed,
"bg_green": color.BgGreen,
"bg_yellow": color.BgYellow,
"bg_blue": color.BgBlue,
"bg_magenta": color.BgMagenta,
"bg_cyan": color.BgCyan,
"bg_white": color.BgWhite,
"bg_hi_black": color.BgHiBlack,
"bg_hi_red": color.BgHiRed,
"bg_hi_green": color.BgHiGreen,
"bg_hi_yellow": color.BgHiYellow,
"bg_hi_blue": color.BgHiBlue,
"bg_hi_magenta": color.BgHiMagenta,
"bg_hi_cyan": color.BgHiCyan,
"bg_hi_white": color.BgHiWhite,
}
13 changes: 8 additions & 5 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/humanlogio/humanlog/internal/pkg/config"
)

func TestHarness(t *testing.T) {
Expand All @@ -30,14 +32,15 @@ func TestHarness(t *testing.T) {
if err != nil {
t.Fatalf("reading expected output: %v", err)
}
optsjson, err := ioutil.ReadFile(filepath.Join(root, de.Name(), "opts.json"))
cfgjson, err := ioutil.ReadFile(filepath.Join(root, de.Name(), "config.json"))
if err != nil {
t.Fatalf("reading options: %v", err)
t.Fatalf("reading config: %v", err)
}
opts := new(HandlerOptions)
if err := json.Unmarshal(optsjson, &opts); err != nil {
t.Fatalf("unmarshaling options: %v", err)
var cfg config.Config
if err := json.Unmarshal(cfgjson, &cfg); err != nil {
t.Fatalf("unmarshaling config: %v", err)
}
opts := HandlerOptionsFrom(cfg)
gotw := bytes.NewBuffer(nil)
err = Scanner(bytes.NewReader(input), gotw, opts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aybabtme/humanlog
module github.com/humanlogio/humanlog

go 1.19

Expand Down
61 changes: 61 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package humanlog

import (
"log"
"time"

"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/kr/logfmt"
)

Expand Down Expand Up @@ -45,6 +47,54 @@ type HandlerOptions struct {
Palette Palette
}

var _ = HandlerOptionsFrom(config.DefaultConfig) // ensure it's valid

func HandlerOptionsFrom(cfg config.Config) *HandlerOptions {
opts := DefaultOptions
if cfg.Skip != nil {
opts.Skip = sliceToSet(cfg.Skip)
}
if cfg.Keep != nil {
opts.Keep = sliceToSet(cfg.Keep)
}
if cfg.TimeFields != nil {
opts.TimeFields = *cfg.TimeFields
}
if cfg.MessageFields != nil {
opts.MessageFields = *cfg.MessageFields
}
if cfg.LevelFields != nil {
opts.LevelFields = *cfg.LevelFields
}
if cfg.SortLongest != nil {
opts.SortLongest = *cfg.SortLongest
}
if cfg.SkipUnchanged != nil {
opts.SkipUnchanged = *cfg.SkipUnchanged
}
if cfg.Truncates != nil {
opts.Truncates = *cfg.Truncates
}
if cfg.LightBg != nil {
opts.LightBg = *cfg.LightBg
}
if cfg.TruncateLength != nil {
opts.TruncateLength = *cfg.TruncateLength
}
if cfg.TimeFormat != nil {
opts.TimeFormat = *cfg.TimeFormat
}
if cfg.Palette != nil {
pl, err := PaletteFrom(*cfg.Palette)
if err != nil {
log.Printf("invalid palette, using default one: %v", err)
} else {
opts.Palette = *pl
}
}
return opts
}

func (h *HandlerOptions) shouldShowKey(key string) bool {
if len(h.Keep) != 0 {
if _, keep := h.Keep[key]; keep {
Expand All @@ -67,3 +117,14 @@ func (h *HandlerOptions) shouldShowUnchanged(key string) bool {
}
return false
}

func sliceToSet(arr *[]string) map[string]struct{} {
if arr == nil {
return nil
}
out := make(map[string]struct{})
for _, key := range *arr {
out[key] = struct{}{}
}
return out
}
Loading

0 comments on commit 651e674

Please sign in to comment.