Skip to content

Commit

Permalink
NEW json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTobi committed Jan 15, 2024
1 parent 67c14c1 commit 79c81b2
Show file tree
Hide file tree
Showing 53 changed files with 27,825 additions and 74 deletions.
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
module github.com/ms-henglu/pal

go 1.19
go 1.21

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.1
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
)
12 changes: 9 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +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.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/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/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

0 comments on commit 79c81b2

Please sign in to comment.