Download this file
| 1 | package hostwrite |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | var ErrUnavailable = errors.New("hostwrite: lifecycle provider unavailable") |
| 10 | |
| 11 | type Identity struct { |
| 12 | Config string |
| 13 | Release string |
| 14 | } |
| 15 | |
| 16 | type Work interface { |
| 17 | With(context.Context, Identity, func() error) error |
| 18 | } |
| 19 | |
| 20 | type Selector interface { |
| 21 | Select(context.Context, string, string) (Work, error) |
| 22 | } |
| 23 | |
| 24 | type Config struct { |
| 25 | Enabled bool |
| 26 | Selector Selector |
| 27 | } |
| 28 | |
| 29 | func Enabled(getenv func(string) string) bool { |
| 30 | return strings.TrimSpace(getenv("CARLOS_HOST_CLAIM")) != "" |
| 31 | } |
| 32 | |
| 33 | func (c Config) Select(ctx context.Context, host, action string) (Work, error) { |
| 34 | if !c.Enabled { |
| 35 | return legacy{}, nil |
| 36 | } |
| 37 | if c.Selector == nil { |
| 38 | return nil, ErrUnavailable |
| 39 | } |
| 40 | return c.Selector.Select(ctx, host, action) |
| 41 | } |
| 42 | |
| 43 | type legacy struct{} |
| 44 | |
| 45 | // Legacy reports whether w is the flag-off pass-through work, so callers |
| 46 | // can keep their exact pre-claim behaviour when the flag is unset. |
| 47 | func Legacy(w Work) bool { |
| 48 | _, ok := w.(legacy) |
| 49 | return ok |
| 50 | } |
| 51 | |
| 52 | func (legacy) With(_ context.Context, _ Identity, fn func() error) error { return fn() } |
| 53 | |