<commit_before>package stns

import (
	"errors"
	"fmt"
	"io/ioutil"
	"path/filepath"
	"strings"

	"github.com/BurntSushi/toml"
	"github.com/STNS/STNS/model"
	"github.com/go-yaml/yaml"
)

type decoder interface {
	decode(string, *Config) error
}

type tomlDecoder struct{}
type yamlDecoder struct{}

func (t *tomlDecoder) decode(path string, conf *Config) error {
	_, err := toml.DecodeFile(path, conf)
	return err
}

func (y *yamlDecoder) decode(path string, conf *Config) error {
	_, err := toml.DecodeFile(path, conf)

	buf, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(buf, conf)
	return err
}

func decode(path string, conf *Config) error {
	var d decoder
	d = new(tomlDecoder)
	if strings.HasSuffix(path, "yaml") || strings.HasSuffix(path, "yml") {
		d = new(yamlDecoder)
	}
	return d.decode(path, conf)
}

func NewConfig(confPath string) (Config, error) {
	var conf Config
	conf.dir = filepath.Dir(confPath)

	defaultConfig(&conf)

	if err := decode(confPath, &conf); err != nil {
		return conf, err
	}

	if conf.Include != "" {
		if err := includeConfigFile(&conf, conf.Include); err != nil {
			return Config{}, err
		}
	}

	if err := conf.validate(); err != nil {
		return Config{}, err
	}
	return conf, nil
}

type Config struct {
	dir       string
	Port      int        `toml:"port"`
	BasicAuth *BasicAuth `toml:"basic_auth" yaml:"basic_auth"`
	TokenAuth *TokenAuth `toml:"token_auth" yaml:"token_auth"`

	UseServerStarter bool
	Users            *model.Users
	Groups           *model.Groups
	Include          string   `toml:"include"`
	ModulePath       string   `toml: "module_path" yaml: "module_path"`
	ResolveModules   []string `toml: "resolve_modules" yaml: "resolve_modules"`
	Etcd             *Etcd
}

type BasicAuth struct {
	User     string
	Password string
}
type TokenAuth struct {
	Tokens []string
}

type Etcd struct {
	Endpoints []string
	User      string
	Password  string
	SecretKey []byte `toml: "secret_key" yaml: "secret_key"`
}

func defaultConfig(c *Config) {
	c.Port = 1104
	c.ModulePath = "./modules.d"
}

func includeConfigFile(config *Config, include string) error {
	if !strings.HasPrefix(include, "/") {
		include = filepath.Join(config.dir, include)
	}

	files, err := filepath.Glob(include)
	if err != nil {
		return err
	}

	for _, file := range files {
		if err := decode(file, config); err != nil {
			return fmt.Errorf("while loading included config file %s: %s", file, err)
		}
	}
	return nil
}

func (c *Config) validate() error {
	if c.Etcd != nil && len(c.Etcd.SecretKey) == 0 {
		return errors.New("Please specify secret_key when using etcd")
	}
	return nil
}<commit_msg>aesのキーの長さは制限がある<commit_after>package stns

import (
	"errors"
	"fmt"
	"io/ioutil"
	"path/filepath"
	"strings"

	"github.com/BurntSushi/toml"
	"github.com/STNS/STNS/model"
	"github.com/go-yaml/yaml"
)

type decoder interface {
	decode(string, *Config) error
}

type tomlDecoder struct{}
type yamlDecoder struct{}

func (t *tomlDecoder) decode(path string, conf *Config) error {
	_, err := toml.DecodeFile(path, conf)
	return err
}

func (y *yamlDecoder) decode(path string, conf *Config) error {
	_, err := toml.DecodeFile(path, conf)

	buf, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(buf, conf)
	return err
}

func decode(path string, conf *Config) error {
	var d decoder
	d = new(tomlDecoder)
	if strings.HasSuffix(path, "yaml") || strings.HasSuffix(path, "yml") {
		d = new(yamlDecoder)
	}
	return d.decode(path, conf)
}

func NewConfig(confPath string) (Config, error) {
	var conf Config
	conf.dir = filepath.Dir(confPath)

	defaultConfig(&conf)

	if err := decode(confPath, &conf); err != nil {
		return conf, err
	}

	if conf.Include != "" {
		if err := includeConfigFile(&conf, conf.Include); err != nil {
			return Config{}, err
		}
	}

	if err := conf.validate(); err != nil {
		return Config{}, err
	}
	return conf, nil
}

type Config struct {
	dir       string
	Port      int        `toml:"port"`
	BasicAuth *BasicAuth `toml:"basic_auth" yaml:"basic_auth"`
	TokenAuth *TokenAuth `toml:"token_auth" yaml:"token_auth"`

	UseServerStarter bool
	Users            *model.Users
	Groups           *model.Groups
	Include          string   `toml:"include"`
	ModulePath       string   `toml: "module_path" yaml: "module_path"`
	ResolveModules   []string `toml: "resolve_modules" yaml: "resolve_modules"`
	Etcd             *Etcd
}

type BasicAuth struct {
	User     string
	Password string
}
type TokenAuth struct {
	Tokens []string
}

type Etcd struct {
	Endpoints []string
	User      string
	Password  string
	SecretKey []byte `toml: "secret_key" yaml: "secret_key"`
}

func defaultConfig(c *Config) {
	c.Port = 1104
	c.ModulePath = "./modules.d"
}

func includeConfigFile(config *Config, include string) error {
	if !strings.HasPrefix(include, "/") {
		include = filepath.Join(config.dir, include)
	}

	files, err := filepath.Glob(include)
	if err != nil {
		return err
	}

	for _, file := range files {
		if err := decode(file, config); err != nil {
			return fmt.Errorf("while loading included config file %s: %s", file, err)
		}
	}
	return nil
}

func (c *Config) validate() error {
	if c.Etcd != nil {
		if len(c.Etcd.SecretKey) == 0 {
			return errors.New("Please specify secret_key when using etcd")
		}
		k := len(c.Etcd.SecretKey)
		switch k {
		default:
			return errors.New("Please input secret_key length is (16,24,32)")
		case 16, 24, 32:
			break
		}
	}
	return nil
}
{'fraction_non_alphanumeric': 0.13336982302499545, 'fraction_numerical': 0.0040138660828316, 'mean_word_length': 3.6028547439126783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3253645', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1940, 'n_words': 530}