node icon indicating copy to clipboard operation
node copied to clipboard

Normalize codebase to accessing configuration through viper

Open hydrogen18 opened this issue 5 years ago • 3 comments

In the code base we commonly declare flags using the pflag package

cmd.Flags().BoolP("follow", "f", false, "Specify if the logs should be streamed. Defaults to false")

We also do stuff like this

viper.BindPFlag(FlagDeploymentIngressExposeLBHosts, cmd.Flags().Lookup(FlagDeploymentIngressExposeLBHosts))

This "binds" a flag into Viper so that the provided value can be picked up.

The problem is we have code that does this cmd.Flags().GetBool("follow"). When the config is accessed this way we can't actually pick up an env. var. if the user configured it that way.

We need to change all the commands to

  1. Declare configuration using cmd.Flags().String() (or equivalent)
  2. Setup viper with the appropriate prefix & turn on automatic environmental variables
  3. Access all configuration via viper (and no other mechanism) so that our code can use the configuration precedence in Viper and users may configure things however they wish.

hydrogen18 avatar Nov 12 '20 23:11 hydrogen18

I fucking hate viper, but yeah, okay.

It is so easy to make a package that populates flags from environment variables, it's ridiculous that everyone uses viper.

/rant

boz avatar Nov 16 '20 10:11 boz

I believe this is done by now. What do you think @hydrogen18 ?

boz avatar Dec 16 '20 05:12 boz

There are still places where we access the flags directly like this

https://github.com/ovrclk/akash/blob/master/cmd/akash/cmd/genaccounts.go#L80

I think some changes @troian made did some sort of bidirectional binding, which may have mitigated this but I haven't been through all of what he did in his workaround yet.

hydrogen18 avatar Dec 16 '20 15:12 hydrogen18