Skip to content

Commit

Permalink
Autodetect config format based on extension
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Mar 15, 2023
1 parent 560c87b commit a5572f6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions altsrc/detect_input_source.go
@@ -0,0 +1,43 @@
package altsrc

import (
"fmt"
"path/filepath"

"github.com/urfave/cli/v2"
)

var detectableSources = map[string]func(string) func(cCtx *cli.Context) (InputSourceContext, error){
".conf": NewTomlSourceFromFlagFunc,
".json": NewJSONSourceFromFlagFunc,
".toml": NewTomlSourceFromFlagFunc,
".yaml": NewYamlSourceFromFlagFunc,
}

// RegisterDetectableSource lets developers add support for their own altsrc filetypes to the autodetection list.
func RegisterDetectableSource(extension string, handler func(string) func(cCtx *cli.Context) (InputSourceContext, error)) {
detectableSources[extension] = handler
}

// DetectNewSourceFromFlagFunc creates a new InputSourceContext from a provided flag name and source context.
func DetectNewSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (InputSourceContext, error) {
return func(cCtx *cli.Context) (InputSourceContext, error) {
if fileFullPath := cCtx.String(flagFileName); fileFullPath != "" {
fileExt := filepath.Ext(fileFullPath)
if handler, ok := detectableSources[fileExt]; ok {
return handler(flagFileName)(cCtx)
}
return nil, fmt.Errorf("Unable to determine config file type from extension.\nMust be one of %v", detectableExtensions())
}
return defaultInputSource()
}
}

func detectableExtensions() []string {
extensions := make([]string, 0, len(detectableSources))
for ext := range detectableSources {
extensions = append(extensions, ext)
}

return extensions
}

0 comments on commit a5572f6

Please sign in to comment.