Skip to content

Commit

Permalink
provider: Add -debug flag to main binary (hashicorp#16565)
Browse files Browse the repository at this point in the history
Reference: https://github.com/hashicorp/terraform-provider-scaffolding/blob/3c87a86ff2eeb6b0835e26b16f2bf8bc293622fd/main.go#L22-L38

To match recent updates to the scaffold provider, this allows easier usage of Go debugging tooling, such as delve, via a provider reattachment mode. This is primarily for provider developers and does not affect practitioner usage.

Example usage:

```console
$ go run . -debug
{"@Level":"debug","@message":"plugin address","@timestamp":"2020-12-02T21:23:22.095285-05:00","address":"/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/plugin326120673","network":"unix"}
Provider started, to attach Terraform set the TF_REATTACH_PROVIDERS env var:

	TF_REATTACH_PROVIDERS='{"registry.terraform.io/hashicorp/aws":{"Protocol":"grpc","Pid":45042,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/plugin326120673"}}}'
```
  • Loading branch information
bflad authored Dec 3, 2020
1 parent 1ef07ea commit 1e755a5
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package main

import (
"context"
"flag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/terraform-providers/terraform-provider-aws/aws"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: aws.Provider})
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: aws.Provider}

if debugMode {
err := plugin.Debug(context.Background(), "registry.terraform.io/hashicorp/aws", opts)

if err != nil {
log.Fatal(err.Error())
}

return
}

plugin.Serve(opts)
}

0 comments on commit 1e755a5

Please sign in to comment.