2
0

os_env_driver.go 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package preview
  2. import (
  3. "os"
  4. "strings"
  5. "github.com/porter-dev/switchboard/pkg/drivers"
  6. "github.com/porter-dev/switchboard/pkg/models"
  7. )
  8. type OSEnvDriver struct {
  9. output map[string]interface{}
  10. }
  11. func NewOSEnvDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
  12. return &OSEnvDriver{
  13. output: make(map[string]interface{}),
  14. }, nil
  15. }
  16. func (d *OSEnvDriver) ShouldApply(resource *models.Resource) bool {
  17. return true
  18. }
  19. func (d *OSEnvDriver) Apply(resource *models.Resource) (*models.Resource, error) {
  20. for _, key := range os.Environ() {
  21. keyVal := strings.Split(key, "=")
  22. if len(keyVal) == 2 && keyVal[0] != "" && keyVal[1] != "" &&
  23. strings.HasPrefix(keyVal[0], "PORTER_APPLY_") {
  24. envName := strings.TrimPrefix(keyVal[0], "PORTER_APPLY_")
  25. if len(envName) > 0 {
  26. d.output[envName] = keyVal[1]
  27. }
  28. }
  29. }
  30. return resource, nil
  31. }
  32. func (d *OSEnvDriver) Output() (map[string]interface{}, error) {
  33. return d.output, nil
  34. }