Skip to content

Commit e492882

Browse files
authored
feat: Add WithManagedDebugEnvFilePath() option that allows writing TF_REATTACH_PROVIDERS to an environment file (#484)
* feat: Add WithManagedDebugEnvFilePath() option that allows writing TF_REATTACH_PROVIDERS to an environment file * add changie
1 parent a2cdb7a commit e492882

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: Add WithManagedDebugEnvFilePath() option that allows writing TF_REATTACH_PROVIDERS to an environment file
3+
time: 2025-03-07T15:38:00.962006+01:00
4+
custom:
5+
Issue: "484"

tfprotov5/tf5server/server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type ServeConfig struct {
104104
managedDebug bool
105105
managedDebugReattachConfigTimeout time.Duration
106106
managedDebugStopSignals []os.Signal
107+
managedDebugEnvFilePath string
107108

108109
disableLogInitStderr bool
109110
disableLogLocation bool
@@ -178,6 +179,15 @@ func WithManagedDebugReattachConfigTimeout(timeout time.Duration) ServeOpt {
178179
})
179180
}
180181

182+
// WithManagedDebugEnvFilePath returns a ServeOpt that will set the output path
183+
// for the managed debug process to write the reattach configuration into.
184+
func WithManagedDebugEnvFilePath(path string) ServeOpt {
185+
return serveConfigFunc(func(in *ServeConfig) error {
186+
in.managedDebugEnvFilePath = path
187+
return nil
188+
})
189+
}
190+
181191
// WithGoPluginLogger returns a ServeOpt that will set the logger that
182192
// go-plugin should use to log messages.
183193
func WithGoPluginLogger(logger hclog.Logger) ServeOpt {
@@ -380,6 +390,15 @@ func Serve(name string, serverFactory func() tfprotov5.ProviderServer, opts ...S
380390

381391
fmt.Println("")
382392

393+
if conf.managedDebugEnvFilePath != "" {
394+
fmt.Printf("Writing reattach configuration to env file at path %s\n", conf.managedDebugEnvFilePath)
395+
396+
err = os.WriteFile(conf.managedDebugEnvFilePath, []byte(fmt.Sprintf("%s='%s'\n", envTfReattachProviders, strings.ReplaceAll(reattachStr, `'`, `'"'"'`))), 0644)
397+
if err != nil {
398+
return fmt.Errorf("Error writing to env file at path %s: %w", conf.managedDebugEnvFilePath, err)
399+
}
400+
}
401+
383402
// Wait for the server to be done.
384403
<-conf.debugCloseCh
385404

tfprotov6/tf6server/server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type ServeConfig struct {
104104
managedDebug bool
105105
managedDebugReattachConfigTimeout time.Duration
106106
managedDebugStopSignals []os.Signal
107+
managedDebugEnvFilePath string
107108

108109
disableLogInitStderr bool
109110
disableLogLocation bool
@@ -178,6 +179,15 @@ func WithManagedDebugReattachConfigTimeout(timeout time.Duration) ServeOpt {
178179
})
179180
}
180181

182+
// WithManagedDebugEnvFilePath returns a ServeOpt that will set the output path
183+
// for the managed debug process to write the reattach configuration into.
184+
func WithManagedDebugEnvFilePath(path string) ServeOpt {
185+
return serveConfigFunc(func(in *ServeConfig) error {
186+
in.managedDebugEnvFilePath = path
187+
return nil
188+
})
189+
}
190+
181191
// WithGoPluginLogger returns a ServeOpt that will set the logger that
182192
// go-plugin should use to log messages.
183193
func WithGoPluginLogger(logger hclog.Logger) ServeOpt {
@@ -380,6 +390,15 @@ func Serve(name string, serverFactory func() tfprotov6.ProviderServer, opts ...S
380390

381391
fmt.Println("")
382392

393+
if conf.managedDebugEnvFilePath != "" {
394+
fmt.Printf("Writing reattach configuration to env file at path %s\n", conf.managedDebugEnvFilePath)
395+
396+
err = os.WriteFile(conf.managedDebugEnvFilePath, []byte(fmt.Sprintf("%s='%s'\n", envTfReattachProviders, strings.ReplaceAll(reattachStr, `'`, `'"'"'`))), 0644)
397+
if err != nil {
398+
return fmt.Errorf("Error writing to env file at path %s: %w", conf.managedDebugEnvFilePath, err)
399+
}
400+
}
401+
383402
// Wait for the server to be done.
384403
<-conf.debugCloseCh
385404

0 commit comments

Comments
 (0)