-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathcreate_package.go
250 lines (229 loc) · 6.47 KB
/
create_package.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/elastic/elastic-package/internal/licenses"
"github.com/elastic/elastic-package/internal/surveyext"
"github.com/elastic/elastic-package/pkg/packages"
"github.com/elastic/elastic-package/pkg/packages/archetype"
)
const createPackageLongDescription = `Use this command to create a new package.
The command can bootstrap the first draft of a package using embedded package template and wizard.`
const (
noLicenseValue = "None"
noLicenseOnCreationMessage = "I will add a license later."
)
type newPackageAnswers struct {
Name string
Type string
Version string
SourceLicense string `survey:"source_license"`
Title string
Description string
Categories []string
KibanaVersion string `survey:"kibana_version"`
ElasticSubscription string `survey:"elastic_subscription"`
GithubOwner string `survey:"github_owner"`
OwnerType string `survey:"owner_type"`
DataStreamType string `survey:"datastream_type"`
}
func createPackageCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Create a new package")
qs := []*survey.Question{
{
Name: "type",
Prompt: &survey.Select{
Message: "Package type:",
Options: []string{"input", "integration"},
Default: "integration",
},
Validate: survey.Required,
},
}
var answers newPackageAnswers
err := survey.Ask(qs, &answers)
if err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
qs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: "Package name:",
Default: "new_package",
},
Validate: survey.ComposeValidators(survey.Required, surveyext.PackageDoesNotExistValidator, surveyext.PackageNameValidator),
},
{
Name: "version",
Prompt: &survey.Input{
Message: "Version:",
Default: "0.0.1",
},
Validate: survey.ComposeValidators(survey.Required, surveyext.SemverValidator),
},
{
Name: "source_license",
Prompt: &survey.Select{
Message: "License:",
Options: []string{
licenses.Elastic20,
licenses.Apache20,
noLicenseValue,
},
Description: func(value string, _ int) string {
if value == noLicenseValue {
return noLicenseOnCreationMessage
}
return ""
},
Default: licenses.Elastic20,
},
},
{
Name: "title",
Prompt: &survey.Input{
Message: "Package title:",
Default: "New Package",
},
Validate: survey.Required,
},
{
Name: "description",
Prompt: &survey.Input{
Message: "Description:",
Default: "This is a new package.",
},
Validate: survey.Required,
},
{
Name: "categories",
Prompt: &survey.MultiSelect{
Message: "Categories:",
Options: []string{"aws", "azure", "cloud", "config_management", "containers", "crm", "custom",
"datastore", "elastic_stack", "google_cloud", "kubernetes", "languages", "message_queue",
"monitoring", "network", "notification", "os_system", "productivity", "security", "support",
"ticketing", "version_control", "web"},
Default: []string{"custom"},
PageSize: 50,
},
Validate: survey.Required,
},
{
Name: "kibana_version",
Prompt: &survey.Input{
Message: "Kibana version constraint:",
Default: surveyext.DefaultKibanaVersionConditionValue(),
},
Validate: survey.ComposeValidators(survey.Required, surveyext.ConstraintValidator),
},
{
Name: "elastic_subscription",
Prompt: &survey.Select{
Message: "Required Elastic subscription:",
Options: []string{"basic", "gold", "platinum", "enterprise"},
Default: "basic",
},
Validate: survey.Required,
},
{
Name: "github_owner",
Prompt: &survey.Input{
Message: "Github owner:",
Default: "elastic/integrations",
},
Validate: survey.ComposeValidators(survey.Required, surveyext.GithubOwnerValidator),
},
{
Name: "owner_type",
Prompt: &survey.Select{
Message: "Owner type:",
Options: []string{"elastic", "partner", "community"},
Description: func(value string, _ int) string {
switch value {
case "elastic":
return "Owned and supported by Elastic"
case "partner":
return "Vendor-owned with support from Elastic"
case "community":
return "Supported by the community"
}
return ""
},
Default: "elastic",
},
Validate: survey.Required,
},
}
if answers.Type == "input" {
inputQs := []*survey.Question{
{
Name: "datastream_type",
Prompt: &survey.Select{
Message: "Input Data Stream type:",
Options: []string{"logs", "metrics"},
Default: "logs",
},
Validate: survey.Required,
},
}
qs = append(qs, inputQs...)
}
err = survey.Ask(qs, &answers)
if err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
descriptor := createPackageDescriptorFromAnswers(answers)
specVersion, err := archetype.GetLatestStableSpecVersion()
if err != nil {
return fmt.Errorf("failed to get spec version: %w", err)
}
descriptor.Manifest.SpecVersion = specVersion.String()
err = archetype.CreatePackage(descriptor)
if err != nil {
return fmt.Errorf("can't create new package: %w", err)
}
cmd.Println("Done")
return nil
}
func createPackageDescriptorFromAnswers(answers newPackageAnswers) archetype.PackageDescriptor {
sourceLicense := ""
if answers.SourceLicense != noLicenseValue {
sourceLicense = answers.SourceLicense
}
inputDataStreamType := ""
if answers.Type == "input" {
inputDataStreamType = answers.DataStreamType
}
return archetype.PackageDescriptor{
Manifest: packages.PackageManifest{
Name: answers.Name,
Title: answers.Title,
Type: answers.Type,
Version: answers.Version,
Source: packages.Source{
License: sourceLicense,
},
Conditions: packages.Conditions{
Kibana: packages.KibanaConditions{
Version: answers.KibanaVersion,
},
Elastic: packages.ElasticConditions{
Subscription: answers.ElasticSubscription,
},
},
Owner: packages.Owner{
Github: answers.GithubOwner,
Type: answers.OwnerType,
},
License: answers.ElasticSubscription,
Description: answers.Description,
Categories: answers.Categories,
},
InputDataStreamType: inputDataStreamType,
}
}