From ee2779cc9d1463e32f4a90389426172df31d4b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Fri, 19 Apr 2019 17:07:22 +0100 Subject: [PATCH] better map merge --- cmd/dkl-dir2config/render-context.go | 19 ++++++++++++++++--- cmd/dkl-local-server/ws.go | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd/dkl-dir2config/render-context.go b/cmd/dkl-dir2config/render-context.go index 7544f19..5cc775a 100644 --- a/cmd/dkl-dir2config/render-context.go +++ b/cmd/dkl-dir2config/render-context.go @@ -41,11 +41,11 @@ func newRenderContext(host *clustersconfig.Host, cfg *clustersconfig.Config) (ct group.Vars, host.Vars, } { - for k, v := range oVars { - vars[k] = v - } + mapMerge(vars, oVars) } + log.Print("vars: ", vars) + return &renderContext{ Host: host, Group: group, @@ -58,6 +58,19 @@ func newRenderContext(host *clustersconfig.Host, cfg *clustersconfig.Config) (ct }, nil } +func mapMerge(target, source map[string]interface{}) { + for k, v := range source { + if tMap, targetIsMap := target[k].(map[string]interface{}); targetIsMap { + if sMap, sourceIsMap := v.(map[string]interface{}); sourceIsMap { + mapMerge(tMap, sMap) + continue + } + } + + target[k] = v + } +} + func (ctx *renderContext) Config() string { if ctx.ConfigTemplate == nil { log.Fatalf("no such config: %q", ctx.Group.Config) diff --git a/cmd/dkl-local-server/ws.go b/cmd/dkl-local-server/ws.go index f778b60..e94aa65 100644 --- a/cmd/dkl-local-server/ws.go +++ b/cmd/dkl-local-server/ws.go @@ -12,7 +12,7 @@ import ( ) func registerWS(rest *restful.Container) { - // Admin API + // Admin-level APIs ws := &restful.WebService{} ws.Filter(adminAuth). HeaderParameter("Authorization", "Admin bearer token")