mirror of
https://github.com/hanzoai/ingress-parser.git
synced 2026-10-30 18:33:22 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-Authored-By: Claude Opus 5 <[email protected]>
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package env
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/ingress-parser/parser"
|
|
)
|
|
|
|
// FindPrefixedEnvVars finds prefixed environment variables.
|
|
func FindPrefixedEnvVars(environ []string, prefix string, element any) []string {
|
|
prefixes := getRootPrefixes(element, prefix)
|
|
|
|
var values []string
|
|
for _, px := range prefixes {
|
|
for _, value := range environ {
|
|
if strings.HasPrefix(value, px) {
|
|
values = append(values, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
return values
|
|
}
|
|
|
|
func getRootPrefixes(element any, prefix string) []string {
|
|
if element == nil {
|
|
return nil
|
|
}
|
|
|
|
rootType := reflect.TypeOf(element)
|
|
|
|
return getPrefixes(prefix, rootType)
|
|
}
|
|
|
|
func getPrefixes(prefix string, rootType reflect.Type) []string {
|
|
if rootType.Kind() == reflect.Pointer {
|
|
rootType = rootType.Elem()
|
|
}
|
|
|
|
if rootType.Kind() != reflect.Struct {
|
|
return nil
|
|
}
|
|
|
|
var names []string
|
|
for field := range rootType.Fields() {
|
|
|
|
if !parser.IsExported(field) {
|
|
continue
|
|
}
|
|
|
|
if field.Anonymous &&
|
|
(field.Type.Kind() == reflect.Pointer && field.Type.Elem().Kind() == reflect.Struct || field.Type.Kind() == reflect.Struct) {
|
|
names = append(names, getPrefixes(prefix, field.Type)...)
|
|
continue
|
|
}
|
|
|
|
names = append(names, prefix+strings.ToUpper(field.Name))
|
|
}
|
|
|
|
return names
|
|
}
|