Skip to content

Commit

Permalink
feat(framework/config): module dump command (#273)
Browse files Browse the repository at this point in the history
feat(framework/config): module dump command

Co-authored-by: Ivan Maidurov <ivan.maidurov@omnevo.net>
  • Loading branch information
IvanMaidurov and IvanMaidurov committed Jul 14, 2022
1 parent 7dd9288 commit 8a1b915
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 8 deletions.
93 changes: 93 additions & 0 deletions framework/config/modulescmd.go
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())
}
100 changes: 100 additions & 0 deletions framework/config/modulescmd_test.go
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"})
})
}
4 changes: 3 additions & 1 deletion framework/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ package framework

import (
"flamingo.me/dingo"
"github.com/spf13/cobra"

"flamingo.me/flamingo/v3/framework/config"
"flamingo.me/flamingo/v3/framework/controller"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"flamingo.me/flamingo/v3/framework/web/filter"
"github.com/spf13/cobra"
)

const (
Expand All @@ -40,6 +41,7 @@ type (
func (*InitModule) Configure(injector *dingo.Injector) {
injector.BindMulti(new(cobra.Command)).ToProvider(web.RoutesCmd)
injector.BindMulti(new(cobra.Command)).ToProvider(web.HandlerCmd)
injector.BindMulti(new(cobra.Command)).ToProvider(config.ModulesCmd)
injector.BindMulti(new(cobra.Command)).ToProvider(config.Cmd)

web.BindRoutes(injector, new(routes))
Expand Down
13 changes: 6 additions & 7 deletions framework/web/routescmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import (
"sort"
"strings"

"flamingo.me/flamingo/v3/framework/config"
"github.com/spf13/cobra"
)

// RoutesCmd for debugging the router configuration
func RoutesCmd(router *Router, area *config.Area) *cobra.Command {
func RoutesCmd(router *Router) *cobra.Command {

cmd := &cobra.Command{
Use: "routes",
Short: "Routes dump",
Run: func(cmd *cobra.Command, args []string) {

dumpRoutes(router, area)
dumpRoutes(router)

},
}
Expand All @@ -27,22 +26,22 @@ func RoutesCmd(router *Router, area *config.Area) *cobra.Command {
}

// HandlerCmd for debugging the router/handler configuration
func HandlerCmd(router *Router, area *config.Area) *cobra.Command {
func HandlerCmd(router *Router) *cobra.Command {

cmd := &cobra.Command{
Use: "handler",
Short: "Dump the Handlers and its registered methods",
Run: func(cmd *cobra.Command, args []string) {

dumpHandler(router, area)
dumpHandler(router)

},
}

return cmd
}

func dumpRoutes(router *Router, area *config.Area) {
func dumpRoutes(router *Router) {
if router == nil {
return
}
Expand All @@ -60,7 +59,7 @@ func dumpRoutes(router *Router, area *config.Area) {
}
}

func dumpHandler(router *Router, area *config.Area) {
func dumpHandler(router *Router) {
if router == nil {
return
}
Expand Down

0 comments on commit 8a1b915

Please sign in to comment.