|
21 | 21 | package cmd
|
22 | 22 |
|
23 | 23 | import (
|
24 |
| - "github.com/spf13/cobra" |
25 |
| - "github.com/ugol/jr/pkg/functions" |
26 | 24 | "os"
|
27 |
| - "strings" |
28 | 25 | "time"
|
29 |
| -) |
30 | 26 |
|
31 |
| -const NUM = 1 |
32 |
| -const LOCALE = "US" |
33 |
| -const FREQUENCY = -1 |
34 |
| -const DURATION = 0 |
35 |
| -const TEMPLATEDIR = "$HOME/.jr/templates" |
36 |
| -const DEFAULT_KEY = "null" |
37 |
| -const DEFAULT_OUTPUT = "stdout" |
38 |
| -const DEFAULT_OUTPUT_TEMPLATE = "{{.V}}\n" |
39 |
| -const DEFAULT_SERIALIZER = "json-schema" |
40 |
| -const KAFKA_CONFIG = "./kafka/config.properties" |
41 |
| -const REGISTRY_CONFIG = "./kafka/registry.properties" |
42 |
| -const REDIS_TTL = 1 * time.Minute |
43 |
| -const REDIS_CONFIG = "./redis/config.json" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "github.com/ugol/jr/pkg/functions" |
| 29 | +) |
44 | 30 |
|
45 | 31 | var runCmd = &cobra.Command{
|
46 | 32 | Use: "run [template]",
|
@@ -117,52 +103,41 @@ jr run --templateFileName ~/.jr/templates/net_device.tpl
|
117 | 103 | Preload: preload,
|
118 | 104 | PreloadSize: preloadSize,
|
119 | 105 | }
|
120 |
| - configureJrContext(conf) |
121 |
| - functions.DoTemplates(conf) |
122 |
| - }, |
123 |
| -} |
124 | 106 |
|
125 |
| -func configureJrContext(conf functions.Configuration) { |
126 |
| - functions.Random.Seed(conf.Seed) |
127 |
| - functions.JrContext.Num = conf.Num |
128 |
| - functions.JrContext.Range = make([]int, conf.Num) |
129 |
| - functions.JrContext.Frequency = conf.Frequency |
130 |
| - functions.JrContext.Locale = strings.ToLower(conf.Locale) |
131 |
| - functions.JrContext.Seed = conf.Seed |
132 |
| - functions.JrContext.TemplateDir = conf.TemplateDir |
133 |
| - functions.JrContext.CountryIndex = functions.IndexOf(strings.ToUpper(conf.Locale), "country") |
| 107 | + functions.DoTemplates(conf, nil) |
| 108 | + }, |
134 | 109 | }
|
135 | 110 |
|
136 | 111 | func init() {
|
137 | 112 | rootCmd.AddCommand(runCmd)
|
138 |
| - runCmd.Flags().IntP("num", "n", NUM, "Number of elements to create for each pass") |
139 |
| - runCmd.Flags().DurationP("frequency", "f", FREQUENCY, "how much time to wait for next generation pass") |
140 |
| - runCmd.Flags().DurationP("duration", "d", DURATION, "If frequency is enabled, with Duration you can set a finite amount of time") |
| 113 | + runCmd.Flags().IntP("num", "n", functions.NUM, "Number of elements to create for each pass") |
| 114 | + runCmd.Flags().DurationP("frequency", "f", functions.FREQUENCY, "how much time to wait for next generation pass") |
| 115 | + runCmd.Flags().DurationP("duration", "d", functions.DURATION, "If frequency is enabled, with Duration you can set a finite amount of time") |
141 | 116 |
|
142 | 117 | runCmd.Flags().Int64("seed", time.Now().UTC().UnixNano(), "Seed to init pseudorandom generator")
|
143 | 118 |
|
144 |
| - runCmd.Flags().String("templateDir", os.ExpandEnv(TEMPLATEDIR), "directory containing templates") |
145 |
| - runCmd.Flags().StringP("kafkaConfig", "F", KAFKA_CONFIG, "Kafka configuration") |
146 |
| - runCmd.Flags().String("registryConfig", REGISTRY_CONFIG, "Kafka configuration") |
| 119 | + runCmd.Flags().String("templateDir", os.ExpandEnv(functions.TEMPLATEDIR), "directory containing templates") |
| 120 | + runCmd.Flags().StringP("kafkaConfig", "F", functions.KAFKA_CONFIG, "Kafka configuration") |
| 121 | + runCmd.Flags().String("registryConfig", functions.REGISTRY_CONFIG, "Kafka configuration") |
147 | 122 | runCmd.Flags().Bool("templateFileName", false, "If enabled, [template] must be a template file")
|
148 | 123 | runCmd.Flags().Bool("template", false, "If enabled, [template] must be a string containing a template, to be embedded directly in the script")
|
149 | 124 |
|
150 | 125 | runCmd.Flags().StringSliceP("preload", "p", []string{""}, "Array of templates to preload")
|
151 | 126 | runCmd.Flags().IntSlice("preloadSize", []int{}, "Array of template sizes to preload")
|
152 | 127 |
|
153 |
| - runCmd.Flags().StringP("key", "k", DEFAULT_KEY, "A template to generate a key") |
| 128 | + runCmd.Flags().StringP("key", "k", functions.DEFAULT_KEY, "A template to generate a key") |
154 | 129 | runCmd.Flags().StringSliceP("topic", "t", []string{"test"}, "Array of Kafka topic names")
|
155 | 130 |
|
156 | 131 | runCmd.Flags().Bool("kcat", false, "If you want to pipe jr with kcat, use this flag: it is equivalent to --output stdout --outputTemplate '{{key}},{{value}}' --oneline")
|
157 |
| - runCmd.Flags().StringP("output", "o", DEFAULT_OUTPUT, "can be one of stdout, kafka, redis, mongo") |
158 |
| - runCmd.Flags().String("outputTemplate", DEFAULT_OUTPUT_TEMPLATE, "Formatting of K,V on standard output") |
| 132 | + runCmd.Flags().StringP("output", "o", functions.DEFAULT_OUTPUT, "can be one of stdout, kafka, redis, mongo") |
| 133 | + runCmd.Flags().String("outputTemplate", functions.DEFAULT_OUTPUT_TEMPLATE, "Formatting of K,V on standard output") |
159 | 134 | runCmd.Flags().BoolP("oneline", "l", false, "strips /n from output, for example to be pipelined to tools like kcat")
|
160 | 135 | runCmd.Flags().BoolP("autocreate", "a", false, "if enabled, autocreate topics")
|
161 |
| - runCmd.Flags().String("locale", LOCALE, "Locale") |
| 136 | + runCmd.Flags().String("locale", functions.LOCALE, "Locale") |
162 | 137 |
|
163 | 138 | runCmd.Flags().BoolP("schemaRegistry", "s", false, "If you want to use Confluent Schema Registry")
|
164 |
| - runCmd.Flags().String("serializer", DEFAULT_SERIALIZER, "Type of serializer: json-schema, avro-generic, avro, protobuf") |
165 |
| - runCmd.Flags().Duration("redis.ttl", REDIS_TTL, "If output is redis, ttl of the object") |
166 |
| - runCmd.Flags().String("redisConfig", REDIS_CONFIG, "Redis configuration") |
| 139 | + runCmd.Flags().String("serializer", functions.DEFAULT_SERIALIZER, "Type of serializer: json-schema, avro-generic, avro, protobuf") |
| 140 | + runCmd.Flags().Duration("redis.ttl", functions.REDIS_TTL, "If output is redis, ttl of the object") |
| 141 | + runCmd.Flags().String("redisConfig", functions.REDIS_CONFIG, "Redis configuration") |
167 | 142 |
|
168 | 143 | }
|
0 commit comments