master
go 82 lines 1.97 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dyncfg
4
5 import (
6 "errors"
7 "strings"
8 )
9
10 // Status represents the state of a dyncfg entity
11 type Status string
12
13 const (
14 StatusAccepted Status = "accepted"
15 StatusRunning Status = "running"
16 StatusFailed Status = "failed"
17 StatusIncomplete Status = "incomplete"
18 StatusDisabled Status = "disabled"
19 )
20
21 func (s Status) String() string {
22 return string(s)
23 }
24
25 type ConfigType string
26
27 const (
28 ConfigTypeTemplate ConfigType = "template"
29 ConfigTypeJob ConfigType = "job"
30 )
31
32 func (t ConfigType) String() string {
33 return string(t)
34 }
35
36 type Command string
37
38 const (
39 CommandAdd Command = "add"
40 CommandRemove Command = "remove"
41 CommandGet Command = "get"
42 CommandUpdate Command = "update"
43 CommandRestart Command = "restart"
44 CommandEnable Command = "enable"
45 CommandDisable Command = "disable"
46 CommandTest Command = "test"
47 CommandSchema Command = "schema"
48 CommandUserconfig Command = "userconfig"
49 )
50
51 func JoinCommands(commands ...Command) string {
52 strs := make([]string, len(commands))
53 for i, cmd := range commands {
54 strs[i] = string(cmd)
55 }
56 return strings.Join(strs, " ")
57 }
58
59 // ErrNonDisruptiveUpdate marks update failures where runtime state was not changed.
60 // Handler rollback logic uses this marker to keep old config/status authoritative.
61 var ErrNonDisruptiveUpdate = errors.New("non-disruptive update")
62
63 type nonDisruptiveUpdateError struct {
64 err error
65 }
66
67 func (e *nonDisruptiveUpdateError) Error() string { return e.err.Error() }
68 func (e *nonDisruptiveUpdateError) Unwrap() error { return e.err }
69 func (e *nonDisruptiveUpdateError) Is(target error) bool { return target == ErrNonDisruptiveUpdate }
70
71 // MarkNonDisruptiveUpdate wraps err to indicate update failed before disrupting runtime.
72 func MarkNonDisruptiveUpdate(err error) error {
73 if err == nil {
74 return nil
75 }
76
77 if errors.Is(err, ErrNonDisruptiveUpdate) {
78 return err
79 }
80
81 return &nonDisruptiveUpdateError{err: err}
82 }