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 6de748e
Show file tree
Hide file tree
Showing 3 changed files with 59 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
}
8 changes: 8 additions & 0 deletions godoc-current.txt
Expand Up @@ -2342,6 +2342,10 @@ func ApplyInputSourceValues(cCtx *cli.Context, inputSourceContext InputSourceCon
ApplyInputSourceValue on flags implementing the FlagInputSourceExtension
interface to initialize these flags to an alternate input source.

func DetectNewSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (InputSourceContext, error)
DetectNewSourceFromFlagFunc creates a new InputSourceContext from a provided
flag name and source context.

func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc
InitInputSource is used to to setup an InputSourceContext on a cli.Command
Before method. It will create a new input source based on the func provided.
Expand All @@ -2368,6 +2372,10 @@ func NewYamlSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (Inp
NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a
provided flag name and source context.

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


TYPES

Expand Down
8 changes: 8 additions & 0 deletions testdata/godoc-v2.x.txt
Expand Up @@ -2342,6 +2342,10 @@ func ApplyInputSourceValues(cCtx *cli.Context, inputSourceContext InputSourceCon
ApplyInputSourceValue on flags implementing the FlagInputSourceExtension
interface to initialize these flags to an alternate input source.

func DetectNewSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (InputSourceContext, error)
DetectNewSourceFromFlagFunc creates a new InputSourceContext from a provided
flag name and source context.

func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc
InitInputSource is used to to setup an InputSourceContext on a cli.Command
Before method. It will create a new input source based on the func provided.
Expand All @@ -2368,6 +2372,10 @@ func NewYamlSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (Inp
NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a
provided flag name and source context.

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


TYPES

Expand Down

0 comments on commit 6de748e

Please sign in to comment.