Esempio n. 1
0
func GetConfig(configFilePath string) (config *Config, err error) {
	var (
		file *os.File
		buf  []byte
	)
	file, err = os.Open(configFilePath)
	if err != nil {
		return
	}
	defer file.Close()
	buf, err = ioutil.ReadAll(file)
	if err != nil {
		return
	}
	config = new(Config)
	if err = toml.Unmarshal(buf, config); err != nil {
		return
	}
	config.Logger.LogFile, err = reopen.NewFileWriter(config.Logger.LogFilePath)

	if config.DatabaseConnectOpts.Log {
		config.DatabaseConnectOpts.logFile = config.Logger.LogFile
	} else {
		config.DatabaseConnectOpts.logFile = ioutil.Discard
	}
	return
}
Esempio n. 2
0
// LoadConfiguration from given file
func LoadConfiguration(filename string) (conf *AppConfiguration, err error) {
	log.Info("config.LoadConfiguration", "filename", filename)
	var content []byte
	conf = &AppConfiguration{}
	conf.loadDefaults()
	content, err = ioutil.ReadFile(filename)
	if err == nil {
		if err = toml.Unmarshal(content, conf); err != nil {
			panic(err)
		}
	} else {
		log.Error("config.LoadConfiguration", "filename", filename, "err", err)
	}
	conf.validate()

	if !common.DirExists(conf.CertsDir) {
		log.Info("config.LoadConfiguration dir for certs not exists - creating", "path", conf.CertsDir)
		if err := os.MkdirAll(conf.CertsDir, 600); err != nil {
			log.Crit("config.LoadConfiguration creating dir for certs failed",
				"path", conf.CertsDir, "err", err)
		}
	}

	return
}
Esempio n. 3
0
// Creates a new instance of the tomlConfig
// struct and reads the file accordingly, checkins
// its a local a file or a remote file.
func NewTomlTemplate(fileLocation string) (*tomlConfig, error) {

	var buf []byte
	var err error

	reg := regexp.MustCompile("^(http|https)://")
	if reg.MatchString(fileLocation) {
		fmt.Fprintf(os.Stdout, "===> Fetiching url: %s\n", fileLocation)
		buf, err = getRemoteFile(fileLocation)
		if err != nil {
			return nil, err
		}
	} else {
		buf, err = getLocalFile(fileLocation)
		if err != nil {
			return nil, err
		}
	}

	var config tomlConfig
	if err = toml.Unmarshal(buf, &config); err != nil {
		return nil, err
	}

	return &config, nil
}
Esempio n. 4
0
func readConfig(filename string) Config {
	f, err := os.Open(filename)

	if err != nil {
		log.Fatalln(err)
	}

	defer f.Close()

	buf, err := ioutil.ReadAll(f)

	if err != nil {
		log.Fatalln(err)
	}

	var config Config

	if err := toml.Unmarshal(buf, &config); err != nil {
		log.Fatalln(err)
	}

	if err != nil {
		log.Fatalln(err)
	}

	return config
}
Esempio n. 5
0
func GetConfig(rootDir string) cfg.Config {
	rootDir = getTMRoot(rootDir)
	initTMRoot(rootDir)

	var mapConfig = cfg.MapConfig(make(map[string]interface{}))
	configFilePath := path.Join(rootDir, "config.toml")
	configFileBytes := MustReadFile(configFilePath)
	err := toml.Unmarshal(configFileBytes, mapConfig)
	if err != nil {
		Exit(Fmt("Could not read config: %v", err))
	}

	// Set defaults or panic
	if !mapConfig.IsSet("network") {
		Exit("Must set 'network'")
	}
	if mapConfig.IsSet("version") {
		Exit("Cannot set 'version' via config.toml")
	}
	// mapConfig.SetDefault("network", "tendermint_testnet0")
	mapConfig.SetDefault("version", "0.2.1")
	mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
	mapConfig.SetDefault("moniker", "anonymous")
	mapConfig.SetDefault("node_laddr", "0.0.0.0:46656")
	// mapConfig.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
	mapConfig.SetDefault("fast_sync", true)
	mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
	mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
	mapConfig.SetDefault("db_backend", "leveldb")
	mapConfig.SetDefault("db_dir", rootDir+"/data")
	mapConfig.SetDefault("log_level", "info")
	mapConfig.SetDefault("rpc_laddr", "0.0.0.0:46657")
	return mapConfig
}
Esempio n. 6
0
func (s *spyrun) loadToml(tomlpath string) error { // {{{
	var err error

	if _, err = os.Stat(tomlpath); err != nil {
		fmt.Fprintf(os.Stderr, "%s is not found !", tomlpath)
		os.Exit(1)
	}

	f, err := os.Open(tomlpath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open %s", tomlpath)
		os.Exit(1)
	}
	defer f.Close()

	buf, err := ioutil.ReadAll(f)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load %s", tomlpath)
		os.Exit(1)
	}

	err = toml.Unmarshal(buf, &s.conf)

	return err
} // }}}
Esempio n. 7
0
func loadConfig(path string) {
	// default values
	config.Postgres.UpdatesChannelName = "task"
	config.Postgres.TableName = "tasks"

	f, err := os.Open(path)
	if err != nil {
		log.Fatalln("Cannot open configuration file:", err)
	}
	defer f.Close()

	buf, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalln("Cannot read configuration file:", err)
	}

	if err := toml.Unmarshal(buf, &config); err != nil {
		log.Fatalln("Cannot decode configuration file:", err)
	}

	quotedTableName = quoteIdentifier(config.Postgres.TableName)

	var q bytes.Buffer
	q.WriteString(`UPDATE ` + quotedTableName + ` a SET begin_time=now(), status='running' FROM (SELECT task_id FROM ` + quotedTableName + ` WHERE status = 'queued' OR (status = 'running' AND begin_time <= now() - interval '10 seconds' - (CASE worker`)
	for name, worker := range config.Workers {
		q.WriteString(" WHEN ")
		q.WriteString(fetchQueryArgs.Append(name))
		q.WriteString(" THEN ")
		q.WriteString(strconv.Itoa(worker.TimeoutSecs))
	}
	q.WriteString(` ELSE NULL END * interval '1 second')) ORDER BY insert_time LIMIT 1) b WHERE a.task_id = b.task_id RETURNING a.task_id,a.worker,a.parameters`)

	fetchQuery = q.String()
}
Esempio n. 8
0
// Load a file as a YAML document and return the structure
func LoadConfig(file string) (*Config, error) {
	var sFile string

	// Check for tag
	if !strings.HasSuffix(file, ".toml") {
		// file must be a tag so add a "."
		sFile = filepath.Join(os.Getenv("HOME"),
			fmt.Sprintf(".%s", file),
			"config.toml")
	} else {
		sFile = file
	}

	c := new(Config)
	buf, err := ioutil.ReadFile(sFile)
	if err != nil {
		return c, errors.New(fmt.Sprintf("Can not read %s file.", sFile))
	}

	err = toml.Unmarshal(buf, &c)
	if err != nil {
		return c, errors.New(fmt.Sprintf("Can not parse %s: %v", sFile, err))
	}

	return c, err
}
Esempio n. 9
0
//Parse the Assemblyfile from the raw bytes
func Parse(rawConfig []byte) (Config, error) {
	var config Config
	if err := toml.Unmarshal(rawConfig, &config); err != nil {
		return Config{}, err
	}
	return config, nil
}
Esempio n. 10
0
func newConfig(filename string) (*config, error) {
	bytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	conf := &config{handled: make(map[string]bool)}
	switch path.Ext(filename) {
	case ".json":
		if err := json.Unmarshal(bytes, &conf); err != nil {
			return nil, err
		}
	case ".toml", ".tml":
		if err := toml.Unmarshal(bytes, &conf); err != nil {
			return nil, err
		}
	case ".yaml", ".yml":
		if err := yaml.Unmarshal(bytes, &conf); err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("unsupported configuration file format")
	}

	return conf, nil
}
Esempio n. 11
0
func Start(file string) (syslog.Writer, domains.Config) {

	golog, err := syslog.New(syslog.LOG_ERR, "golog")

	defer golog.Close()
	if err != nil {
		log.Fatal("error writing syslog!!")

	}

	f, err := os.Open(file)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}
	//    var config tomlConfig
	if err := toml.Unmarshal(buf, &config); err != nil {
		panic(err)
	}

	return *golog, config

}
Esempio n. 12
0
// ReadInventory will read an inventory file and return the found items.
func ReadInventory(file string, bec BackendConfig) (*Inventory, error) {
	f, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	conf, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}
	drops := Droplets{}
	err = toml.Unmarshal(conf, &drops)
	if err != nil {
		return nil, err
	}

	inv := &Inventory{
		bec:      bec,
		backends: make([]Backend, 0, len(drops.Droplets)),
	}

	for _, v := range drops.Droplets {
		inv.backends = append(inv.backends, NewDropletBackend(v, bec))
	}

	return inv, nil
}
Esempio n. 13
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 MapConfig{}, err
	}
	return NewMapConfig(configData), nil
}
Esempio n. 14
0
func init() {
	b, err := ioutil.ReadFile(os.ExpandEnv("$GOPATH/src/github.com/janvogt/go-vereinsflieger/config.toml"))
	if err != nil {
		panic(err)
	}
	err = toml.Unmarshal(b, &DefaultConfig)
	if err != nil {
		panic(err)
	}
}
Esempio n. 15
0
func LoadConfig(file string) (cfg Config, err error) {
	f, err := os.Open(file)
	if err != nil {
		return cfg, err
	}
	defer f.Close()

	buf, err := ioutil.ReadAll(f)
	if err != nil {
		return cfg, err
	}

	err = toml.Unmarshal(buf, &cfg)

	if cfg.DefaultInterval == 0 {
		cfg.DefaultInterval = 60
	}

	if cfg.HostName == "" {
		fqdn, err := os.Hostname()
		if err != nil {
			return cfg, err
		}

		cfg.HostName = fqdn
	}

	if len(cfg.GlobalTags) == 0 {
		cfg.GlobalTags = []string{"nagios"}
	}

	for k := range cfg.Checks {
		chk := cfg.Checks[k]

		if chk.Command == "" {
			return cfg, errors.New("Missing required command argument on " + k + " check.")
		}

		if chk.Host == "" {
			chk.Host = cfg.HostName
		}
		if chk.Interval == 0 {
			chk.Interval = cfg.DefaultInterval
		}
		if cfg.Checks[k].TTL == 0 {
			chk.TTL = cfg.Checks[k].Interval
		}

		cfg.Checks[k] = chk
	}

	return cfg, err
}
Esempio n. 16
0
//Read reads a spanner config from a reader, and returns the Config object
//returns an error if the config could not be read, or if it is invalid
func Read(reader io.Reader) (Config, error) {
	buf, err := ioutil.ReadAll(reader)
	if err != nil {
		return Config{}, err
	}

	var config Config
	if err := toml.Unmarshal(buf, &config); err != nil {
		return Config{}, err
	}
	return config, nil
}
Esempio n. 17
0
func testUnmarshal(t *testing.T, testcases []testcase) {
	for _, v := range testcases {
		var actual error = toml.Unmarshal([]byte(v.data), v.actual)
		var expect error = v.err
		if !reflect.DeepEqual(actual, expect) {
			t.Errorf(`toml.Unmarshal([]byte(%#v), %#v) => %#v; want %#v`, v.data, nil, actual, expect)
		}
		if !reflect.DeepEqual(v.actual, v.expect) {
			t.Errorf(`toml.Unmarshal([]byte(%#v), v); v => %#v; want %#v`, v.data, v.actual, v.expect)
		}
	}
}
Esempio n. 18
0
func (c *StateFile) loadState() *State {
	buf, err := ioutil.ReadFile(c.FilePath)

	c.State = new(State)
	if err != nil && !os.IsNotExist(err) {
		panic(err)
	}

	if err := toml.Unmarshal(buf, c.State); err != nil {
		fmt.Println("Problem with reading configuration file")
	}

	return c.State
}
Esempio n. 19
0
func setConfig() error {
	f, err := Asset("config/application.toml")
	if err != nil {
		return err
	}

	if err := toml.Unmarshal(f, config); err != nil {
		return err
	}

	oauthConf.ClientID = config.ClientID
	oauthConf.ClientSecret = config.ClientSecret
	return nil
}
//LoadSettingsFromTomlFile does exactly what the name says, it loads a toml in a Settings struct
func LoadSettingsFromTomlFile(filename string) (settings Settings, err error) {
	f, err := os.Open(filename)
	if err != nil {
		return
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		return
	}
	err = toml.Unmarshal(buf, &settings)
	return

}
Esempio n. 21
0
// Init prepares and parses the metadata and markdown file itself
func (t *TOMLParser) 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
}
Esempio n. 22
0
// ServerFromConf generates a server instance with the settings
// specified in the specified config file. All other command line
// arguments are igored in this operation mode
func ServerFromConf(cfg string) *gouncer.Server {
	conf, err := ioutil.ReadFile(cfg)

	if err != nil {
		log.Fatalln("Error reading config", err.Error())
	}

	var gcfg gouncer.Config

	if err := toml.Unmarshal(conf, &gcfg); err != nil {
		log.Fatalln("Error parsing config", err.Error())
	}

	return gouncer.NewServer(&gcfg)
}
Esempio n. 23
0
func LoadConfig(file string) (cfg Config, err error) {
	f, err := os.Open(file)
	if err != nil {
		return cfg, err
	}
	defer f.Close()

	buf, err := ioutil.ReadAll(f)
	if err != nil {
		return cfg, err
	}

	err = toml.Unmarshal(buf, &cfg)

	return cfg, err
}
Esempio n. 24
0
func GetConfig() *appConfig {
	f, err := os.Open("config.toml")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}
	var config appConfig
	if err := toml.Unmarshal(buf, &config); err != nil {
		panic(err)
	}
	return &config
}
Esempio n. 25
0
func configFromFile() config {
	f, err := os.Open("config.toml")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatal(err)
	}
	var cfg config
	if err = toml.Unmarshal(buf, &cfg); err != nil {
		log.Fatal(err)
	}
	return cfg
}
Esempio n. 26
0
File: main.go Progetto: iqstack/toml
func main() {
	f, err := os.Open("example.toml")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}
	var config tomlConfig
	if err := toml.Unmarshal(buf, &config); err != nil {
		panic(err)
	}
	// then to use the unmarshaled config...
}
Esempio n. 27
0
func readConf(path string) (*Config, error) {
	fd, err := os.Open(*file)
	if err != nil {
		return nil, err
	}
	defer fd.Close()
	buf, err := ioutil.ReadAll(fd)
	if err != nil {
		return nil, err
	}
	config := new(Config)
	if err := toml.Unmarshal(buf, config); err != nil {
		return nil, err
	}
	return config, nil
}
Esempio n. 28
0
func TestUnmarshal_WithUnmarshaler(t *testing.T) {
	type testStruct struct {
		Title         UnmarshalString
		MaxConn       UnmarshalString
		Ports         UnmarshalString
		Servers       UnmarshalString
		Table         UnmarshalString
		Arraytable    UnmarshalString
		ArrayOfStruct []testUnmarshalStruct
	}
	data := `title = "testtitle"
max_conn = 777
ports = [8080, 8081, 8082]
servers = [1, 2, 3]
[table]
name = "alice"
[[arraytable]]
name = "alice"
[[arraytable]]
name = "bob"
[[array_of_struct]]
title = "Alice's Adventures in Wonderland"
author = "Lewis Carroll"
`
	var v testStruct
	if err := toml.Unmarshal([]byte(data), &v); err != nil {
		t.Fatal(err)
	}
	actual := v
	expect := testStruct{
		Title:      `UnmarshalString: "testtitle"`,
		MaxConn:    `UnmarshalString: 777`,
		Ports:      `UnmarshalString: [8080, 8081, 8082]`,
		Servers:    `UnmarshalString: [1, 2, 3]`,
		Table:      "UnmarshalString: [table]\nname = \"alice\"",
		Arraytable: "UnmarshalString: [[arraytable]]\nname = \"alice\"\n[[arraytable]]\nname = \"bob\"",
		ArrayOfStruct: []testUnmarshalStruct{
			{
				Title:  "Unmarshaled: [[array_of_struct]]\ntitle = \"Alice's Adventures in Wonderland\"\nauthor = \"Lewis Carroll\"",
				Author: "",
			},
		},
	}
	if !reflect.DeepEqual(actual, expect) {
		t.Errorf(`toml.Unmarshal(data, &v); v => %#v; want %#v`, actual, expect)
	}
}
Esempio n. 29
0
// NewSlack returns a initialized Slack struct by config.toml
func NewSlack() (*Slack, error) {
	var s = &Slack{
		Payload: Payload{},
		URL:     "",
	}

	buf, err := s.readConfigToml()
	if err != nil {
		return nil, err
	}

	if err := toml.Unmarshal(buf, s); err != nil {
		return nil, err
	}

	return s, nil
}
Esempio n. 30
0
func LoadConfig(filename string, config interface{}) (err error) {

	f, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	buf, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}
	if err := toml.Unmarshal(buf, config); err != nil {
		panic(err)
	}

	return nil
}