Skip to content

Commit 3d41c90

Browse files
committed
extension: choose language first, supported categories per launage
Signed-off-by: mathetake <[email protected]>
1 parent b014305 commit 3d41c90

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

pkg/cmd/extension/init/cmd.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ import (
2727

2828
var (
2929
// extension categories supported by the `init` command.
30-
supportedCategories = options{
31-
{Value: extension.EnvoyHTTPFilter.String(), DisplayText: "HTTP Filter"},
32-
{Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"},
33-
{Value: extension.EnvoyAccessLogger.String(), DisplayText: "Access Logger"},
30+
supportedCategories = map[string]options{
31+
extension.LanguageRust.String(): {
32+
{Value: extension.EnvoyHTTPFilter.String(), DisplayText: "HTTP Filter"},
33+
{Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"},
34+
{Value: extension.EnvoyAccessLogger.String(), DisplayText: "Access Logger"},
35+
},
36+
extension.LanguageTinyGo.String(): {
37+
{Value: extension.EnvoyHTTPFilter.String(), DisplayText: "HTTP Filter"},
38+
{Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"},
39+
},
3440
}
3541
// programming languages supported by the `init` command.
3642
supportedLanguages = options{
@@ -119,9 +125,14 @@ Scaffold a new Envoy extension in a language of your choice.`,
119125
return scaffold.Scaffold(opts)
120126
},
121127
}
122-
cmd.PersistentFlags().StringVar(&params.Category.Value, "category", "", "Choose extension category. "+hintOneOf(supportedCategories.Values()...))
123-
cmd.PersistentFlags().StringVar(&params.Language.Value, "language", "", "Choose programming language. "+hintOneOf(supportedLanguages.Values()...))
124-
cmd.PersistentFlags().StringVar(&params.Name.Value, "name", "", `Choose extension name, e.g. "mycompany.filters.http.custom_metrics"`)
128+
cmd.PersistentFlags().StringVar(&params.Language.Value,
129+
"language", "",
130+
"Choose programming language. "+hintOneOf(supportedLanguages.Values()...))
131+
cmd.PersistentFlags().StringVar(&params.Category.Value,
132+
"category", "",
133+
"Choose extension category. "+hintOneOf(supportedCategories[params.Language.Value].Values()...))
134+
cmd.PersistentFlags().StringVar(&params.Name.Value,
135+
"name", "", `Choose extension name, e.g. "mycompany.filters.http.custom_metrics"`)
125136
return cmd
126137
}
127138

pkg/cmd/extension/init/cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ var _ = Describe("getenvoy extension init", func() {
7575
Expect(stderr.String()).To(Equal(given.expectedStdErr))
7676
},
7777
Entry("extension category is missing", give(testCase{
78-
args: []string{},
78+
args: []string{"--language", "rust"},
7979
expectedStdErr: `Error: extension category cannot be empty
8080
8181
Run 'getenvoy extension init --help' for usage.
8282
`,
8383
})),
8484
Entry("extension category is not valid", give(testCase{
85-
args: []string{"--category", "invalid.category"},
85+
args: []string{"--language", "rust", "--category", "invalid.category"},
8686
expectedStdErr: `Error: "invalid.category" is not a supported extension category
8787
8888
Run 'getenvoy extension init --help' for usage.

pkg/cmd/extension/init/params.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func (p *param) IsValid() bool {
4343

4444
// params represents all parameters to be filled in by a user.
4545
type params struct {
46-
Category param
47-
Language param
48-
OutputDir param
49-
Name param
46+
Category *param
47+
Language *param
48+
OutputDir *param
49+
Name *param
5050
}
5151

5252
func (o *params) Validate() error {
@@ -85,32 +85,38 @@ func (o *params) DefaultName() {
8585

8686
//nolint:gocyclo
8787
func newParams() *params {
88+
language := &param{
89+
Title: "Language",
90+
Validator: func(value string) error {
91+
if value == "" {
92+
return errors.New("programming language cannot be empty")
93+
}
94+
if !supportedLanguages.Contains(value) {
95+
return errors.Errorf("%q is not a supported programming language", value)
96+
}
97+
return nil
98+
},
99+
}
88100
return &params{
89-
Category: param{
101+
Category: &param{
90102
Title: "Category",
91103
Validator: func(value string) error {
104+
if err := language.Validate(); err != nil {
105+
// fmt.Println(err)
106+
return err
107+
}
108+
92109
if value == "" {
93110
return errors.New("extension category cannot be empty")
94111
}
95-
if !supportedCategories.Contains(value) {
112+
if !supportedCategories[language.Value].Contains(value) {
96113
return errors.Errorf("%q is not a supported extension category", value)
97114
}
98115
return nil
99116
},
100117
},
101-
Language: param{
102-
Title: "Language",
103-
Validator: func(value string) error {
104-
if value == "" {
105-
return errors.New("programming language cannot be empty")
106-
}
107-
if !supportedLanguages.Contains(value) {
108-
return errors.Errorf("%q is not a supported programming language", value)
109-
}
110-
return nil
111-
},
112-
},
113-
OutputDir: param{
118+
Language: language,
119+
OutputDir: &param{
114120
Title: "Output directory",
115121
Validator: func(value string) error {
116122
outputDir, err := scaffold.NormalizeOutputPath(value)
@@ -137,7 +143,7 @@ func newParams() *params {
137143
return nil
138144
},
139145
},
140-
Name: param{
146+
Name: &param{
141147
Title: "Extension name",
142148
Validator: func(value string) error {
143149
if value == "" {

pkg/cmd/extension/init/wizard.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ type promptOpts struct {
5959
// Fill runs the interactive UI to help a user to fill in parameters.
6060
func (w *wizard) Fill(params *params) error {
6161
w.out.Println(uiutil.Underline("What kind of extension would you like to create?"))
62-
if err := w.choose(&params.Category, supportedCategories, w.newCategorySelector); err != nil {
62+
if err := w.choose(params.Language, supportedLanguages, w.newLanguageSelector); err != nil {
6363
return err
6464
}
65-
if err := w.choose(&params.Language, supportedLanguages, w.newLanguageSelector); err != nil {
65+
if err := w.choose(params.Category, supportedCategories[params.Language.Value], w.newCategorySelector); err != nil {
6666
return err
6767
}
68-
if err := w.prompt(&params.OutputDir, w.newOutputDirPrompt, promptOpts{}); err != nil {
68+
if err := w.prompt(params.OutputDir, w.newOutputDirPrompt, promptOpts{}); err != nil {
6969
return err
7070
}
71-
if err := w.prompt(&params.Name, w.newNamePrompt, promptOpts{init: params.DefaultName}); err != nil {
71+
if err := w.prompt(params.Name, w.newNamePrompt, promptOpts{init: params.DefaultName}); err != nil {
7272
return err
7373
}
7474
w.out.Println("Great! Let me help you with that!")

pkg/cmd/extension/init/wizard_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ var _ = Describe("interactive mode", func() {
7575
return testCase{
7676
noColors: true,
7777
expected: fmt.Sprintf(`What kind of extension would you like to create?
78-
* Category HTTP Filter
7978
* Language Rust
79+
* Category HTTP Filter
8080
* Output directory %s
8181
* Extension name mycompany.filters.http.custom_metrics
8282
Great! Let me help you with that!
@@ -88,8 +88,8 @@ Great! Let me help you with that!
8888
return testCase{
8989
noColors: false,
9090
expected: "\x1b[4mWhat kind of extension would you like to create?\x1b[0m\n" +
91-
"\x1b[32m✔\x1b[0m \x1b[3mCategory\x1b[0m \x1b[2mHTTP Filter\x1b[0m\n" +
9291
"\x1b[32m✔\x1b[0m \x1b[3mLanguage\x1b[0m \x1b[2mRust\x1b[0m\n" +
92+
"\x1b[32m✔\x1b[0m \x1b[3mCategory\x1b[0m \x1b[2mHTTP Filter\x1b[0m\n" +
9393
fmt.Sprintf("\x1b[32m✔\x1b[0m \x1b[3mOutput directory\x1b[0m \x1b[2m%s\x1b[0m\n", tmpDir) +
9494
"\x1b[32m✔\x1b[0m \x1b[3mExtension name\x1b[0m \x1b[2mmycompany.filters.http.custom_metrics\x1b[0m\n" +
9595
"Great! Let me help you with that!\n" +

0 commit comments

Comments
 (0)