Skip to content

Commit 68ac391

Browse files
authored
feat: allow regex patterns (#29)
1 parent acb4084 commit 68ac391

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

cmd/cmd.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
PARSE_PATTERN_NAME = "pattern"
2020
PARSE_PATTERN_NAME_NAME = "pattern-name"
2121
FOLLOW_NAME = "follow"
22+
DISABLE_REGEX_ESCAPE = "disable-regex-escape"
2223
)
2324

2425
// Default values
@@ -60,7 +61,6 @@ Keyboard shortcuts
6061

6162
var cfgFile string
6263

63-
// TODO: Add option to turn off Regex escaping
6464
// TODO: Add option to set filter
6565
// TODO: Add option to save the logs into file (as the logs are discarded when exceeding buffer size)
6666
// TODO: Allow "infinite" buffer size?
@@ -112,13 +112,14 @@ var cmd = &cobra.Command{
112112
cobra.CheckErr(err)
113113

114114
initialState := state.State{
115-
IsFollowing: viper.GetBool(FOLLOW_NAME),
116-
IsFilterOn: false,
117-
FilterString: "",
118-
ParsingPatternString: pattern,
119-
ParsingPattern: parsingPatternInstance,
120-
InputName: inputName,
121-
IsLogsFirstLine: true,
115+
IsFollowing: viper.GetBool(FOLLOW_NAME),
116+
ShouldEscapeParsingPattern: !viper.GetBool(DISABLE_REGEX_ESCAPE),
117+
IsFilterOn: false,
118+
FilterString: "",
119+
ParsingPatternString: pattern,
120+
ParsingPattern: parsingPatternInstance,
121+
InputName: inputName,
122+
IsLogsFirstLine: true,
122123
}
123124

124125
stateStore := gredux.New(initialState)
@@ -161,6 +162,7 @@ func init() {
161162
cmd.Flags().StringP(PARSE_PATTERN_NAME, "p", "", "parsing pattern see above for details")
162163
cmd.Flags().StringP(PARSE_PATTERN_NAME_NAME, "n", "", "use predefined pattern in config")
163164
cmd.Flags().BoolP(FOLLOW_NAME, "f", false, "turn on following mode which always show latest logs")
165+
cmd.Flags().BoolP(DISABLE_REGEX_ESCAPE, "r", false, "turn off pattern regex escaping")
164166
}
165167

166168
// initConfig reads in config file and ENV variables if set.

state/state.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ type State struct {
3030
DisplayFilterInput bool
3131

3232
// PatternFilter
33-
DisplayPatternInput bool
34-
ParsingPatternString string
35-
ParsingPattern allot.Command
33+
ShouldEscapeParsingPattern bool
34+
DisplayPatternInput bool
35+
ParsingPatternString string
36+
ParsingPattern allot.Command
3637
}

store/filter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package store
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/antonmedv/expr"
67
"github.com/antonmedv/expr/vm"
78
"github.com/auhau/allot"
9+
"strconv"
810
"strings"
911
)
1012

@@ -75,7 +77,13 @@ func buildParameters(match allot.MatchInterface, pattern allot.Command) (map[str
7577
if match != nil {
7678
value, err := match.Integer(parameter.Name())
7779
if err != nil {
78-
return nil, err
80+
// The parameter was not present in the line and hence atoi() fails because it
81+
// tries to convert empty string. So we gonna default to 0.
82+
if errors.Is(err, strconv.ErrSyntax) {
83+
parameters[parameter.Name()] = 0
84+
} else {
85+
return nil, err
86+
}
7987
}
8088

8189
parameters[parameter.Name()] = value
@@ -112,11 +120,12 @@ func IsLineMatching(line string, filter *vm.Program, pattern allot.Command) (fil
112120
// We did not match the line against the parsing pattern so not expected parameters are available
113121
// but we will expose this information as parameter itself to the user.
114122
parameters, err = buildParameters(match, pattern)
115-
parameters[PATTERN_MATCHING_PARAMETER_NAME] = matchError == nil
116123
if err != nil {
117124
return false, false, err
118125
}
119126

127+
parameters[PATTERN_MATCHING_PARAMETER_NAME] = matchError == nil
128+
120129
result, err := expr.Run(filter, parameters)
121130
if err != nil {
122131
return false, false, err

ui/input_pattern.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// makeParsingPattern is a main entry point for UI to set a new parsing pattern
16-
func makeParsingPattern(pattern string) (parsingPatternInstance allot.Command, err error) {
16+
func makeParsingPattern(pattern string, regexEscape bool) (parsingPatternInstance allot.Command, err error) {
1717
// There might be "so invalid syntax" that regex starts panicking
1818
// and not returning error, so we catch everything here just to be sure.
1919
defer func() {
@@ -23,7 +23,11 @@ func makeParsingPattern(pattern string) (parsingPatternInstance allot.Command, e
2323
}
2424
}()
2525

26-
parsingPatternInstance, err = allot.NewWithEscaping(pattern, store.Types)
26+
if regexEscape {
27+
parsingPatternInstance, err = allot.NewWithEscaping(pattern, store.Types)
28+
} else {
29+
parsingPatternInstance, err = allot.New(pattern, store.Types)
30+
}
2731

2832
if err != nil {
2933
return parsingPatternInstance, fmt.Errorf("invalid syntax of parsing pattern: %s", err)
@@ -49,7 +53,7 @@ func patternInputReducer(s gredux.State, action gredux.Action) gredux.State {
4953
st.DisplayPatternInput = false
5054
return st
5155
case actions.ActionNameSetPattern:
52-
pattern, err := makeParsingPattern(action.Data.(string))
56+
pattern, err := makeParsingPattern(action.Data.(string), st.ShouldEscapeParsingPattern)
5357
if err != nil {
5458
st.DisplayError = true
5559
st.ErrorMessage = fmt.Sprint(err)

0 commit comments

Comments
 (0)