Skip to content

Commit

Permalink
cmd/ecs - add CLI tool to check if field exists
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkroh committed Aug 26, 2024
1 parent a835555 commit 72a5eef
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions cmd/ecs/ecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

Check failure on line 1 in cmd/ecs/ecs.go

View workflow job for this annotation

GitHub Actions / go-licenser

is missing the license header

import (
"encoding/json"
"flag"
"fmt"
"os"

"github.com/andrewkroh/go-ecs"
)

var usage = `ecs [field]
ecs is a command-line tool for retrieving definitions of Elastic Common Schema
(ECS) fields. The field definition is written as JSON to stdout.
See https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html
OPTIONS:
-h Show this help message and exit.
-r ECS release version (e.g. 8.11.0 or 8.11 or 8).
Defaults to latest version incorporated into
github.com/andrewkroh/go-ecs at build time.
-q Quite mode. No ECS definition is written to stdout.
ARGUMENTS:
field The name of the ECS field to retrieve the definition for.
This argument is required.
EXAMPLES:
ecs source.ip
Retrieves the JSON definition of the "source.ip" ECS field.
EXIT STATUS:
0 Successful completion. Field is defined in ECS.
1 Field not defined.
2 Usage/argument problem.
`

var (
ecsVersion = flag.String("r", "", "ECS release version")
quiet = flag.Bool("q", false, "Quiet mode")
)

func main() {
// Flag handling.
flag.Usage = func() {
fmt.Fprintln(os.Stderr, usage)
os.Exit(2)
}
flag.Parse()

if len(flag.Args()) == 0 {
flag.Usage()
}
if len(flag.Args()) > 1 {
fmt.Fprintln(os.Stderr, "Only one field name may be specified.")
os.Exit(2)
}

// ECS lookup.
field, err := ecs.Lookup(flag.Arg(0), *ecsVersion)
if err != nil {
if !*quiet {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}

if !*quiet {
// Dump as pretty JSON.
data, err := json.MarshalIndent(field, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
fmt.Printf("%s\n", data)
}
}

0 comments on commit 72a5eef

Please sign in to comment.