-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move things back to `altsrc` - Add support for extensions to `App` - Implement `DetectableSourcesAppExtension` to hold `detectableSources` - Refactor code and tests for the revised setup
- Loading branch information
1 parent
1d1e48a
commit 4439bfa
Showing
17 changed files
with
402 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package altsrc | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
// defaultInputSource creates a default cli.InputSourceContext. | ||
func defaultInputSource() (cli.InputSourceContext, error) { | ||
// defaultInputSource creates a default InputSourceContext. | ||
func defaultInputSource() (InputSourceContext, error) { | ||
return &MapInputSource{file: "", valueMap: map[interface{}]interface{}{}}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package altsrc | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type DetectableSourcesAppExtension struct { | ||
detectableSources map[string]func(string) func(*cli.Context) (InputSourceContext, error) | ||
} | ||
|
||
func NewDetectableSourcesAppExtension() DetectableSourcesAppExtension { | ||
return DetectableSourcesAppExtension{ | ||
detectableSources: make(map[string]func(string) func(*cli.Context) (InputSourceContext, error)), | ||
} | ||
} | ||
|
||
// MyName satisfies the cli.AppExtension interface, providing a name to register the extension under | ||
func (e DetectableSourcesAppExtension) MyName() string { | ||
return "DetectableSources" | ||
} | ||
|
||
// RegisterDetectableSource lets developers add support for their own altsrc filetypes to the autodetection list. | ||
func (e DetectableSourcesAppExtension) RegisterDetectableSource(extension string, handler func(string) func(*cli.Context) (InputSourceContext, error)) { | ||
if e.detectableSources == nil { | ||
e.detectableSources = make(map[string]func(string) func(*cli.Context) (InputSourceContext, error)) | ||
} | ||
e.detectableSources[extension] = handler | ||
} | ||
|
||
func (e DetectableSourcesAppExtension) getDetectableSources() map[string]func(string) func(*cli.Context) (InputSourceContext, error) { | ||
return e.detectableSources | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package altsrc | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func newTestApp() *cli.App { | ||
a := cli.NewApp() | ||
a.Writer = io.Discard | ||
a.AddExtension(NewDetectableSourcesAppExtension()) | ||
return a | ||
} | ||
|
||
func TestRegisterDetectableSource(t *testing.T) { | ||
app := newTestApp() | ||
testHandler := func(s string) func(*cli.Context) (InputSourceContext, error) { | ||
return func(ctx *cli.Context) (InputSourceContext, error) { | ||
return testInputSource{}, nil | ||
} | ||
} | ||
|
||
app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).RegisterDetectableSource(".test", testHandler) | ||
|
||
_, ok := app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).detectableSources[".test"] | ||
expect(t, ok, true) | ||
} | ||
|
||
func TestGetDetectableSources(t *testing.T) { | ||
app := newTestApp() | ||
testHandler := func(s string) func(*cli.Context) (InputSourceContext, error) { | ||
return func(ctx *cli.Context) (InputSourceContext, error) { | ||
return testInputSource{}, nil | ||
} | ||
} | ||
|
||
_, ok := app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).getDetectableSources()[".test"] | ||
expect(t, ok, false) | ||
|
||
app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).RegisterDetectableSource(".test", testHandler) | ||
|
||
_, ok = app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).detectableSources[".test"] | ||
expect(t, ok, true) | ||
|
||
_, ok = app.GetExtension("DetectableSources").(DetectableSourcesAppExtension).getDetectableSources()[".test"] | ||
expect(t, ok, true) | ||
} |
Oops, something went wrong.