-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (105 loc) · 3.04 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
"github.com/powersj/spawn/agent"
"github.com/powersj/spawn/config"
"github.com/powersj/spawn/internal"
"github.com/urfave/cli/v2"
)
const (
cliDescription = `spawn is a TOML config driven agent to generate data
The configuration consists of generators, serializers, and outputs. Generators
are used to generate random data. Serializers determine a specific output data
type and format, and outputs is where the data is sent.
Agent level settings are also available to control logging and generation of
data.
`
)
func main() {
app := &cli.App{
Name: "spawn",
Usage: "TOML config driven agent to generate data",
Description: cliDescription,
Suggest: true,
Commands: []*cli.Command{
{
Name: "run",
Usage: "Runs the generator",
Description: "Runs the generator",
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return fmt.Errorf("missing TOML file")
} else if ctx.NArg() > 1 {
return fmt.Errorf("too many arguments")
}
c, err := config.NewConfig(ctx.Args().First())
if err != nil {
return fmt.Errorf("Error loading config: %w", err)
}
a, err := agent.NewAgent(c)
if err != nil {
return fmt.Errorf("Error creating agent: %w", err)
}
pprof := agent.NewPprofServer()
pprof.Start(c.Agent.PprofPort)
return a.Run()
},
},
{
Name: "toml",
Usage: "Used to verify a TOML configuration format",
Description: "Used to verify a TOML configuration format",
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return fmt.Errorf("missing TOML file")
} else if ctx.NArg() > 1 {
return fmt.Errorf("too many arguments")
}
var data any
if _, err := toml.DecodeFile(ctx.Args().First(), &data); err != nil {
return fmt.Errorf("Invalid TOML File: %w", err)
}
fmt.Println("The TOML file is valid.")
return nil
},
},
{
Name: "once",
Usage: "Run all generators and serializers and output to stdout",
Description: "Run all generators and serializers and output to stdout",
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return fmt.Errorf("missing TOML file")
} else if ctx.NArg() > 1 {
return fmt.Errorf("too many arguments")
}
c, err := config.NewConfig(ctx.Args().First())
if err != nil {
return fmt.Errorf("Error loading config: %w", err)
}
a, err := agent.NewAgent(c)
if err != nil {
return fmt.Errorf("Error creating agent: %w", err)
}
fmt.Println("skipping outputs in run once mode")
return a.RunOnce()
},
},
{
Name: "version",
Usage: "Print version, build, and platform info",
Description: "Print version, build, and platform info",
Action: func(*cli.Context) error {
fmt.Println(internal.AppVersion())
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}