-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/ecs - add CLI tool to check if field exists
- Loading branch information
1 parent
a835555
commit 72a5eef
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
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) | ||
} | ||
} |