generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
115 lines (98 loc) · 2.54 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/hashicorp/go-plugin"
server "github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
"github.com/paultyng/terraform-provider-sql/internal/provider"
)
// Generate docs for website
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
var (
// these will be set by the goreleaser configuration
// to appropriate values for the compiled binary
version string = "dev"
// goreleaser can also pass the specific commit if you want
// commit string = ""
)
const (
providerAddr = "registry.terraform.io/paultyng/sql"
)
func main() {
var debugMode bool
flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()
ctx := context.Background()
opts := []server.ServeOpt{}
close := make(chan struct{})
if debugMode {
reattach := make(chan *plugin.ReattachConfig)
go func() {
for {
select {
case conf := <-reattach:
reattachBytes, err := json.Marshal(map[string]struct {
Protocol string
Pid int
Test bool
Addr struct {
Network string
String string
}
}{
providerAddr: {
Protocol: string(conf.Protocol),
Pid: conf.Pid,
Test: conf.Test,
Addr: struct {
Network string
String string
}{
Network: conf.Addr.Network(),
String: conf.Addr.String(),
},
},
})
if err != nil {
panic(fmt.Sprintf("Error building reattach string: %s", err))
}
reattachStr := string(reattachBytes)
fmt.Printf("Provider started, to attach Terraform set the TF_REATTACH_PROVIDERS env var:\n\n")
switch runtime.GOOS {
case "windows":
fmt.Printf("\tCommand Prompt:\tset \"TF_REATTACH_PROVIDERS=%s\"\n", reattachStr)
fmt.Printf("\tPowerShell:\t$env:TF_REATTACH_PROVIDERS='%s'\n", strings.ReplaceAll(reattachStr, `'`, `''`))
case "linux", "darwin":
fmt.Printf("\tTF_REATTACH_PROVIDERS='%s'\n", strings.ReplaceAll(reattachStr, `'`, `'"'"'`))
default:
fmt.Println(reattachStr)
}
fmt.Println("")
case <-close:
case <-ctx.Done():
}
}
}()
opts = append(opts, server.WithDebug(ctx, reattach, close))
}
var err error
go func() {
err = server.Serve(providerAddr, provider.New(version), opts...)
}()
select {
case <-close:
case <-ctx.Done():
}
if err == nil {
err = ctx.Err()
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}