-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
90 lines (76 loc) · 2.31 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"strings"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)
// Run "go generate" to format example terraform files and generate the docs for the registry/website
// If you do not have terraform installed, you can remove the formatting command, but its suggested to
// ensure the documentation is formatted properly.
//go:generate terraform fmt -recursive ./examples/
// Run the docs generation tool, check its repository for more information on how it works and how docs
// can be customized.
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
// Same us plugin.Debug with simpler output to parse
func debug(ctx context.Context, providerAddr string, opts *plugin.ServeOpts) error {
ctx, cancel := context.WithCancel(ctx)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
defer func() {
signal.Stop(sigCh)
cancel()
}()
config, closeCh, err := plugin.DebugServe(ctx, opts)
if err != nil {
return fmt.Errorf("Error launching debug server: %w", err)
}
go func() {
select {
case <-sigCh:
cancel()
case <-ctx.Done():
}
}()
reattachBytes, err := json.Marshal(map[string]plugin.ReattachConfig{
providerAddr: config,
})
if err != nil {
return fmt.Errorf("Error building reattach string: %w", err)
}
reattachStr := string(reattachBytes)
switch runtime.GOOS {
case "windows":
fmt.Printf("Command Prompt:\tset \"TF_REATTACH_PROVIDERS=%s\"\n", reattachStr)
fmt.Printf("PowerShell:\t$env:TF_REATTACH_PROVIDERS='%s'\n", strings.ReplaceAll(reattachStr, `'`, `''`))
case "linux", "darwin":
fmt.Printf("TF_REATTACH_PROVIDERS='%s'\n", strings.ReplaceAll(reattachStr, `'`, `'"'"'`))
default:
fmt.Println(reattachStr)
}
fmt.Println("")
// wait for the server to be done
<-closeCh
return nil
}
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()
opts := &plugin.ServeOpts{ProviderFunc: provider.Provider}
if debugMode {
err := debug(context.Background(), "checkpointsw/infinity-next", opts)
if err != nil {
log.Fatal(err.Error())
}
return
}
plugin.Serve(opts)
}