Skip to content

Commit

Permalink
Keep old DB data and update config values from pal-actions.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
marshyski committed Oct 15, 2024
1 parent 7ca6898 commit cf6802a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions test/pal-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ test:
desc: Test Background Action Failure
background: true
concurrent: true
output: true
resp_headers:
- header: Access-Control-Allow-Origin
value: "*"
Expand Down
47 changes: 47 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit cf6802a

Please sign in to comment.