Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW json parser #10

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ require (
github.com/gertd/go-pluralize v0.2.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.8.0 // indirect
golang.org/x/text v0.8.0 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.1 h1:SEy2xmstIphdPwNBUi7uhvjyj
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.1/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInmMgOsuGwdjjVkEIde0OtY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ var showVersion = flag.Bool("version", false, "Show version")

func main() {
input := ""
jsonInput := false
output := ""
mode := ""

flag.StringVar(&input, "i", "", "Input terraform log file")
flag.StringVar(&input, "i", "", "Input terraform text log file")
flag.BoolVar(&jsonInput, "j", false, "Input terraform file is json")
flag.StringVar(&output, "o", "", "Output directory")
flag.StringVar(&mode, "m", "markdown", "Output format, allowed values are `markdown`, `oav` and `azapi`")

Expand All @@ -34,6 +36,7 @@ func main() {
mode = "markdown"
}
}

if input == "" {
flag.Parse()
if *showHelp {
Expand All @@ -45,9 +48,10 @@ func main() {
os.Exit(0)
}
}

if input == "" {
flag.Usage()
log.Fatalf("[ERROR] input file is required")
log.Fatalf("[ERROR] input text or json file is required")
}

if output == "" {
Expand All @@ -58,14 +62,19 @@ func main() {
log.Printf("[INFO] output directory: %s", output)
log.Printf("[INFO] output format: %s", mode)

traces, err := trace.RequestTracesFromFile(input)
// switch between different file parser implementations.
traceFormat := trace.TextParser
if jsonInput {
traceFormat = trace.JsonParser
}

traces, err := trace.NewRequestTraceParser(traceFormat).ParseFromFile(input)
if err != nil {
log.Fatalf("[ERROR] failed to parse request traces: %v", err)
}

for _, t := range traces {
out := trace.VerifyRequestTrace(t)
if len(out) > 0 {
if out := trace.VerifyRequestTrace(t); len(out) > 0 {
log.Printf("[WARN] verification failed: url %s\n%s", t.Url, strings.Join(out, "\n"))
}
}
Expand Down
18 changes: 18 additions & 0 deletions rawlog/raw_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ func NewRawLog(message string) (*RawLog, error) {
}
return nil, fmt.Errorf("failed to parse log message: %s", message)
}

func NewRawLogJson(message map[string]interface{}) (*RawLog, error) {
timeStamp := message["@timestamp"]
if timeStamp == nil {
return nil, fmt.Errorf("failed to parse timestamp from log message : %v", message)
}

t, err := time.Parse(time.RFC3339, timeStamp.(string))
if err != nil {
return nil, fmt.Errorf("failed to parse timestamp to %s : %v", time.RFC3339, message)
}

return &RawLog{
TimeStamp: t,
Level: strings.ToUpper(message["@level"].(string)),
Message: message["@message"].(string),
}, nil
}
Loading