Skip to content

Commit

Permalink
env config implementation (#2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Sep 25, 2020
1 parent 4028e01 commit 6e2c9e7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions config/env/env.go
@@ -0,0 +1,48 @@
// Package env provides config from environment variables
package env

import (
"encoding/json"
"os"
"strings"

"github.com/micro/go-micro/v3/config"
)

type envConfig struct{}

// NewConfig returns new config
func NewConfig() (*envConfig, error) {
return new(envConfig), nil
}

func formatKey(v string) string {
if len(v) == 0 {
return ""
}

v = strings.ToUpper(v)
return strings.Replace(v, ".", "_", -1)
}

func (c *envConfig) Get(path string, options ...config.Option) (config.Value, error) {
v := os.Getenv(formatKey(path))
if len(v) == 0 {
v = "{}"
}
return config.NewJSONValue([]byte(v)), nil
}

func (c *envConfig) Set(path string, val interface{}, options ...config.Option) error {
key := formatKey(path)
v, err := json.Marshal(val)
if err != nil {
return err
}
return os.Setenv(key, string(v))
}

func (c *envConfig) Delete(path string, options ...config.Option) error {
v := formatKey(path)
return os.Unsetenv(v)
}

0 comments on commit 6e2c9e7

Please sign in to comment.