-
Notifications
You must be signed in to change notification settings - Fork 9
/
hooks.go
64 lines (51 loc) · 2.17 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package planninghooks
import (
"context"
"fmt"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var registrations struct {
prepare map[string]PrepareHookFunc
}
type ProvisionResult struct {
ProvisionInput []rtypes.ProvisionInput
SerializedProvisionInput []*schema.SerializedMessage
Extension []*schema.DefExtension
ServerExtension []*schema.ServerExtension
}
type InternalPrepareProps struct {
ComputePlanWith []*schema.Invocation // Will generate further plan contents.
ProvisionResult
}
func (p *InternalPrepareProps) AppendWith(rhs InternalPrepareProps) {
p.ComputePlanWith = append(p.ComputePlanWith, rhs.ComputePlanWith...)
p.ProvisionResult.AppendWith(rhs.ProvisionResult)
}
func (p *ProvisionResult) AppendWith(rhs ProvisionResult) {
p.ProvisionInput = append(p.ProvisionInput, rhs.ProvisionInput...)
p.SerializedProvisionInput = append(p.SerializedProvisionInput, rhs.SerializedProvisionInput...)
p.Extension = append(p.Extension, rhs.Extension...)
p.ServerExtension = append(p.ServerExtension, rhs.ServerExtension...)
}
type PrepareHookFunc func(context.Context, cfg.Context, *schema.Stack_Entry) (*InternalPrepareProps, error)
func RegisterPrepareHook(name string, f PrepareHookFunc) {
if registrations.prepare == nil {
registrations.prepare = map[string]PrepareHookFunc{}
}
registrations.prepare[name] = f
}
func InvokeInternalPrepareHook(ctx context.Context, name string, env cfg.Context, srv *schema.Stack_Entry) (*InternalPrepareProps, error) {
if f, ok := registrations.prepare[name]; ok {
return tasks.Return(ctx, tasks.Action("prepare.invoke-hook").Scope(srv.GetPackageName()).Arg("name", name), func(ctx context.Context) (*InternalPrepareProps, error) {
return f(ctx, env, srv)
})
}
return nil, fnerrors.New(fmt.Sprintf("%s: does not exist", name))
}