This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
88 lines (77 loc) · 2.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
This is a console application that prints Cucumber messages to
STDOUT. By default it prints them as protobuf, but the --json flag
will print them as JSON (useful for testing/debugging)
*/
package main
import (
b64 "encoding/base64"
"flag"
"fmt"
"github.com/cucumber/gherkin-go/v15"
messages "github.com/cucumber/messages-go/v13"
fio "github.com/cucumber/messages-go/v13/io"
gio "github.com/gogo/protobuf/io"
"os"
)
var noSource = flag.Bool("no-source", false, "Skip gherkin source events")
var noAst = flag.Bool("no-ast", false, "Skip gherkin AST events")
var noPickles = flag.Bool("no-pickles", false, "Skip gherkin Pickle events")
var formatFlag = flag.String("format", "protobuf", "Output format")
var predictableIds = flag.Bool("predictable-ids", false, "Generate incrementing ids rather than UUIDs")
var versionFlag = flag.Bool("version", false, "print version")
var dialectsFlag = flag.Bool("dialects", false, "print dialects as JSON")
var defaultDialectFlag = flag.String("default-dialect", "en", "the default dialect")
// Set during build with -ldflags
var version string = "(unknown version)"
var gherkinDialects string
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("gherkin %s\n", version)
os.Exit(0)
}
if *dialectsFlag {
sDec, _ := b64.StdEncoding.DecodeString(gherkinDialects)
fmt.Println(string(sDec))
os.Exit(0)
}
var newId func() string
if *predictableIds {
newId = (&messages.Incrementing{}).NewId
} else {
newId = messages.UUID{}.NewId
}
paths := flag.Args()
var writer = newWriter()
defer writer.Close()
_, err := gherkin.Messages(
paths,
os.Stdin,
*defaultDialectFlag,
!*noSource,
!*noAst,
!*noPickles,
writer,
newId,
)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse Gherkin: %+v\n", err)
os.Exit(1)
}
}
func newWriter() gio.WriteCloser {
var reader gio.WriteCloser
switch *formatFlag {
case "protobuf":
reader = gio.NewDelimitedWriter(os.Stdout)
case "ndjson":
reader = fio.NewNdjsonWriter(os.Stdout)
default:
_, err := fmt.Fprintf(os.Stderr, "Unsupported format: %s\n", *formatFlag)
if err != nil {
panic(err)
}
}
return reader
}