/*
Reads a config file into a specific type. Must match the structure of the
type passed in in order to succeed.
*/
9566
func ReadSpecific(filename string, configObject interface{}) error {
	fileContents, err := ioutil.ReadFile(filename)

	if err == nil {
		json.Unmarshal(fileContents, &configObject)
	}

	return err
}
15383
func ReadConfig(buffer []byte) (*Config, error) {
	config := new(Config)
	if err := yaml.Unmarshal(buffer, &config.Values); err != nil {
		return nil, err
	}

	// As there might be multiple instances of the same plugin class we iterate
	// over an array here.
	hasError := false
	for pluginID, configValues := range config.Values {
		if typeName, _ := configValues.String("Type"); typeName == pluginAggregate {
			// aggregate behavior
			aggregateMap, err := configValues.MarshalMap("Plugins")
			if err != nil {
				hasError = true
				logrus.Error("Can't read 'Aggregate' configuration: ", err)
				continue
			}

			// loop through aggregated plugins and set them up
			for subPluginID, subConfigValues := range aggregateMap {
				subPluginsID := fmt.Sprintf("%s-%s", pluginID, subPluginID)
				subConfig, err := tcontainer.ConvertToMarshalMap(subConfigValues, nil)
				if err != nil {
					hasError = true
					logrus.Error("Error in plugin config ", subPluginsID, err)
					continue
				}

				// set up sub-plugin
				delete(configValues, "Type")
				delete(configValues, "Plugins")

				pluginConfig := NewPluginConfig(subPluginsID, "")
				pluginConfig.Read(configValues)
				pluginConfig.Read(subConfig)

				config.Plugins = append(config.Plugins, pluginConfig)
			}
		} else {
			// default behavior
			pluginConfig := NewPluginConfig(pluginID, "")
			pluginConfig.Read(configValues)
			config.Plugins = append(config.Plugins, pluginConfig)
		}
	}

	if hasError {
		return config, fmt.Errorf("configuration parsing produced errors")
	}
	return config, nil
}
842419
func (b *Builder) ReadFile(path string) (Source, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return Source{}, fmt.Errorf("config: ReadFile failed on %s: %s", path, err)
	}
	return Source{Name: path, Data: string(data)}, nil
}
82557
func loadConfig(configFile string) ([][]string, error) {
	f, err := os.Open(configFile)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	var config [][]string
	if err := json.NewDecoder(f).Decode(&config); err != nil {
		return nil, err
	}
	return config, nil
}
1089406
func ReadFile(cfgpath string, debug bool) (*Config, error) {
	content, err := ioutil.ReadFile(cfgpath)
	if err != nil {
		return nil, fmt.Errorf("error reading config file %q: %v", cfgpath, err)
	}
	if debug {
		log.Printf("Config file %q contents:\n%s", cfgpath, content)
	}
	return GetConfig(string(content), debug)
}
462111
func (s *server) readConf() error {
	confPath := path.Join(s.path, "conf")
	s.debugln("readConf.open ", confPath)

	// open conf file
	b, err := ioutil.ReadFile(confPath)

	if err != nil {
		return nil
	}

	conf := &Config{}

	if err = json.Unmarshal(b, conf); err != nil {
		return err
	}

	s.log.updateCommitIndex(conf.CommitIndex)

	return nil
}
1071059
func (loader *ConfigLoader) Load(target interface{}) error {
	if target == nil {
		return errors.New("target is nil error")
	}
	// fetch all target's field member
	instance := reflect.ValueOf(target).Elem()

	if !instance.IsValid() || instance.Kind() != reflect.Struct {
		return errors.New("target is not a valid struct type")
	}

	// file content reader. handled line by line
	lines := readAllLines(*bufio.NewReader(loader.conf))
	if len(lines) == 0 {
		// empty file or have error
		return errors.New("configuration file is empty or read error")
	}

	for _, line := range lines {
		pair := strings.SplitN(line, "=", 2)
		key := strings.Trim(pair[0], " ")
		if len(pair) != 2 || !onlyAlpha.MatchString(key) {
			return fmt.Errorf("error confiugre key line found. key is %s", key)
		}
		value := strings.Trim(pair[1], " ")

		// key is good. look up the matching tag
		var err error
		var found string
		var genericType int
		if found, genericType, err = lookupTagMember(target, key); err != nil {
			// not found in struct, give error
			return fmt.Errorf("couldn't found structure key's tag '%s'. one must be tagged", key)
		}

		field := instance.FieldByName(found)
		if !field.IsValid() || !field.CanSet() {
			return fmt.Errorf("structure key '%s' is not accessable", key)
		}

		// set config's value
		switch field.Kind() {
		case reflect.String: // for string
			field.SetString(value)
		case reflect.Bool: // for bool
			if v, err := strconv.ParseBool(value); err == nil {
				field.SetBool(v)
			} else {
				return fmt.Errorf("boolean key %s wrong format %v", key, value)
			}
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: // for numberic
			if genericType == GenericTypeDate {
				if t, e := time.ParseInLocation(loader.format, value, time.UTC); e == nil {
					field.SetInt(t.Unix())
				} else {
					return fmt.Errorf("integer conver to date format [%s] from (%s)", loader.format, value)
				}
			} else if v, err := strconv.ParseInt(value, 10, 64); err == nil {
				field.SetInt(v)
			} else {
				return fmt.Errorf("integer key %s wrong format %v", key, value)
			}
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
			if v, err := strconv.ParseUint(value, 10, 64); err == nil {
				field.SetUint(v)
			} else {
				return fmt.Errorf("unsigned integer key %s wrong format %v", key, value)
			}
		case reflect.Slice: // for slice
			if items, err := loader.extractAsList(value); err == nil {
				filtered := make([]string, 0, len(items))
				for _, item := range items {
					if s := strings.TrimSpace(item); len(s) != 0 {
						filtered = append(filtered, s)
					}
				}
				field.Set(reflect.ValueOf(filtered))
			} else {
				return err
			}
		}
	}
	return nil
}
599210
func ReadSpecificDockerConfigJsonFile(filePath string) (cfg DockerConfig, err error) {
	var contents []byte

	if contents, err = ioutil.ReadFile(filePath); err != nil {
		return nil, err
	}
	return readDockerConfigJsonFileFromBytes(contents)
}
278066
func (c *ConfigParser) ReadFile(path string) (err error) {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	return c.Read(f)
}
1373653
func LoadReader(fd io.Reader) (config Project, err error) {
	data, err := ioutil.ReadAll(fd)
	if err != nil {
		return config, err
	}
	err = yaml.UnmarshalStrict(data, &config)
	log.WithField("config", config).Debug("loaded config file")
	return config, err
}
946090
func ReadConfig(reader io.Reader) (*FileConfig, error) {
	// read & parse YAML config:
	bytes, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, trace.Wrap(err, "failed reading Teleport configuration")
	}
	var fc FileConfig
	if err = yaml.Unmarshal(bytes, &fc); err != nil {
		return nil, trace.BadParameter("failed to parse Teleport configuration: %v", err)
	}
	// don't start Teleport with invalid ciphers, kex algorithms, or mac algorithms.
	err = fc.Check()
	if err != nil {
		return nil, trace.BadParameter("failed to parse Teleport configuration: %v", err)
	}
	// now check for unknown (misspelled) config keys:
	var validateKeys func(m YAMLMap) error
	validateKeys = func(m YAMLMap) error {
		var recursive, ok bool
		var key string
		for k, v := range m {
			if key, ok = k.(string); ok {
				if recursive, ok = validKeys[key]; !ok {
					return trace.BadParameter("unrecognized configuration key: '%v'", key)
				}
				if recursive {
					if m2, ok := v.(YAMLMap); ok {
						if err := validateKeys(m2); err != nil {
							return err
						}
					}
				}
			}
		}
		return nil
	}
	// validate configuration keys:
	var tmp YAMLMap
	if err = yaml.Unmarshal(bytes, &tmp); err != nil {
		return nil, trace.BadParameter("error parsing YAML config")
	}
	if err = validateKeys(tmp); err != nil {
		return nil, trace.Wrap(err)
	}
	return &fc, nil
}
311935
func ReadConfigFile(cliConfigPath string) (*FileConfig, error) {
	configFilePath := defaults.ConfigFilePath
	// --config tells us to use a specific conf. file:
	if cliConfigPath != "" {
		configFilePath = cliConfigPath
		if !fileExists(configFilePath) {
			return nil, trace.Errorf("file not found: %s", configFilePath)
		}
	}
	// default config doesn't exist? quietly return:
	if !fileExists(configFilePath) {
		log.Info("not using a config file")
		return nil, nil
	}
	log.Debug("reading config file: ", configFilePath)
	return ReadFromFile(configFilePath)
}
536429
func readConfig(configFile string) *core.Config {
	if *flagHelp || configFile == "" {
		logrus.Error("Please provide a config file")
		return nil
	}

	config, err := core.ReadConfigFromFile(configFile)
	if err != nil {
		logrus.WithError(err).Error("Failed to read config")
		return nil
	}

	if err := config.Validate(); err != nil {
		logrus.WithError(err).Error("Config validation failed")
		return nil
	}

	return config
}
282111
func detectConfigFileType(path, def string) string {
	switch ext := filepath.Ext(path); ext {
	case ".toml":
		return "toml"
	case ".yaml", ".yml":
		return "yaml"
	case ".json":
		return "json"
	default:
		return def
	}
}
297591
func MatchReader(reader io.Reader) (types.Type, error) {
	buffer := make([]byte, 8192) // 8K makes msooxml tests happy and allows for expanded custom file checks

	_, err := reader.Read(buffer)
	if err != nil && err != io.EOF {
		return types.Unknown, err
	}

	return Match(buffer)
}
1257645
func MatchFile(filepath string) (types.Type, error) {
	file, err := os.Open(filepath)
	if err != nil {
		return types.Unknown, err
	}
	defer file.Close()

	return MatchReader(file)
}
1324489
func ReadFromStart() FileConfig {
	return func(t *File) error {
		_, err := t.file.Seek(0, os.SEEK_SET)
		return err
	}
}