carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostwrite
2
3import (
4 "context"
5 "errors"
6 "strings"
7)
8
9var ErrUnavailable = errors.New("hostwrite: lifecycle provider unavailable")
10
11type Identity struct {
12 Config string
13 Release string
14}
15
16type Work interface {
17 With(context.Context, Identity, func() error) error
18}
19
20type Selector interface {
21 Select(context.Context, string, string) (Work, error)
22}
23
24type Config struct {
25 Enabled bool
26 Selector Selector
27}
28
29func Enabled(getenv func(string) string) bool {
30 return strings.TrimSpace(getenv("CARLOS_HOST_CLAIM")) != ""
31}
32
33func (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
43type 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.
47func Legacy(w Work) bool {
48 _, ok := w.(legacy)
49 return ok
50}
51
52func (legacy) With(_ context.Context, _ Identity, fn func() error) error { return fn() }
53