api_log_arguments.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package api
  2. import (
  3. "fmt"
  4. "maps"
  5. "sort"
  6. "strings"
  7. "connectrpc.com/connect"
  8. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  9. "github.com/OliveTin/OliveTin/internal/executor"
  10. )
  11. func logEntryArgumentsToProto(args map[string]string) []*apiv1.StartActionArgument {
  12. if len(args) == 0 {
  13. return nil
  14. }
  15. names := make([]string, 0, len(args))
  16. for name := range args {
  17. names = append(names, name)
  18. }
  19. sort.Strings(names)
  20. out := make([]*apiv1.StartActionArgument, 0, len(names))
  21. for _, name := range names {
  22. out = append(out, &apiv1.StartActionArgument{
  23. Name: name,
  24. Value: args[name],
  25. })
  26. }
  27. return out
  28. }
  29. func copyStringMap(source map[string]string) map[string]string {
  30. copied := make(map[string]string, len(source))
  31. maps.Copy(copied, source)
  32. return copied
  33. }
  34. func restartArgumentsIncompleteError() error {
  35. return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("stored arguments are incomplete for restart; use StartAction with the required arguments instead"))
  36. }
  37. func validateRestartLogEntry(entry *executor.InternalLogEntry) error {
  38. if entry.Binding.Action.RequiresJustification() && strings.TrimSpace(entry.Justification) == "" {
  39. return restartRequiresJustificationError()
  40. }
  41. if executor.RestartArgumentsIncomplete(entry.Binding.Action, entry.Binding.Entity, entry.Arguments) {
  42. return restartArgumentsIncompleteError()
  43. }
  44. return nil
  45. }