Giter VIP home page Giter VIP logo

Comments (1)

tahacodes avatar tahacodes commented on July 30, 2024

Hi there

So if i understand this currently, you need to load a config file into your application but some of the variables need to be set by environment variables.

I had a similar experience and this little trick may be helpful:

if errConfig := cleanenv.ReadConfig("config.yaml", configuration); errConfig != nil {
	if errEnv := cleanenv.ReadEnv(configuration); errEnv != nil {
		return nil, multierr.Combine(errConfig, errEnv)
	}
}

Basically you can load your configs from a config.yaml file but you can also load some other variables from env after that and you can use the uber's "multierr" package to combine ReadConfig and ReadEnv errors together. Here's an example of the struct that i used:

type Configuration struct {
	Dialer struct {
		UpstreamTimeout    string `env:"DIALER_UPSTREAM_TIMEOUT" env-default:"2s"`
		KeepAliveDuration  string `env:"DIALER_KEEPALIVE_DURATION" env-default:"5s"`
		InsecureSkipVerify bool   `env:"DIALER_INSECURE_SKIP_VERIFY" env-default:"false"`
	}

	DNS struct {
		Resolver string `env:"DNS_RESOLVER" env-default:"8.8.8.8:53"`
		Protocol string `env:"DNS_PROTOCOL" env-default:"udp"`
		Timeout  string `env:"DNS_TIMEOUT" env-default:"10ms"`
	}

	Log struct {
		LogLevel      string `env:"LOG_LEVEL" env-default:"info"`
		SentryDSN     string `env:"SENTRY_DSN"`
		TimePrecision uint   `env:"LOG_TIMEPRECISION" env-default:"3"`
	}

	Storage struct {
		Endpoints []struct {
			Name         string `yaml:"name"`
			Endpoint     string `yaml:"endpoint"`
		} `yaml:"endpoints"`

		Key struct {
			ServiceAlpha struct {
				Access string `env:"STORAGE_ALPHA_ACCESS"`
				Secret string `env:"STORAGE_ALPHA_SECRET"`
			}
			ServiceBeta struct {
				Access string `env:"STORAGE_BETA_ACCESS"`
				Secret string `env:"STORAGE_BETA_SECRET"`
			}
		}
	} `yaml:"storage"`
}

As you can see i have a combination of yaml configs and env configs.

Here's the config.yaml as well:

storage:
  endpoints:
    - name: Main
      endpoint: https://s3.us-east-2.amazonaws.com
    - name: Fallback
      endpoint: https://s3.us-west-2.amazonaws.com

Hope this helps.

from cleanenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.