Skip to content

Commit

Permalink
Add initial support for lnav
Browse files Browse the repository at this point in the history
  • Loading branch information
5nord committed May 12, 2023
1 parent 96fb801 commit 7c39c92
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
69 changes: 66 additions & 3 deletions describe_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/nokia/ntt/internal/lnav"
"github.com/nokia/ntt/k3/log"
"github.com/spf13/cobra"
)
Expand All @@ -18,9 +19,11 @@ var (
Hidden: true,
RunE: describe,
}
formatLnav bool
)

func init() {
DescribeEventsCommand.Flags().BoolVar(&formatLnav, "lnav", false, "Output in lnav format")
RootCommand.AddCommand(DescribeEventsCommand)
}

Expand All @@ -47,22 +50,82 @@ func describe(cmd *cobra.Command, args []string) error {
return strings.ToLower(events[i].Name) < strings.ToLower(events[j].Name)
})

switch Format() {
case "json":
switch {
case formatLnav:
return OutputLnavSpec(events)
case Format() == "json":
return OutputJSON(events)
default:
return OutputText(events)

}
}

func OutputLnavSpec(events []Event) error {

regexes := make(map[string]lnav.Regex)
for _, e := range events {
pattern := fmt.Sprintf(`^(?<timestamp>\d{8}T\d{6}\.\d{6})\|(?<eventtype>%s)\|(?<component>(\?|\w+))(=(?<location>[^\|]*))?`, e.Name)
for range e.Fields {
pattern += fmt.Sprintf(`\|.*`)
}
pattern += "$"
regexes[e.Name] = lnav.Regex{
Pattern: pattern,
}
}

formats := map[string]lnav.Format{
"ttcn3_log": {
Title: "TTCN3 Log Format",
Description: "Format describing TTCN3 log files.",
URL: []string{"https://pkg.go.dev/github.com/nokia/ntt/k3/log#Category"},
TimestampFormat: []string{"%Y%m%dT%H%M%S.%f"},
OrderedByTime: true,
Regex: regexes,
OPidField: "eventtype",
LevelField: "verdict",
Level: map[string]string{
"error": "error|[A-Z]{4}|DIV0|UTF8|inconc",
"trace": "none",
"notice": "pass",
"warning": "fail",
},
Value: map[string]lnav.Value{
"eventtype": {
Kind: "string",
Identifier: true,
Hidden: false,
Description: "The k3r event type",
},
"component": {Kind: "string", Identifier: true},
"location": {Kind: "string", Identifier: false},
"timestamp": {Kind: "string", Identifier: false},
},
},
}

spec := lnav.Spec{
Schema: "https://lnav.org/schemas/format-v1.schema.json",
Formats: formats,
}

b, err := json.MarshalIndent(spec, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}

func OutputJSON(events []Event) error {
b, err := json.MarshalIndent(events, "", " ")
if err != nil {
return err
}

fmt.Println(string(b))
return err
return nil
}

func OutputText(events []Event) error {
Expand Down
30 changes: 30 additions & 0 deletions internal/lnav/lnav.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lnav

type Spec struct {
Schema string `json:"$schema"`
Formats map[string]Format `json:"inline"`
}

type Format struct {
Title string `json:"title"`
Description string `json:"description"`
URL []string `json:"url"`
Regex map[string]Regex `json:"regex"`
TimestampFormat []string `json:"timestamp_format"`
OrderedByTime bool `json:"ordered_by_time"`
OPidField string `json:"opid_field"`
LevelField string `json:"level_field"`
Level map[string]string `json:"level"`
Value map[string]Value `json:"value"`
}

type Regex struct {
Pattern string `json:"pattern"`
}

type Value struct {
Kind string `json:"kind"`
Identifier bool `json:"identifier"`
Hidden bool `json:"hidden,omitempty"`
Description string `json:"description,omitempty"`
}

0 comments on commit 7c39c92

Please sign in to comment.