func LoadConfigForRoot()

in pkg/config/config.go [94:138]


func LoadConfigForRoot(configFileFlagName string, cmdDefaults map[string]interface{}) {

	// Set config file following Viper's precedence, in order to allow Viper to apply correct precedence for values in config file
	// 1) config file name in flag
	// 2) config file name in env variable
	// 3) default config file in user's HOME dir
	configFile := viper.GetString(configFileFlagName)
	if configFile != "" {
		viper.SetConfigFile(configFile)
	} else if val, ok := os.LookupEnv(cfgFileEnvKey); ok {
		viper.SetConfigFile(val)
	} else if home != "" {
		// Search for config file in home directory by name, without including extension
		viper.SetConfigName(defaultCfgFileName)
		viper.AddConfigPath(home)
	}

	// set up env
	viper.AutomaticEnv()
	viper.AllowEmptyEnv(true)  // empty strings are perfectly valid values for our usecases
	viper.SetEnvPrefix("aemm") // AEMM stands for amazon-ec2-metadata-mock, set prefix to avoid name clashes
	viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))

	// set up defaults
	LoadConfigFromDefaults(cmdDefaults)
	SetMetadataDefaults(defaults.GetDefaultValues())
	SetDynamicDefaults(defaults.GetDefaultValues())
	SetUserdataDefaults(defaults.GetDefaultValues())
	SetServerCfgDefaults()

	// read in config using viper
	if err := viper.ReadInConfig(); err != nil {
		switch err.(type) {
		case viper.ConfigFileNotFoundError:
			log.Println("Warning: ", err)
		default:
			log.Fatalf("Error while attempting to apply overrides from %s: %s\n", viper.ConfigFileUsed(), err)
		}
	} else {
		fmt.Println("Using configuration from file: ", viper.ConfigFileUsed())
	}

	// if config overrides update placeholder values, then paths with placeholders need to be updated as well
	overrideMetadataPathsWithPlaceholders()
}