pulumi/sdk/go/common/resource/plugin
bors[bot] 40e0ac20c9
Merge #11362
11362: Library for environmental variable access r=iwahbe a=iwahbe

Add a library to encapsulate environmental variable declaration, lookup and parsing. 

### Usage
Variables strongly typed and declared in the module scope like so:
```go
// Automatically prefixed with PULUMI_ by default
var Experimental = env.Bool("EXPERIMENTAL", "Enable experimental options and commands")
```

Values are accessed by calling `.Value()` on the module level variable:
```go
if Experimental.Value() { // .Value() returns a bool here
  // do the new exiting thing
} else {
  // do the well tested thing
}
```

Environmental variables can be predicated on other boolean type variables:
```go
var NewThingParam = env.String("NEW_THING", "pass this to the new thing", 
    env.Needs(Expiremental))
```

Predicated variables will only show up as set if there predicate is `true`:

```go
if v := NewThingParam.Value(); v != "" {
  do_thing(v)
} else {
  // do the well tested thing
}
```
The above `if` check is equivalent to 
```go
if v = os.Getenv("PULUMI_NEW_THING"); v != "" && cmduilt.IsTruthy(os.Getenv("PULUMI_EXPERIMENTAL"))
```

This makes marking and unmarking a variable as experimental a 1 line change.

--- 

All declared variables can then be iterated on for documentation with `env.Variables()`. This is how we can use this lib to build documentation for our environmental variables.

### Assumptions

#### Immutability 
The library assumes that environmental variables are inherited from the environment. Values are captured during program startup, changes to the environment are not observed. We are not resilient to environmental variables changing out from under us, so this complies with current usage. This assumption can be changed without breaking the API.

#### Known values
The library assumes that the names of environmental variables are known at compile time. If the variable to be looked up is derived from user input, a different mechanism should be used.

### Adoption

Adopting this system over our pulumi/pkg codebase will take a while. My plan is to merge in this PR without moving over our existing env vars from `os.Getenv`. We can then gradually move over existing env vars piecemeal in subsequent PRs. Any new env vars added will use the new system.

Prerequisite for https://github.com/pulumi/pulumi/issues/10747.

### Other Options - Viper
Viper looks like an excellent config retrieval package, but doesn't support at-site documentation. It also has less type safety then I would prefer as a top level api. If it would be helpful, we could switch the underlying mechanism from `os.Lookup` to `viper.Get*` without changing the API this library exposes. I don't think viper is necessary right now, but adopting this library doesn't preclude viper. 

Co-authored-by: Ian Wahbe <ian@wahbe.com>
2022-12-15 12:15:34 +00:00
..
analyzer.go feat(engine): Adds structured alias support to the engine 2022-10-11 17:56:32 -04:00
analyzer_plugin.go Engine and Golang support for shimless providers 2022-11-14 11:25:41 +00:00
check.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
config_source.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
context.go Introduce PULUMI_DEBUG_GRPC support 2022-11-08 14:01:15 -05:00
doc.go Provider implementer's guide draft (#6322) 2021-04-13 14:11:02 -07:00
host.go Example of usage and test injection 2022-12-14 15:41:42 +01:00
host_server.go Introduce PULUMI_DEBUG_GRPC support 2022-11-08 14:01:15 -05:00
langruntime.go Engine and Golang support for shimless providers 2022-11-14 11:25:41 +00:00
langruntime_plugin.go Engine and Golang support for shimless providers 2022-11-14 11:25:41 +00:00
plugin.go Engine and Golang support for shimless providers 2022-11-14 11:25:41 +00:00
plugin_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
provider.go Phase 3 of the convert mapper 2022-12-01 23:43:43 +00:00
provider_plugin.go Phase 3 of the convert mapper 2022-12-01 23:43:43 +00:00
provider_plugin_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
provider_server.go Make the PluginMapper lazy 2022-12-14 15:26:33 +00:00
provider_test.go Add forward compatible UnimplementedProvider for bridge 2022-12-09 18:44:28 -08:00
provider_unimplemented.go Add forward compatible UnimplementedProvider for bridge 2022-12-09 18:44:28 -08:00
rpc.go [sdk/go] Marshal output values (#7958) 2021-09-27 09:01:40 -07:00
rpc_rapid_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
rpc_test.go chore: Update doc comments, coding style, fix lint 2022-10-13 13:50:49 -07:00