1a7658fa7f04a591a8460cb6d15b882d79ae81ac

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Use comma separation for filters and output selectors

Diff

 1diff --git a/config/config.go b/config/config.go
 2index 0b7ab673d6401787bf31bd3b2b3ac134f461baeb..d1f5ce535f6e6547215088fcefe12a7c0f3d6ddb 100644
 3--- a/config/config.go
 4+++ b/config/config.go
 5@@ -4,7 +4,6 @@ import (
 6 	"fmt"
 7 	"strings"
 8 
 9-	"github.com/go-logfmt/logfmt"
10 	flags "github.com/jessevdk/go-flags"
11 )
12 
13@@ -29,15 +28,16 @@ type Config struct {
14 
15 type rawConfig struct {
16 	LogLevel     string `long:"level" short:"l" description:"Log level filter. One of DEBUG, INFO, WARN, ERROR, FATAL" default:"INFO"` // nolint:lll
17-	OutputFields string `long:"output" short:"o" description:"Output field selector (space separated)"`
18-	Filter       string `long:"filter" short:"f" description:"Filter fields (key=value space separated)"`
19+	OutputFields string `long:"output" short:"o" description:"Output field selector (comma separated)"`
20+	Filter       string `long:"filter" short:"f" description:"Filter fields (key=value comma separated)"`
21 	NoColor      bool   `long:"no-color" short:"n" description:"Disable color output"`
22 }
23 
24 func Parse() (*Config, error) {
25 	var raw rawConfig
26 
27-	_, err := flags.Parse(&raw)
28+	parser := flags.NewParser(&raw, flags.HelpFlag|flags.PassDoubleDash)
29+	_, err := parser.Parse()
30 	if err != nil {
31 		return nil, err
32 	}
33@@ -78,21 +78,26 @@ func (c *Config) setLevel(level string) error {
34 
35 func (c *Config) setOutputFields(fields string) {
36 	fields = strings.Trim(fields, " ")
37-	c.OutputFields = strings.Split(fields, " ")
38-	if len(c.OutputFields) == 1 && c.OutputFields[0] == "" {
39-		c.OutputFields = nil
40+	if fields == "" {
41+		return
42 	}
43+	c.OutputFields = strings.Split(fields, ",")
44 }
45 
46 func (c *Config) setFilter(filter string) error {
47-	dec := logfmt.NewDecoder(strings.NewReader(filter))
48+	if filter == "" {
49+		return nil
50+	}
51+
52+	filters := strings.Split(filter, ",")
53 	c.Filter = make(map[string]string)
54-	dec.ScanRecord()
55-	for dec.ScanKeyval() {
56-		if dec.Err() != nil {
57-			return dec.Err()
58+	for _, f := range filters {
59+		f = strings.Trim(f, " ")
60+		parts := strings.Split(f, "=")
61+		if len(parts) != 2 {
62+			return fmt.Errorf("invalid filter: %s", f)
63 		}
64-		c.Filter[string(dec.Key())] = string(dec.Value())
65+		c.Filter[parts[0]] = parts[1]
66 	}
67 	return nil
68 }
69diff --git a/main.go b/main.go
70index afa4a0765e3954d304a14e89ae6bddf2bab33ae6..978894aa833d2cf971264f08ccb5efe0704dc2b7 100644
71--- a/main.go
72+++ b/main.go
73@@ -1,9 +1,12 @@
74 package main
75 
76 import (
77+	"errors"
78 	"fmt"
79 	"os"
80 
81+	"github.com/jessevdk/go-flags"
82+
83 	"github.com/TheEdgeOfRage/logfmt/config"
84 	"github.com/TheEdgeOfRage/logfmt/parser"
85 )
86@@ -12,6 +15,10 @@ func main() {
87 	cfg, err := config.Parse()
88 	if err != nil {
89 		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
90+		var flagsErr *flags.Error
91+		if errors.As(err, &flagsErr) && flagsErr.Type == flags.ErrHelp {
92+			os.Exit(0)
93+		}
94 		os.Exit(1)
95 	}
96