From cf6802a190624978d48ba8d37cc5203c1ec4995f Mon Sep 17 00:00:00 2001 From: marshyski Date: Mon, 14 Oct 2024 20:45:14 -0400 Subject: [PATCH] Keep old DB data and update config values from pal-actions.yml --- main.go | 5 +++-- test/pal-actions.yml | 1 + utils/utils.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a1caa30..7e9ccd7 100644 --- a/main.go +++ b/main.go @@ -132,9 +132,10 @@ Documentation: https://github.com/marshyski/pal defer dbc.Close() - // TODO: Update old actions with new YAML values and delete old actions + // Update old DB data with new values from pal-actions.yml config + mergedGroups := utils.MergeGroups(dbc.GetGroups(), groups) - err = db.DBC.PutGroups(groups) + err = db.DBC.PutGroups(mergedGroups) if err != nil { // TODO: DEBUG STATEMENT log.Println(err.Error()) diff --git a/test/pal-actions.yml b/test/pal-actions.yml index 43ffb03..a8a3ebd 100644 --- a/test/pal-actions.yml +++ b/test/pal-actions.yml @@ -78,6 +78,7 @@ test: desc: Test Background Action Failure background: true concurrent: true + output: true resp_headers: - header: Access-Control-Allow-Origin value: "*" diff --git a/utils/utils.go b/utils/utils.go index d418437..b92fd40 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -117,3 +117,50 @@ func GetCmd(action data.ActionData) (string, error) { return "", errors.New("error cmd is empty for action") } + +func MergeGroups(oldGroups, newGroups map[string][]data.ActionData) map[string][]data.ActionData { + for group, newGroupData := range newGroups { + if oldGroupData, ok := oldGroups[group]; ok { + // Group exists in the old map, update its actions + for _, newAction := range newGroupData { + found := false + for i, oldAction := range oldGroupData { + if newAction.Action == oldAction.Action { + // Update existing action + oldGroups[group][i] = updateAction(oldAction, newAction) + found = true + break + } + } + if !found { + // Add new action + oldGroups[group] = append(oldGroups[group], newAction) + } + } + } else { + // Group doesn't exist in the old map, add it + oldGroups[group] = newGroupData + } + } + + return oldGroups +} + +func updateAction(oldAction, newAction data.ActionData) data.ActionData { + // Update fields (all except the excluded ones) + oldAction.Group = newAction.Group + oldAction.Desc = newAction.Desc + oldAction.Background = newAction.Background + oldAction.Concurrent = newAction.Concurrent + oldAction.AuthHeader = newAction.AuthHeader + oldAction.Output = newAction.Output + oldAction.Timeout = newAction.Timeout + oldAction.Cmd = newAction.Cmd + oldAction.ResponseHeaders = newAction.ResponseHeaders + oldAction.Cron = newAction.Cron + oldAction.OnError = newAction.OnError + oldAction.InputValidate = newAction.InputValidate + oldAction.Tags = newAction.Tags + + return oldAction +}