Skip to content

Commit 331cd8c

Browse files
committed
make arrays work
1 parent f909984 commit 331cd8c

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

cli/deployment/config.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/cligen/main.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Field struct {
4949
Enterprise bool
5050
Hidden bool
5151
Type string
52+
ViperType string
5253
}
5354

5455
func GenerateData(ctx context.Context, log slog.Logger, dir string) (*Data, error) {
@@ -123,6 +124,7 @@ func (g *Generator) generateAll() (*Data, error) {
123124
if !ok {
124125
return nil, xerrors.Errorf("expected struct type, found %T", spec.Type)
125126
}
127+
126128
for _, field := range t.Fields.List {
127129
key := reflect.StructTag(strings.Trim(field.Tag.Value, "`")).Get("mapstructure")
128130
if key == "" {
@@ -132,24 +134,37 @@ func (g *Generator) generateAll() (*Data, error) {
132134
Key: key,
133135
Env: "CODER_" + strings.ReplaceAll(strings.ToUpper(key), "-", "_"),
134136
}
135-
ft, ok := field.Type.(*ast.Ident)
136-
if !ok {
137-
// return nil, xerrors.Errorf("expected Ident type, found %T", field.Type)
138-
continue
139-
}
140-
switch ft.Name {
141-
case "string":
142-
f.Type = "String"
143-
case "int":
144-
f.Type = "Int"
145-
case "bool":
146-
f.Type = "Bool"
147-
case "[]string":
148-
f.Type = "StringArray"
149-
case "time.Duration":
137+
switch ft := field.Type.(type) {
138+
case *ast.Ident:
139+
switch ft.Name {
140+
case "string":
141+
f.Type = "String"
142+
f.ViperType = "String"
143+
case "int":
144+
f.Type = "Int"
145+
f.ViperType = "Int"
146+
case "bool":
147+
f.Type = "Bool"
148+
f.ViperType = "Bool"
149+
default:
150+
continue
151+
}
152+
case *ast.SelectorExpr:
153+
if ft.Sel.Name != "Duration" {
154+
continue
155+
}
150156
f.Type = "Duration"
151-
default:
152-
continue
157+
f.ViperType = "Duration"
158+
case *ast.ArrayType:
159+
i, ok := ft.Elt.(*ast.Ident)
160+
if !ok {
161+
continue
162+
}
163+
if i.Name != "string" {
164+
continue
165+
}
166+
f.Type = "StringArray"
167+
f.ViperType = "StringSlice"
153168
}
154169

155170
for _, line := range field.Doc.List {
@@ -225,6 +240,7 @@ package deployment
225240
import (
226241
"os"
227242
"path/filepath"
243+
"time"
228244
229245
"github.com/spf13/pflag"
230246
"github.com/spf13/viper"
@@ -254,7 +270,7 @@ func DefaultViper() *viper.Viper {
254270
func AttachFlags(flagset *pflag.FlagSet, vip *viper.Viper) {
255271
{{- range .Fields }}
256272
{{- if and (.Flag) (not .Enterprise) }}
257-
_ = flagset.{{ .Type }}P("{{ .Flag }}", "{{ .Shorthand }}", vip.Get{{ .Type }}("{{ .Key }}"), ` + "`{{ .Usage }}`" + `+"\n"+cliui.Styles.Placeholder.Render("Consumes ${{ .Env }}"))
273+
_ = flagset.{{ .Type }}P("{{ .Flag }}", "{{ .Shorthand }}", vip.Get{{ .ViperType }}("{{ .Key }}"), ` + "`{{ .Usage }}`" + `+"\n"+cliui.Styles.Placeholder.Render("Consumes ${{ .Env }}"))
258274
_ = vip.BindPFlag("{{ .Key }}", flagset.Lookup("{{ .Flag }}"))
259275
{{- if and .Hidden }}
260276
_ = flagset.MarkHidden("{{ .Flag }}")
@@ -266,7 +282,7 @@ func AttachFlags(flagset *pflag.FlagSet, vip *viper.Viper) {
266282
func AttachEnterpriseFlags(flagset *pflag.FlagSet, vip *viper.Viper) {
267283
{{- range .Fields }}
268284
{{- if and (.Flag) (.Enterprise) }}
269-
_ = flagset.{{ .Type }}P("{{ .Flag }}", "{{ .Shorthand }}", vip.Get{{ .Type }}("{{ .Key }}"), ` + "`{{ .Usage }}`" + `)
285+
_ = flagset.{{ .Type }}P("{{ .Flag }}", "{{ .Shorthand }}", vip.Get{{ .ViperType }}("{{ .Key }}"), ` + "`{{ .Usage }}`" + `)
270286
_ = vip.BindPFlag("{{ .Key }}", flagset.Lookup("{{ .Flag }}"))
271287
{{- if and .Hidden }}
272288
_ = flagset.MarkHidden("{{ .Flag }}")

0 commit comments

Comments
 (0)