Skip to content

Commit

Permalink
Add the source code for the CLI totool
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgelbg committed Jun 2, 2020
1 parent 8946301 commit fad66ea
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2020 Jorge Luis Betancourt. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

package main

import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/elastic/beats/libbeat/processors/dissect"
"gopkg.in/alecthomas/kingpin.v2"
)

func main() {
var (
pattern = kingpin.Flag("pattern", "Tokenizer pattern to test the samples.").Required().Short('p').String()
)

kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("0.1").Author("Jorge Luis Betancourt")
kingpin.CommandLine.Help = "Tool for testing a set of sample loglines against a dissect pattern."
kingpin.Parse()

processor, err := dissect.New(*pattern)
if err != nil {
fmt.Printf("ERROR: Coudln't create the processor: %s", err.Error())
return
}

fmt.Println("Enter a sample per line to evaluate")
reader := bufio.NewReader(os.Stdin)
for {
sample, _ := reader.ReadString('\n')
sample = strings.TrimSuffix(sample, "\n")

if sample == "" {
return
}

tokens, err := processor.Dissect(sample)
if err != nil {
fmt.Printf("ERROR: %s", err.Error())
continue
}

payload, err := json.Marshal(tokens)

fmt.Printf("%v\n", string(payload))
}
}

0 comments on commit fad66ea

Please sign in to comment.