-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package defaults | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func Env[T any, R any](envVar string, defaultValue T) R { | ||
return envDefaultValue{ | ||
envVar: envVar, | ||
defaultValue: defaultValue, | ||
} | ||
} | ||
|
||
type envDefaultValue struct { | ||
envVar string | ||
defaultValue string | ||
} | ||
|
||
func (d envDefaultValue) Description(_ context.Context) string { | ||
return fmt.Sprintf("If value is not configured, defaults to a string representation of the provided env variable") | ||
} | ||
|
||
func (d envDefaultValue) MarkdownDescription(_ context.Context) string { | ||
return fmt.Sprintf("If value is not configured, defaults to a string representation of the provided env variable") | ||
} | ||
|
||
func (d envDefaultValue) DefaultString(_ context.Context, req defaults.StringRequest, resp *defaults.StringResponse) { | ||
resp.PlanValue = types.StringValue(os.Getenv(d.envVar)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters