Skip to content

Commit

Permalink
Add type slice (only for strings) type (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
shackra authored Jul 16, 2024
1 parent c4d7f74 commit a1186f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ func DefineConfiguration(
}
mainCMD.PersistentFlags().
StringP(field.FieldName, field.CLIShortHand, value, field.GetDescription())
case reflect.Slice:
value, err := field.StringArray()
if err != nil {
return nil, nil, fmt.Errorf(
"field %s, %s: %w",
field.FieldName,
field.FieldType,
err,
)
}
mainCMD.PersistentFlags().
StringArrayP(field.FieldName, field.CLIShortHand, value, field.GetDescription())
default:
return nil, nil, fmt.Errorf(
"field %s, %s is not yet supported",
Expand Down
24 changes: 24 additions & 0 deletions pkg/field/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (s SchemaField) String() (string, error) {
return value, nil
}

// StringArray retuns the default value as a string array.
func (s SchemaField) StringArray() ([]string, error) {
value, ok := s.DefaultValue.([]string)
if !ok {
return nil, WrongValueTypeErr
}

return value, nil
}

func (s SchemaField) GetDescription() string {
var line string
if s.Description == "" {
Expand Down Expand Up @@ -120,6 +130,20 @@ func IntField(name string, optional ...fieldOption) SchemaField {
return field
}

func StringArrayField(name string, optional ...fieldOption) SchemaField {
field := SchemaField{
FieldName: name,
FieldType: reflect.Slice,
DefaultValue: []string{},
}

for _, o := range optional {
field = o(field)
}

return field
}

func toUpperCase(i string) string {
return strings.ReplaceAll(strings.ToUpper(i), "-", "_")
}

0 comments on commit a1186f0

Please sign in to comment.