generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
80 lines (66 loc) · 2.28 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"context"
"flag"
"log"
"log/slog"
"github.com/chainguard-dev/terraform-provider-apko/internal/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
//go:generate terraform fmt -recursive ./examples/
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
const version string = "dev"
func main() {
var debug bool
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()
opts := providerserver.ServeOpts{
Address: "registry.terraform.io/chainguard-dev/apko",
Debug: debug,
}
if err := providerserver.Serve(context.Background(), provider.New(version), opts); err != nil {
log.Fatal(err.Error())
}
}
func init() {
slog.SetDefault(slog.New(tfhandler{}))
}
type tfhandler struct{ attrs []slog.Attr }
const subsystem = "apko"
func (h tfhandler) Handle(ctx context.Context, r slog.Record) error {
// This is a bit of a hack, but it's the only way to get the correct
// source location for the log message.
//
// This creates a new tflog subsystem for logging, with the location
// offset set to 3, which is the number of frames between this function
// and the actual logging call site. Then we use this subsystem below to log
// the message to TF's logger.
ctx = tflog.NewSubsystem(ctx, subsystem, tflog.WithAdditionalLocationOffset(3))
addl := make(map[string]interface{})
r.Attrs(func(s slog.Attr) bool {
addl[s.Key] = s.Value.String()
return true
})
for _, a := range h.attrs {
addl[a.Key] = a.Value.String()
}
switch r.Level {
case slog.LevelDebug:
tflog.SubsystemDebug(ctx, subsystem, r.Message, addl)
case slog.LevelInfo:
tflog.SubsystemInfo(ctx, subsystem, r.Message, addl)
case slog.LevelWarn:
tflog.SubsystemWarn(ctx, subsystem, r.Message, addl)
case slog.LevelError:
tflog.SubsystemError(ctx, subsystem, r.Message, addl)
default:
tflog.SubsystemInfo(ctx, subsystem, r.Message, addl)
}
return nil
}
func (_ tfhandler) Enabled(context.Context, slog.Level) bool { return true }
func (h tfhandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return tfhandler{attrs: append(h.attrs, attrs...)}
}
func (_ tfhandler) WithGroup(name string) slog.Handler { panic("unimplemented") }