From 7601d45009605eb5da35048b7b929027a1f0f7da Mon Sep 17 00:00:00 2001 From: thde Date: Fri, 19 Apr 2024 14:37:45 +0200 Subject: [PATCH] refactor: merge --- main.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index dae03ba..9f7b5a1 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "log" - "os" "os/signal" "strings" @@ -150,28 +149,28 @@ func kongVariables() (kong.Vars, error) { if err != nil { return nil, fmt.Errorf("error on application create kong vars: %w", err) } - if err := merge(result, appCreateKongVars); err != nil { - return nil, fmt.Errorf("error when merging application create kong variables: %w", err) - } - mysqlCreateKongVars, err := create.MySQLKongVars() if err != nil { return nil, fmt.Errorf("error on mysql create kong vars: %w", err) } - if err := merge(result, mysqlCreateKongVars); err != nil { - return nil, fmt.Errorf("error when merging mysql create kong variables: %w", err) + + if err := merge(result, appCreateKongVars, mysqlCreateKongVars); err != nil { + return nil, fmt.Errorf("error when merging kong variables: %w", err) } return result, nil } -func merge(existing kong.Vars, with kong.Vars) error { - for k, v := range with { - _, exists := existing[k] - if exists { - return fmt.Errorf("variable %q is already in use", k) +func merge(existing kong.Vars, withs ...kong.Vars) error { + for _, with := range withs { + for k, v := range with { + _, exists := existing[k] + if exists { + return fmt.Errorf("variable %q is already in use", k) + } + existing[k] = v } - existing[k] = v } + return nil }