From 62cec219cf0cc14c02b39d8006ae35b9d163cc7e Mon Sep 17 00:00:00 2001 From: luigibk <41112628+luigibk@users.noreply.github.com> Date: Wed, 16 Mar 2022 01:12:44 +0000 Subject: [PATCH] feat: 429 add debug mode (#430) * feat: 429 add debug mode * clean up * Add const comment * fix: make build gcflags issue --- CONTRIBUTING.md | 22 ++++++++++++++++++++++ build/Makefile.build | 4 ++-- main.go | 24 +++++++++++++++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 77aca4d97..a02ab143f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -127,3 +127,25 @@ $ make build ``` You can also use the `make install` target if you wish. This target will install the binary and move it to your Terraform plugin location. + +### Debugging + +This provider supports debugger-based debugging as described in the related [Terraform SDK documentation](https://www.terraform.io/plugin/sdkv2/debugging#debugger-based-debugging). To build a provider with the necessary Go compiler flags run: + +```console +$ make build GCFLAGS='-gcflags="all=-N -l"' +``` + +To run the provider and connect it to your debugger run: + +```console +dlv --listen=:60324 --headless=true --api-version=2 --accept-multiclient exec ./bin/terraform-provider-ec -- --debug +``` + +Connect your debugger using port 60234 as target. The provider should output a line containing the following variable: + +```console +TF_REATTACH_PROVIDERS='{"registry.terraform.io/elastic/ec":{"Protocol":"grpc","ProtocolVersion":5,"Pid":13197,"Test":true,"Addr":{"Network":"unix","String":"/tmp/plugin977285207"}}}' +``` + +which you can export or use to prefix every Terraform command. If that variable is not printed as soon as you connect your debugger, pausing execution and restarting it might do the trick. diff --git a/build/Makefile.build b/build/Makefile.build index 03f1e3c39..2988f1553 100644 --- a/build/Makefile.build +++ b/build/Makefile.build @@ -19,8 +19,8 @@ generate: gen ## Builds the source code and saves the binary to bin/terraform-provider-ec. .PHONY: build build: gen - @ echo "-> Building binary in $(BINARY_LOCATION)..." - @ go build -o $(BINARY_LOCATION) . + @ echo "-> Building binary with GCFLAGS=$(GCFLAGS) in $(BINARY_LOCATION)..." + @ go build $(GCFLAGS) -o $(BINARY_LOCATION) . ## Builds the source code and moves the binary to the user's terraform plugin location. .PHONY: install diff --git a/main.go b/main.go index f8f1bbf10..b861dfa41 100644 --- a/main.go +++ b/main.go @@ -18,14 +18,32 @@ package main import ( + "context" + "flag" "github.com/elastic/terraform-provider-ec/ec" "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" + "log" ) //go:generate go run ./gen/gen.go +// ProviderName contains the full name for this terraform provider. +const ProviderName = "registry.terraform.io/elastic/ec" + func main() { - plugin.Serve(&plugin.ServeOpts{ - ProviderFunc: ec.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: ec.Provider} + + if debugMode { + err := plugin.Debug(context.Background(), ProviderName, opts) + if err != nil { + log.Fatal(err.Error()) + } + return + } + + plugin.Serve(opts) }