-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework/config): module dump command (#273)
feat(framework/config): module dump command Co-authored-by: Ivan Maidurov <ivan.maidurov@omnevo.net>
- Loading branch information
1 parent
7dd9288
commit 8a1b915
Showing
4 changed files
with
202 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"flamingo.me/dingo" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const showAllDuplicatesArg = "all" | ||
|
||
// ModulesCmd for debugging the router configuration | ||
func ModulesCmd(area *Area) *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "modules", | ||
Short: "Modules dump", | ||
Run: func(_ *cobra.Command, args []string) { | ||
|
||
argsMap := make(map[string]struct{}) | ||
for _, arg := range args { | ||
argsMap[arg] = struct{}{} | ||
} | ||
|
||
dumpModules(area, argsMap) | ||
|
||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
// dumpModules from root area with arguments | ||
func dumpModules(area *Area, argsMap map[string]struct{}) { | ||
if area == nil { | ||
return | ||
} | ||
|
||
_, printDuplicatedModules := argsMap[showAllDuplicatesArg] | ||
|
||
fmt.Println() | ||
fmt.Println("****************************************************************************") | ||
fmt.Println("Root Area Modules: ") | ||
fmt.Println("****************************************************************************") | ||
|
||
registry := make(map[string]struct{}) | ||
|
||
for _, module := range area.Modules { | ||
moduleName := getModuleName(module) | ||
registry[moduleName] = struct{}{} | ||
fmt.Print(moduleName) | ||
} | ||
|
||
for _, childArea := range area.Childs { | ||
printChildAreaModules(childArea, registry, printDuplicatedModules) | ||
} | ||
fmt.Println() | ||
} | ||
|
||
// printChildAreaModules recursively prints area's modules and modules from every child area | ||
func printChildAreaModules(area *Area, registry map[string]struct{}, printDuplicatedModules bool) { | ||
if area == nil { | ||
return | ||
} | ||
fmt.Println() | ||
fmt.Println("****************************************************************************") | ||
fmt.Printf("Child Area: %s\n", area.Name) | ||
fmt.Println("****************************************************************************") | ||
|
||
for _, module := range area.Modules { | ||
moduleName := getModuleName(module) | ||
_, foundDuplicate := registry[moduleName] | ||
printModuleName := !foundDuplicate || printDuplicatedModules | ||
if printModuleName { | ||
fmt.Print(moduleName) | ||
} | ||
} | ||
|
||
for _, childArea := range area.Childs { | ||
printChildAreaModules(childArea, registry, printDuplicatedModules) | ||
} | ||
} | ||
|
||
// getModuleName from module using package path and interface name | ||
func getModuleName(module dingo.Module) string { | ||
moduleType := reflect.TypeOf(module) | ||
toRemember := "" | ||
if moduleType.Kind() == reflect.Ptr { | ||
toRemember = toRemember + "*" | ||
} | ||
return fmt.Sprintf("%s%s.%s\n", toRemember, moduleType.Elem().PkgPath(), moduleType.Elem().Name()) | ||
} |
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,100 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"flamingo.me/dingo" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"flamingo.me/flamingo/v3/framework/config" | ||
) | ||
|
||
type ( | ||
testModule1 struct{} | ||
testModule2 struct{} | ||
testModule3 struct{} | ||
testModule4 struct{} | ||
testModule5 struct{} | ||
) | ||
|
||
func (m1 *testModule1) Configure(_ *dingo.Injector) {} | ||
func (m2 *testModule2) Configure(_ *dingo.Injector) {} | ||
func (m3 *testModule3) Configure(_ *dingo.Injector) {} | ||
func (m3 *testModule4) Configure(_ *dingo.Injector) {} | ||
func (m3 *testModule5) Configure(_ *dingo.Injector) {} | ||
|
||
func TestModulesCmd_Print(t *testing.T) { | ||
t.Run("Visual test: print modules without duplicates", func(t *testing.T) { | ||
modules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
} | ||
|
||
childModules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
new(testModule4), | ||
} | ||
|
||
subChildModules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
new(testModule4), | ||
new(testModule5), | ||
} | ||
|
||
testArea := config.NewArea("testArea", modules) | ||
childArea := config.NewArea("childTestArea", childModules) | ||
subChildArea := config.NewArea("subChildTestArea", subChildModules) | ||
childArea.Childs = []*config.Area{ | ||
subChildArea, | ||
} | ||
testArea.Childs = []*config.Area{ | ||
childArea, | ||
} | ||
|
||
function := config.ModulesCmd(testArea).Run | ||
assert.NotNil(t, function) | ||
function(nil, nil) | ||
}) | ||
|
||
t.Run("Visual test: print modules with duplicates", func(t *testing.T) { | ||
modules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
} | ||
|
||
childModules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
new(testModule4), | ||
} | ||
|
||
subChildModules := []dingo.Module{ | ||
new(testModule1), | ||
new(testModule2), | ||
new(testModule3), | ||
new(testModule4), | ||
new(testModule5), | ||
} | ||
|
||
testArea := config.NewArea("testArea", modules) | ||
childArea := config.NewArea("childTestArea", childModules) | ||
subChildArea := config.NewArea("subChildTestArea", subChildModules) | ||
childArea.Childs = []*config.Area{ | ||
subChildArea, | ||
} | ||
testArea.Childs = []*config.Area{ | ||
childArea, | ||
} | ||
|
||
function := config.ModulesCmd(testArea).Run | ||
assert.NotNil(t, function) | ||
function(nil, []string{"all"}) | ||
}) | ||
} |
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