Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env var substitution inside config file #418

Closed
PepijnK opened this issue Nov 21, 2017 · 7 comments
Closed

env var substitution inside config file #418

PepijnK opened this issue Nov 21, 2017 · 7 comments
Labels
resolution/wontfix This will not be worked on

Comments

@PepijnK
Copy link

PepijnK commented Nov 21, 2017

Considering the following example YAML file excerpt:

---
version: 1
timeout: 3000ms
token_introspection_endpoint: ${DOWNSTREAM_SERVICES_URL}/auth/token

While reading the config, the value ${DOWNSTREAM_SERVICES_URL} should be replaced with env var value of DOWNSTREAM_SERVICES_URL.

Is there some decoder hook I can use to implement this substitution?

@natemurthy
Copy link

natemurthy commented Nov 25, 2017

I'm looking for a similar solution. My current application has config files that look like this:

database:
  host: $HOST
  dbname: $DBNAME
  username: $USERNAME
  password: $PASSWORD

and I would like to continue to use these configuration files as-is with the viper library. Do you have examples of using environment variable substitution?

@leandro-lugaresi
Copy link

This will help me to improve an application. I need some way to set the base URL for n HTTP consumers:

    consumer-name-1:
      connection: default
      queue:
        name: "wololo"
        options:
          durable: true
      runner:
        type: http
        options:
          url: https://myurl/app/message/wololo-handler"
    consumer-name-2:
      connection: default
      queue:
        name: "fooo"
        options:
          durable: true
      runner:
        type: http
        options:
          url: https://myurl/app/message/fooo-handler"

I can help if needed!

@lvqingan
Copy link

try godotenv

@chen56
Copy link

chen56 commented Aug 12, 2021

Why not support this feature?

@sagikazarmark
Copy link
Collaborator

There are better ways for this that doesn't require a direct integration in Viper:

  • There are solutions that can do that for you (eg. dockerize, gomplate, etc)
  • You can do it yourself by reading the file separately and doing any replacement necessary

Personally, I think if you use env vars anyway, it's just better to load them using Viper instead of replacing values in a config file.

Since there are alternatives available, I'm going to close this as won't fix. Feel free to open a new issue if you think you have a strong case for implementing this feature.

@sagikazarmark sagikazarmark added the resolution/wontfix This will not be worked on label Aug 16, 2021
@hjizettle
Copy link

This would be super convenient actually. I hope you can reconsider.

@pkit
Copy link

pkit commented Feb 18, 2023

@PepijnK

Is there some decoder hook I can use to implement this substitution?

Yes
Obviously it will work only if you use string -> string substitution which is probably what you want in 99% of cases

func StringExpandEnv() mapstructure.DecodeHookFuncKind {
	return func(
		f reflect.Kind,
		t reflect.Kind,
		data interface{}) (interface{}, error) {
		if f != reflect.String || t != reflect.String {
			return data, nil
		}

		return os.ExpandEnv(data.(string)), nil
	}
}

err := viper.Unmarshal(&config, viper.DecodeHook(StringExpandEnv()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution/wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

8 participants