-
Notifications
You must be signed in to change notification settings - Fork 40
/
new.go
203 lines (170 loc) · 4.54 KB
/
new.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package cmd
import (
"errors"
"os"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/meteor/generator"
"github.com/raystack/meteor/registry"
"github.com/spf13/cobra"
)
// NewCmd creates a command object for the "new" action
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new",
Short: "Bootstrap new recipes",
Annotations: map[string]string{
"group": "core",
},
}
cmd.AddCommand(NewRecipeCmd())
return cmd
}
// NewRecipeCmd creates a command object for newerating recipes
func NewRecipeCmd() *cobra.Command {
var (
extractor string
scope string
sinks string
processors string
)
cmd := &cobra.Command{
Use: "recipe [name]",
Aliases: []string{"r"},
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Short: "Generate a new recipe",
Long: heredoc.Doc(`
Generate a new recipe.
The recipe will be printed on standard output.
Specify recipe name with the first argument without extension.
Use commma to separate multiple sinks and processors.`),
Example: heredoc.Doc(`
# generate a recipe with a bigquery extractor and a console sink
$ meteor new recipe sample -e bigquery -s console
# generate recipe with multiple sinks
$ meteor new recipe sample -e bigquery -s compass,kafka -p enrich
# store recipe to a file
$ meteor new recipe sample -e bigquery -s compass > recipe.yaml
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
var sinkList []string
var procList []string
var err error
if extractor == "" {
extractor, err = recipeExtractorSurvey()
if err != nil {
return err
}
}
if sinks != "" {
sinkList = strings.Split(sinks, ",")
} else {
sinkList, err = recipeSinkSurvey()
if err != nil {
return err
}
}
if processors != "" {
procList = strings.Split(processors, ",")
} else {
procList, err = recipeProcessorSurvey()
if err != nil {
return err
}
}
return generator.RecipeWriteTo(generator.RecipeParams{
Name: args[0],
Source: extractor,
Scope: scope,
Sinks: sinkList,
Processors: procList,
}, os.Stdout)
},
}
cmd.Flags().StringVarP(&extractor, "extractor", "e", "", "Type of extractor")
cmd.Flags().StringVarP(&scope, "scope", "n", "", "URN's namespace")
cmd.Flags().StringVarP(&sinks, "sinks", "s", "", "List of sink types")
cmd.Flags().StringVarP(&processors, "processors", "p", "", "List of processor types")
if err := cmd.MarkFlagRequired("scope"); err != nil {
panic(err)
}
return cmd
}
func recipeSinkSurvey() ([]string, error) {
var availableSinks []string
var sinkInput []string
for sink := range registry.Sinks.List() {
availableSinks = append(availableSinks, sink)
}
if len(availableSinks) == 0 {
return []string{}, errors.New("no sinks found")
}
var qs = []*survey.Question{
{
Name: "sink",
Prompt: &survey.MultiSelect{
Message: "Select sink(s)",
Options: availableSinks,
Help: "Select the sink(s) for this recipe",
},
Validate: survey.Required,
},
}
if err := survey.Ask(qs, &sinkInput); err != nil {
return []string{}, err
}
return sinkInput, nil
}
func recipeProcessorSurvey() ([]string, error) {
var availableProcessors []string
var processorInput []string
for processor := range registry.Processors.List() {
availableProcessors = append(availableProcessors, processor)
}
if len(availableProcessors) == 0 {
return []string{}, errors.New("no processors found")
}
var qs = []*survey.Question{
{
Name: "processor",
Prompt: &survey.MultiSelect{
Message: "Select processor",
Options: availableProcessors,
Help: "Select the processor(s) for this recipe",
},
},
}
if err := survey.Ask(qs, &processorInput); err != nil {
return []string{}, err
}
return processorInput, nil
}
func recipeExtractorSurvey() (string, error) {
var availableExtractors []string
var extractorInput string
for extractor := range registry.Extractors.List() {
availableExtractors = append(availableExtractors, extractor)
}
if len(availableExtractors) == 0 {
return "", errors.New("no extractors found")
}
var qs = []*survey.Question{
{
Name: "extractor",
Prompt: &survey.Select{
Message: "Select an extractor",
Options: availableExtractors,
Help: "Select the extractor for this recipe",
},
Validate: survey.Required,
},
}
if err := survey.Ask(qs, &extractorInput); err != nil {
return "", err
}
return extractorInput, nil
}