Example #1
0
func TestConfig(t *testing.T) {
	var configData = make(map[string]interface{})
	err := toml.Unmarshal([]byte(testTxt), &configData)
	if err != nil {
		t.Fatal(err)
	}

	cfg := NewMapConfig(configData)

	for k, v := range stringTable {
		if x := cfg.GetString(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

	for k, v := range intTable {
		if x := cfg.GetInt(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

	for k, v := range boolTable {
		if x := cfg.GetBool(k); x != v {
			t.Fatalf("Got %v. Expected %v", x, v)
		}
	}

}
Example #2
0
// UnmarshalData unmarshal YAML/JSON/TOML serialized data.
func UnmarshalData(cont []byte, f DataFmt) (map[string]interface{}, error) {
	v := make(map[string]interface{})

	switch f {
	case YAML:
		log.Info("Unmarshaling YAML data")
		err := yaml.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	case TOML:
		log.Info("Unmarshaling TOML data")
		err := toml.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	case JSON:
		log.Info("Unmarshaling JSON data")
		err := json.Unmarshal(cont, &v)
		if err != nil {
			return nil, err
		}
	default:
		log.Error("Unsupported data format")
		return nil, errors.New("Unsupported data format")
	}

	return v, nil
}
Example #3
0
func MustParseConfig(configFile string) {
	if fileInfo, err := os.Stat(configFile); err != nil {
		if os.IsNotExist(err) {
			log.Panicf("configuration file %v does not exist.", configFile)
		} else {
			log.Panicf("configuration file %v can not be stated. %v", err)
		}
	} else {
		if fileInfo.IsDir() {
			log.Panicf("%v is a directory name", configFile)
		}
	}

	content, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Panicf("read configuration file error. %v", err)
	}
	content = bytes.TrimSpace(content)

	err = toml.Unmarshal(content, &Conf)
	if err != nil {
		log.Panicf("unmarshal toml object error. %v", err)
	}

	// short url black list
	Conf.Common.BlackShortURLsMap = make(map[string]bool)
	for _, blackShortURL := range Conf.Common.BlackShortURLs {
		Conf.Common.BlackShortURLsMap[blackShortURL] = true
	}

	// base string
	Conf.Common.BaseStringLength = uint64(len(Conf.Common.BaseString))
}
Example #4
0
// NewPostsFrontMatter parse post meta file to create post data
func NewPostsFrontMatter(file string, t FormatType) (map[string]*Post, error) {
	metas := make(map[string]*Post)

	if t == FormatTOML {
		data, err := ioutil.ReadFile(file)
		if err != nil {
			return nil, err
		}
		if err = toml.Unmarshal(data, &metas); err != nil {
			return nil, err
		}
	}

	if t == FormatINI {
		iniObj, err := ini.Load(file)
		if err != nil {
			return nil, err
		}
		for _, s := range iniObj.SectionStrings() {
			s2 := strings.Trim(s, `"`)
			if s2 == "DEFAULT" {
				continue
			}
			post := new(Post)
			if err = newPostFromIniSection(iniObj.Section(filepath.ToSlash(s)), post); err != nil {
				return nil, err
			}
			metas[s2] = post
		}
	}
	return metas, nil
}
Example #5
0
func LoadCfg() (*Cfg, error) {
	flag.Parse()

	bytes, err := ioutil.ReadFile(*configPath)
	if err != nil {
		return nil, err
	}

	conf := Cfg{}
	if err := toml.Unmarshal(bytes, &conf); err != nil {
		return nil, err
	}

	if *staticsPath != "" {
		conf.StaticsPath = *staticsPath
	}

	if *host != "" {
		conf.Host = *host
	}

	if *httpDrainInterval != "" {
		conf.HttpDrainInterval = *httpDrainInterval
	}

	if *boltDDPath != "" {
		conf.BoltDDPath = *boltDDPath
	}

	conf.BatchSize = *batchSize

	return &conf, nil
}
Example #6
0
// NewPageOfMarkdown create new page from markdown file
func NewPageOfMarkdown(file, slug string, page *Page) (*Page, error) {
	// page-node need not read file
	if page != nil && page.Node == true {
		return page, nil
	}
	fileBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	if len(fileBytes) < 3 {
		return nil, fmt.Errorf("page content is too less")
	}
	if page == nil {
		dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
		if len(dataSlice) != 3 {
			return nil, fmt.Errorf("page need front-matter block and markdown block")
		}

		idx := getFirstBreakByte(dataSlice[1])
		if idx == 0 {
			return nil, fmt.Errorf("page need front-matter block and markdown block")
		}

		formatType := detectFormat(string(dataSlice[1][:idx]))
		if formatType == 0 {
			return nil, fmt.Errorf("page front-matter block is unrecognized")
		}

		page = new(Page)
		if formatType == FormatTOML {
			if err = toml.Unmarshal(dataSlice[1][idx:], page); err != nil {
				return nil, err
			}
		}
		if formatType == FormatINI {
			iniObj, err := ini.Load(dataSlice[1][idx:])
			if err != nil {
				return nil, err
			}
			if err = newPageFromIniObject(iniObj, page, "DEFAULT", "meta"); err != nil {
				return nil, err
			}
		}
		if page.Node == false {
			page.Bytes = bytes.Trim(dataSlice[2], "\n")
		}
	} else {
		page.Bytes = bytes.Trim(fileBytes, "\n")
	}
	page.fileURL = file
	if page.Slug == "" {
		page.Slug = slug
	}
	if page.Date == "" && page.Node == false { // page-node need not time
		t, _ := com.FileMTime(file)
		page.dateTime = time.Unix(t, 0)
	}
	return page, page.normalize()
}
Example #7
0
File: main.go Project: rdterner/tp
func loadConfig() (hasFile bool, err error) {
	configBytes, err := ioutil.ReadFile(configFileName)
	if err != nil {
		return false, nil
	}
	err = toml.Unmarshal(configBytes, &config)
	return true, err
}
Example #8
0
func ReadMapConfigFromFile(filePath string) (*MapConfig, error) {
	var configData = make(map[string]interface{})
	fileBytes := MustReadFile(filePath)
	err := toml.Unmarshal(fileBytes, &configData)
	if err != nil {
		return nil, err
	}
	return NewMapConfig(configData), nil
}
Example #9
0
func (tl tomlLoader) Convert(r io.Reader) (map[string]interface{}, error) {
	data, _ := ioutil.ReadAll(r)
	ret := make(map[string]interface{})
	err := toml.Unmarshal(data, &ret)
	if err != nil {
		return nil, err
	}

	return ret, nil
}
Example #10
0
// parseToml performs the real JSON parsing.
func parseToml(cfg []byte) (*Config, error) {
	var out interface{}
	var err error
	if err = toml.Unmarshal(cfg, &out); err != nil {
		return nil, err
	}
	if out, err = normalizeValue(out); err != nil {
		return nil, err
	}
	return &Config{Root: out}, nil
}
Example #11
0
func InitConfig(path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	data, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}
	return toml.Unmarshal(data, &Config)
}
Example #12
0
// I18nDataFromTOML parse toml data to map
func I18nDataFromTOML(data []byte) (map[string]map[string]string, error) {
	maps := make(map[string]map[string]string)
	if err := toml.Unmarshal(data, &maps); err != nil {
		return nil, err
	}
	for k, v := range maps {
		if len(v) == 0 {
			return nil, fmt.Errorf("i18n section '%s' is empty", k)
		}
	}
	return maps, nil
}
Example #13
0
// Parse the metadata
func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
	b, markdown, err := extractMetadata(t, b)
	if err != nil {
		return markdown, err
	}
	m := make(map[string]interface{})
	if err := toml.Unmarshal(b, &m); err != nil {
		return markdown, err
	}
	t.metadata.load(m)
	return markdown, nil
}
Example #14
0
// NewPostOfMarkdown create new post from markdown file
func NewPostOfMarkdown(file string, post *Post) (*Post, error) {
	fileBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	if len(fileBytes) < 3 {
		return nil, fmt.Errorf("post content is too less")
	}

	if post == nil {
		dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
		if len(dataSlice) != 3 {
			return nil, fmt.Errorf("post need front-matter block and markdown block")
		}

		idx := getFirstBreakByte(dataSlice[1])
		if idx == 0 {
			return nil, fmt.Errorf("post need front-matter block and markdown block")
		}

		formatType := detectFormat(string(dataSlice[1][:idx]))
		if formatType == 0 {
			return nil, fmt.Errorf("post front-matter block is unrecognized")
		}

		post = new(Post)
		if formatType == FormatTOML {
			if err = toml.Unmarshal(dataSlice[1][idx:], post); err != nil {
				return nil, err
			}
		}
		if formatType == FormatINI {
			iniObj, err := ini.Load(dataSlice[1][idx:])
			if err != nil {
				return nil, err
			}
			section := iniObj.Section("DEFAULT")
			if err = newPostFromIniSection(section, post); err != nil {
				return nil, err
			}
		}
		post.Bytes = bytes.Trim(dataSlice[2], "\n")
	} else {
		post.Bytes = bytes.Trim(fileBytes, "\n")
	}
	post.fileURL = file
	if post.Date == "" {
		t, _ := com.FileMTime(file)
		post.dateTime = time.Unix(t, 0)
	}
	return post, post.normalize()
}
Example #15
0
func parseSuites(suites []string) (map[string]*configurationSuite, error) {
	configs := map[string]*configurationSuite{}
	for _, suite := range suites {
		logrus.Debugf("Handling suite %s", suite)
		absPath, err := filepath.Abs(suite)
		if err != nil {
			return nil, fmt.Errorf("could not resolve %s: %s", suite, err)
		}

		info, err := os.Stat(absPath)
		if err != nil {
			return nil, fmt.Errorf("error statting %s: %s", suite, err)
		}
		if info.IsDir() {
			absPath = filepath.Join(absPath, "golem.conf")
			if _, err := os.Stat(absPath); err != nil {
				return nil, fmt.Errorf("error statting %s: %s", filepath.Join(suite, "golem.conf"), err)
			}
		}

		confBytes, err := ioutil.ReadFile(absPath)
		if err != nil {
			return nil, fmt.Errorf("unable to open configuration file %s: %s", absPath, err)
		}

		// Load
		var conf suitesConfiguration
		if err := toml.Unmarshal(confBytes, &conf); err != nil {
			return nil, fmt.Errorf("error unmarshalling %s: %s", absPath, err)
		}

		logrus.Debugf("Found %d test suites in %s", len(conf.Suites), suite)
		for _, sc := range conf.Suites {
			p := filepath.Dir(absPath)
			suiteConfig, err := newSuiteConfiguration(p, sc)
			if err != nil {
				return nil, err
			}

			name := suiteConfig.Name()
			_, ok := configs[name]
			for i := 1; ok; i++ {
				name = fmt.Sprintf("%s-%d", suiteConfig.Name(), i)
				_, ok = configs[name]
			}
			suiteConfig.SetName(name)
			configs[name] = suiteConfig
		}
	}

	return configs, nil
}
Example #16
0
func NewMapFromFile(configPath string) (DMap, error) {
	bytes, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, fmt.Errorf("Can't open config file! Err: %v", err)
	}

	var conf DMapConfig
	if err := toml.Unmarshal(bytes, &conf); err != nil {
		return nil, fmt.Errorf("Can't unmarshal config file! Err: %v", err)
	}

	return NewMap(conf)
}
Example #17
0
func main() {
	configtoml := flag.String("f", "moxy.toml", "Path to config. (default moxy.toml)")
	flag.Parse()
	file, err := ioutil.ReadFile(*configtoml)
	if err != nil {
		log.Fatal(err)
	}
	err = toml.Unmarshal(file, &config)
	if err != nil {
		log.Fatal("Problem parsing config: ", err)
	}
	if config.Statsd != "" {
		statsd, _ = g2s.Dial("udp", config.Statsd)
	}
	moxystats := stats.New()
	mux := http.NewServeMux()
	mux.HandleFunc("/moxy_callback", moxy_callback)
	mux.HandleFunc("/moxy_apps", moxy_apps)
	mux.HandleFunc("/moxy_stats", func(w http.ResponseWriter, req *http.Request) {
		if config.Xproxy != "" {
			w.Header().Add("X-Proxy", config.Xproxy)
		}
		stats := moxystats.Data()
		b, _ := json.MarshalIndent(stats, "", "  ")
		w.Write(b)
		return
	})
	mux.HandleFunc("/", moxy_proxy)
	// In case we want to log req/resp.
	//trace, _ := trace.New(redirect, os.Stdout)
	handler := moxystats.Handler(mux)
	s := &http.Server{
		Addr:    ":" + config.Port,
		Handler: handler,
	}
	callbackworker()
	callbackqueue <- true
	if config.TLS {
		log.Println("Starting moxy tls on :" + config.Port)
		err := s.ListenAndServeTLS(config.Cert, config.Key)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		log.Println("Starting moxy on :" + config.Port)
		err := s.ListenAndServe()
		if err != nil {
			log.Fatal(err)
		}
	}
}
Example #18
0
// Load reads a vendor manifest file and returns a Manifest object.
// It also takes an optional format to default the manifest to when written.
func Load(format string) (*Manifest, error) {

	// if the format is not specified default to YAML
	if format == "" {
		format = "yml"
	}

	// create a new manifest and set the format
	m := &Manifest{}
	if err := m.SetFormat(format); err != nil {
		return nil, err
	}

	// check if a yml, json, or toml manifest file exists on disk
	var f string
	for _, ext := range extensions {
		if _, err := os.Stat(file + ext); err == nil {
			f = string([]rune(ext)[1:])
		}
	}

	// if no format has been specified, no valid manifest file is present
	if f == "" {
		return m, nil
	}

	// read the manifest file on disk
	bytes, err := ioutil.ReadFile(file + "." + m.format)
	if err != nil {
		return m, err
	}

	switch m.format {
	case "json":
		if err := json.Unmarshal(bytes, m); err != nil {
			return nil, err
		}
	case "yml":
		if err := yaml.Unmarshal(bytes, m); err != nil {
			return nil, err
		}
	case "toml":
		if err := toml.Unmarshal(bytes, m); err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("file type %s not supported", format)
	}

	return m, nil
}
Example #19
0
func readCommandToml(filePath string, container interface{}) (err error) {
	b, err := loadDataFrom(filePath)
	if err != nil {
		return err
	}

	err = toml.Unmarshal(b, container)
	if err != nil {
		return err
	}

	err = nil
	return
}
Example #20
0
func parseConifg(filename string) (*Config, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}
	var config Config
	err = toml.Unmarshal(buf, &config)
	return &config, err
}
Example #21
0
func load(config interface{}, file string) error {
	data, err := ioutil.ReadFile(file)
	if err != nil {
		return err
	}

	switch {
	case strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml"):
		return yaml.Unmarshal(data, config)
	case strings.HasSuffix(file, ".toml"):
		return toml.Unmarshal(data, config)
	case strings.HasSuffix(file, ".json"):
		return json.Unmarshal(data, config)
	default:
		if toml.Unmarshal(data, config) != nil {
			if json.Unmarshal(data, config) != nil {
				if yaml.Unmarshal(data, config) != nil {
					return errors.New("failed to decode config")
				}
			}
		}
		return nil
	}
}
Example #22
0
// NewConfig new a config.
func NewConfig(conf string) (c *Config, err error) {
	var (
		file *os.File
		blob []byte
	)
	c = new(Config)
	if file, err = os.Open(conf); err != nil {
		return
	}
	if blob, err = ioutil.ReadAll(file); err != nil {
		return
	}
	err = toml.Unmarshal(blob, c)
	return
}
Example #23
0
// Parse metadata/markdown file
func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool {
	meta, data := splitBuffer(b, "+++")
	if meta == nil || data == nil {
		return false
	}
	t.markdown = data

	m := make(map[string]interface{})
	if err := toml.Unmarshal(meta.Bytes(), &m); err != nil {
		return false
	}
	t.metadata = NewMetadata(m)

	return true
}
Example #24
0
func Init() {
	cfgFile := getAbs("./settings/settings.toml") // "D:\\Dev\\gopath\\src\\github.com\\Felamande\\filesync.v2\\settings\\settings.toml"
	b, err := ioutil.ReadFile(cfgFile)
	if err != nil {
		panic(err)
	}
	toml.Unmarshal(b, settingStruct)

	Static = settingStruct.Static
	Server = settingStruct.Server
	Filesync = settingStruct.Filesync
	Template = settingStruct.Template
	DefaultVars = settingStruct.DefaultVars
	Admin = settingStruct.Admin
	Log = settingStruct.Log
}
Example #25
0
func TestConfigLoad(t *testing.T) {
	assert := assert.New(t)

	config := DefaultConfig()
	if err := toml.Unmarshal([]byte(TestConfig), config); assert.NoError(err) {
		assert.True(config.SSL.Enabled)
		assert.Equal("/etc/ansible/server.cert", config.SSL.Certificate)
		assert.Equal("/etc/ansible/server.key", config.SSL.PrivateKey)

		assert.True(config.Ldap.Enabled)
		assert.Equal("ldaps://example.com", config.Ldap.Host)
		assert.Equal(uint16(636), config.Ldap.Port)
		assert.Equal("dc=example,dc=com", config.Ldap.BaseDN)
		assert.Equal("(uid=%s)", config.Ldap.UserFilter)
	}
}
Example #26
0
func main() {
	// load config
	flag.Parse()

	bytes, err := ioutil.ReadFile(configPath)
	if err != nil {
		log.Fatalf("Can't open config file!")
	}

	var conf Config
	if err := toml.Unmarshal(bytes, &conf); err != nil {
		log.Fatalf("Can't decode config file!")
	}

	indexUsers(&conf, streamUsers(&conf))
}
Example #27
0
// NewMeta parse bytes to theme meta
func NewMeta(data []byte, t model.FormatType) (*Meta, error) {
	if t == model.FormatTOML {
		meta := new(Meta)
		if err := toml.Unmarshal(data, meta); err != nil {
			return nil, err
		}
		return meta, nil
	}
	if t == model.FormatINI {
		meta := new(Meta)
		iniObj, err := ini.Load(data)
		if err != nil {
			return nil, err
		}
		if err = iniObj.Section("DEFAULT").MapTo(meta); err != nil {
			return nil, err
		}
		for _, authorKey := range iniObj.Section("author").KeysHash() {
			s := iniObj.Section("author." + authorKey)
			if len(s.Keys()) == 0 {
				continue
			}
			author := new(model.Author)
			if err = s.MapTo(author); err != nil {
				return nil, err
			}
			if author.Name != "" {
				meta.Authors = append(meta.Authors, author)
			}
		}
		for _, refKey := range iniObj.Section("ref").KeysHash() {
			s := iniObj.Section("ref." + refKey)
			if len(s.Keys()) == 0 {
				continue
			}
			ref := new(metaRef)
			if err = s.MapTo(ref); err != nil {
				return nil, err
			}
			if ref.Name != "" {
				meta.Refs = append(meta.Refs, ref)
			}
		}
		return meta, nil
	}
	return nil, nil
}
Example #28
0
// NewMetaAll parse bytes with correct FormatType
func NewMetaAll(data []byte, format FormatType) (*MetaAll, error) {
	var err error
	switch format {
	case FormatTOML:
		meta := &MetaAll{}
		if err = toml.Unmarshal(data, meta); err != nil {
			return nil, err
		}
		if err = meta.Normalize(); err != nil {
			return nil, err
		}
		return meta, nil
	case FormatINI:
		return newMetaAllFromINI(data)
	}
	return nil, errMetaUnsupport
}
Example #29
0
// Function loadRequests loads the requests data structures
func loadRequests(inFName string) (retErr error) {
	var fbytes []byte

	// Read the YAML file data into a byte array
	fbytes, retErr = ioutil.ReadFile(inFName)
	if retErr != nil {
		log.Fatalln("Fn loadRequests: Error reading data to initialize Customers see:", retErr)
	}

	// Unmarshall the YAML data into the data structure
	retErr = toml.Unmarshal(fbytes, &cData)
	if retErr != nil {
		log.Fatalln("Fn loadCustomers: Error parsing the Customer TOML data see:", retErr)
	}

	return
}
Example #30
0
func main() {
	// load config
	flag.Parse()

	bytes, err := ioutil.ReadFile(configPath)
	if err != nil {
		log.Fatalf("Can't open config file!")
	}

	if err := toml.Unmarshal(bytes, &conf); err != nil {
		log.Fatalf("Can't decode config file!")
	}

	log.Printf("Config: %+v", conf)

	launchServer(&conf)
}