Exemplo n.º 1
1
func Load() Config {
	file, err := os.Open(Path())

	var wConfig Config

	if err != nil {
		log.Printf("Config file not found. Creating new...")
		err = CreateNewConfigFile()

		if err != nil {
			log.Fatalf("Unable to create new config file. Error: %v", err)
			os.Exit(1)
		}

		file, err = os.Open(Path())
		if err != nil {
			log.Fatalf("Unable to read new config file. Error: %v", err)
			os.Exit(1)
		}
	}

	defer file.Close()

	toml.DecodeReader(file, &wConfig)

	return wConfig
}
Exemplo n.º 2
0
func (o *Server) LoadRingConfig(config interface{}) (err error) {
	o.Lock()
	defer o.Unlock()
	e := NewEnvGetter(fmt.Sprintf("OORT_%s", strings.ToUpper(o.serviceName)), "_")
	localConfig := e.Get("LOCAL_CONFIG")
	if localConfig != "" {
		_, err = toml.DecodeFile(localConfig, config)
		if err != nil {
			return err
		}
	}
	log.Println("Using ring version:", o.ring.Version())
	b := bytes.NewReader(o.ring.Config())
	if b.Len() > 0 {
		_, err = toml.DecodeReader(b, config)
		if err != nil {
			return err
		}
	}
	// Now overlay per node config on top
	n := o.ring.LocalNode()
	if n == nil {
		panic("n is nil")
	}
	b = bytes.NewReader(o.ring.LocalNode().Config())
	if b.Len() > 0 {
		_, err = toml.DecodeReader(b, config)
		if err != nil {
			return err
		}
	}
	log.Printf("Local Node config is: \n%s", o.ring.LocalNode().Config())
	log.Printf("Ring config is: \n%s", o.ring.Config())
	return nil
}
Exemplo n.º 3
0
// loadReader loads config from reader.
func loadReader(reader io.Reader) (*Config, error) {
	c := new(Config)
	if _, err := toml.DecodeReader(reader, c); err != nil {
		return nil, err
	}
	return c, nil
}
Exemplo n.º 4
0
func readConf() *conf.Conf {
	conf := &conf.Conf{
		Freq: 15,
	}
	loc := *flagConf
	if *flagConf == "" {
		p, err := exePath()
		if err != nil {
			slog.Error(err)
			return conf
		}
		dir := filepath.Dir(p)
		loc = filepath.Join(dir, "scollector.toml")
	}
	f, err := os.Open(loc)
	if err != nil {
		if *flagConf != "" {
			slog.Fatal(err)
		}
		if *flagDebug {
			slog.Error(err)
		}
	} else {
		defer f.Close()
		md, err := toml.DecodeReader(f, conf)
		if err != nil {
			slog.Fatal(err)
		}
		if u := md.Undecoded(); len(u) > 0 {
			slog.Fatalf("extra keys in %s: %v", loc, u)
		}
	}
	return conf
}
Exemplo n.º 5
0
func Read(r io.Reader) (layout *Layout, err error) {
	_, err = toml.DecodeReader(r, &layout)
	if err != nil {
		return nil, err
	}
	return layout, nil
}
Exemplo n.º 6
0
func handlePostConfig(w http.ResponseWriter, r *http.Request) {
	revision, err := strconv.ParseInt(r.Header.Get("X-RobustIRC-Config-Revision"), 0, 64)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	var unused config.Network
	var body bytes.Buffer
	if _, err := toml.DecodeReader(io.TeeReader(r.Body, &body), &unused); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	if node.State() != raft.Leader {
		maybeProxyToLeader(w, r, nopCloser{&body})
		return
	}

	if err := applyConfig(int(revision), body.String()); err != nil {
		if err == raft.ErrNotLeader {
			maybeProxyToLeader(w, r, nopCloser{&body})
			return
		}

		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
}
Exemplo n.º 7
0
func loadConfDir() error {
	if len(C.Confdir) > 0 {
		return filepath.Walk(C.Confdir, func(path string, f os.FileInfo, err error) error {
			if path == C.Confdir {
				// ignore root
				return nil
			}

			if strings.HasSuffix(path, ".toml") {
				r, e := os.Open(path)
				if e != nil {
					return e
				}
				var f File
				if _, e := toml.DecodeReader(r, &f); e != nil {
					r.Close()
					return fmt.Errorf("TOML(%s): %s", path, e)
				}
				r.Close()
				C.Files = append(C.Files, f)
			}

			return nil
		})
	}
	return nil
}
Exemplo n.º 8
0
Arquivo: tq.go Projeto: zoncoen/tq
func main() {
	flag.Parse()

	if opt.help {
		flag.Usage()
		os.Exit(0)
	}

	var t interface{}
	_, err := toml.DecodeReader(os.Stdin, &t)
	if err != nil {
		log.Fatalf("parse error: %s", err)
	}

	var filter ast.Filter
	if flag.NArg() > 0 {
		str := flag.Arg(0)
		r := strings.NewReader(str)
		filter = parser.Parse(r)
		t, err = transformer.Transform(t, filter)
		if err != nil {
			log.Fatalf("%s", err)
		}
	}

	err = write(os.Stdout, t)
	if err != nil {
		log.Fatalf("%s", err)
	}
}
Exemplo n.º 9
0
func Init(f string) error {
	r, e := os.Open(f)
	if e != nil {
		return e
	}
	defer r.Close()
	if _, e := toml.DecodeReader(r, &C); e != nil {
		return fmt.Errorf("TOML: %s", e)
	}
	if e := loadConfDir(); e != nil {
		return e
	}

	if e := prepareRegexp(); e != nil {
		return e
	}

	Hostname, e = os.Hostname()
	if e != nil {
		return e
	}

	DB, e = bolt.Open(C.Db, 0600, &bolt.Options{Timeout: 1 * time.Second})
	if e != nil {
		return e
	}
	return nil
}
Exemplo n.º 10
0
func TestOpen(t *testing.T) {
	cfgfile := os.Getenv("DBCONFIG")
	if cfgfile == "" {
		cfgfile = "config.toml"
	}
	cfg := new(Config)
	if f, err := os.Open(cfgfile); err != nil {
		t.Fatal(err, "(did you set the DBCONFIG env variable?)")
	} else {
		if _, err := toml.DecodeReader(f, cfg); err != nil {
			t.Fatal(err)
		}
	}

	d := NewDriver("ClusterSql", mysql.MySQLDriver{})

	for _, ncfg := range cfg.Nodes {
		if ncfg.Password != "" {
			d.AddNode(ncfg.Name, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", ncfg.UserName, ncfg.Password, ncfg.HostName, ncfg.Port, ncfg.DBName))
		} else {
			d.AddNode(ncfg.Name, fmt.Sprintf("%s@tcp(%s:%d)/%s", ncfg.UserName, ncfg.HostName, ncfg.Port, ncfg.DBName))
		}
	}

	sql.Register("cluster", d)
	var err error
	db, err = sql.Open("cluster", "galera")
	if err != nil {
		t.Error(err)
	}
}
Exemplo n.º 11
0
func Read(r io.Reader) (footprints map[string]*Footprint, err error) {
	_, err = toml.DecodeReader(r, &footprints)
	if err != nil {
		return nil, err
	}
	return footprints, nil
}
Exemplo n.º 12
0
// OpenRepository opens a Repository and creates a new Repository instance.
func OpenRepository(basePath string) (*Repository, error) {
	rootPath, err := filepath.Abs(basePath)
	if err != nil {
		return nil, err
	}

	for {
		configDirPath := filepath.Join(rootPath, DirNameConfig)

		fi, err := os.Stat(configDirPath)
		if err != nil {
			if err.(*os.PathError).Err != syscall.ENOENT {
				return nil, err
			}
		} else {
			if fi.IsDir() {
				break
			} else {
				return nil, ErrInvalidRepository
			}
		}

		parentPath := filepath.Clean(filepath.Join(rootPath, ".."))
		if rootPath == parentPath {
			return nil, ErrNotRepository
		}
		rootPath = parentPath
	}

	configFilePath := filepath.Join(rootPath, DirNameConfig, FileNameConfig)

	fi, err := os.Stat(configFilePath)
	if err != nil {
		return nil, err
	}
	if fi.IsDir() {
		return nil, ErrInvalidRepository
	}

	configFile, err := os.Open(configFilePath)
	if err != nil {
		return nil, err
	}
	defer configFile.Close()

	c := DefaultRepositoryConfig()
	if _, err = toml.DecodeReader(configFile, c); err != nil {
		return nil, err
	}

	c.Normalize(rootPath)

	r, err := NewRepository(c)
	if err != nil {
		return nil, err
	}

	return r, nil
}
Exemplo n.º 13
0
func ParseConfig(reader io.Reader) (*DParConfig, error) {
	config := defaultConfiguration()
	if _, err := toml.DecodeReader(reader, config); err == nil {
		return config, nil
	} else {
		return config, err
	}
}
Exemplo n.º 14
0
func parseAsToml(r io.Reader) (conf, error) {
	var conf conf
	_, err := toml.DecodeReader(r, &conf)
	if err != nil {
		return nil, err
	}
	return conf, nil
}
Exemplo n.º 15
0
func UpdateConfigFromFile(conf *Config, f io.Reader) error {
	_, err := toml.DecodeReader(f, conf)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 16
0
Arquivo: config.go Projeto: unikk/cuto
func loadReader(reader io.Reader) (*ServantConfig, error) {
	sc := new(ServantConfig)
	if _, err := toml.DecodeReader(reader, sc); err != nil {
		return nil, err
	}

	sc.replaceCutoroot()

	return sc, nil
}
Exemplo n.º 17
0
func (c *ConfigWrapper) Load(reader io.Reader) error {
	// We completely replace config object here instead of in-place loading
	// This is needed for safe runtime config reload
	var conf Config
	if _, err := toml.DecodeReader(reader, &conf); err != nil {
		return err
	}
	c.Config = &conf
	return nil
}
Exemplo n.º 18
0
func decode(r io.Reader) (configuration, error) {
	cfg := configuration{}
	res, err := toml.DecodeReader(r, &cfg)
	if err != nil {
		return cfg, fmt.Errorf("can't parse gender config: %s", err)
	}
	if und := res.Undecoded(); len(und) > 0 {
		log.Printf("WARNING: Unknown values in gender config file: %v", und)
	}

	return cfg, nil
}
Exemplo n.º 19
0
func loadReader(reader io.Reader) error {
	c := new(config)

	if _, err := toml.DecodeReader(reader, c); err != nil {
		return err
	}

	Aws = &c.Aws
	Log = &c.Log

	return nil
}
Exemplo n.º 20
0
func ParseConf() (*Conf, error) {
	conf := &Conf{}
	f, err := os.Open(*configFile)
	if err != nil {
		return nil, err
	}
	meta, err := toml.DecodeReader(f, conf)
	if err != nil {
		return nil, fmt.Errorf("Error decoding %s: %s", *configFile, err)
	}

	for _, field := range []string{"graphite_addr", "port", "debug_port", "flush_interval_ms", "namespace"} {
		if !meta.IsDefined(field) {
			return nil, fmt.Errorf("field %s is required", field)
		}
	}
	if conf.FlushIntervalMS <= 0 {
		return nil, errors.New("flush_interval_ms must be positive")
	}

	if meta.IsDefined("forwarding_addr") {
		conf.forwardingEnabled = true
	}

	if meta.IsDefined("forwarder_listen_addr") {
		conf.forwarderEnabled = true
		if !meta.IsDefined("forwarded_namespace") {
			return nil, errors.New("forwarded_namespace is required if gost is configured as a forwarder")
		}
	}
	conf.OSStats = nil
	conf.Scripts = nil
	/**
		if err := validateOSStatsConf(conf.OSStats, meta); err != nil {
			return nil, err
		}
		if !meta.IsDefined("os_stats", "check_interval_ms") {
			conf.OSStats.CheckIntervalMS = conf.FlushIntervalMS
		}
		if err := validateScriptsConf(conf.Scripts, meta); err != nil {
			return nil, err
		}
	**/
	if conf.Namespace == "" {
		conf.Namespace = "default"
	}
	conf.ForwardedNamespace, err = filterNamespace(conf.ForwardedNamespace)
	if err != nil {
		return nil, err
	}
	return conf, nil
}
Exemplo n.º 21
0
func handlePostConfig(w http.ResponseWriter, r *http.Request) {
	revision, err := strconv.ParseInt(r.Header.Get("X-RobustIRC-Config-Revision"), 0, 64)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	configMu.Lock()

	if got, want := int(revision), netConfig.Revision; got != want {
		http.Error(w, fmt.Sprintf("Revision mismatch (got %d, want %d). Try again.", got, want), http.StatusBadRequest)
		return
	}

	var unused config.Network
	var body bytes.Buffer
	if _, err := toml.DecodeReader(io.TeeReader(r.Body, &body), &unused); err != nil {
		configMu.Unlock()
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	if node.State() != raft.Leader {
		maybeProxyToLeader(w, r, nopCloser{&body})
		return
	}

	applyMu.Lock()
	msg := ircServer.NewRobustMessage(types.RobustConfig, types.RobustId{}, body.String())
	msg.Revision = int(revision) + 1
	msgbytes, err := json.Marshal(msg)
	if err != nil {
		applyMu.Unlock()
		configMu.Unlock()
		http.Error(w, fmt.Sprintf("Could not store message, cannot encode it as JSON: %v", err),
			http.StatusBadRequest)
		return
	}

	f := node.Apply(msgbytes, 10*time.Second)
	applyMu.Unlock()
	err = f.Error()
	configMu.Unlock()
	if err != nil {
		if err == raft.ErrNotLeader {
			maybeProxyToLeader(w, r, nopCloser{&body})
			return
		}
		http.Error(w, fmt.Sprintf("Apply(): %v", err), http.StatusInternalServerError)
		return
	}
}
Exemplo n.º 22
0
func NewConfig(filename string) (*Config, error) {
	cfg := &Config{}
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	if _, err := toml.DecodeReader(bufio.NewReader(file), &cfg); err != nil {
		return nil, err
	}

	return cfg, nil
}
Exemplo n.º 23
0
func (c *Config) Read(filename string) error {
	file, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	if _, err = toml.DecodeReader(file, c); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 24
0
// decodeEmotes parses the data from the reader into a list of emotes.
func decodeEmotes(r io.Reader) ([]emote, error) {
	cfgs := emoteConfigs{}

	res, err := toml.DecodeReader(r, &cfgs)
	if err != nil {
		return nil, fmt.Errorf("Error parsing emote config file: %s", err)
	}

	if und := res.Undecoded(); len(und) > 0 {
		log.Printf("WARNING: Unknown values in emote config file: %v", und)
	}
	return cfgs.Emote, nil
}
Exemplo n.º 25
0
func loadReader(r io.Reader) error {
	c := new(Config)
	if _, err := toml.DecodeReader(r, c); err != nil {
		return err
	}

	replaceCutoroot(c)

	Server = c.Server
	Log = c.Log
	Jobnet = c.Jobnet

	return nil
}
Exemplo n.º 26
0
// GetStellarToml returns stellar.toml file for a given domain
func (r *Resolver) GetStellarToml(domain string) (stellarToml StellarToml, err error) {
	var resp *http.Response
	resp, err = http.Get("https://www." + domain + "/.well-known/stellar.toml")
	if err != nil {
		return
	}
	if resp.StatusCode != 200 {
		err = errors.New("stellar.toml response status code indicates error")
		return
	}

	_, err = toml.DecodeReader(resp.Body, &stellarToml)
	return
}
Exemplo n.º 27
0
// Parse converts the config file into a readable map
func (c Config) Parse() (*ConfigMap, error) {
	config := &ConfigMap{}
	file, err := c.Open()
	if err != nil {
		return nil, err
	}

	_, err = toml.DecodeReader(file, config)
	if err != nil {
		return nil, err
	}

	return config, nil
}
Exemplo n.º 28
0
func LoadDBConf(r io.Reader) (conf *DBConf, err error) {
	defer func() {
		if perr := recover(); perr != nil {
			err = perr.(error)
		}
	}()
	conf = DefaultDBConf.DeepCopy()

	if _, err := toml.DecodeReader(r, &conf); err != nil {
		return nil, err
	}

	return conf, nil
}
Exemplo n.º 29
0
Arquivo: config.go Projeto: unikk/cuto
func loadReader(reader io.Reader) error {
	c := new(config)
	c.Job.AttemptLimit = 1
	if _, err := toml.DecodeReader(reader, c); err != nil {
		return err
	}

	replaceCutoroot(c)

	Dir = &c.Dir
	Job = &c.Job
	DB = &c.DB
	Log = &c.Log
	return nil
}
Exemplo n.º 30
0
func main() {
	if flag.NArg() != 0 {
		flag.Usage()
	}

	var tmp interface{}
	if _, err := toml.DecodeReader(os.Stdin, &tmp); err != nil {
		log.Fatalf("Error decoding TOML: %s", err)
	}

	typedTmp := translate(tmp)
	if err := json.NewEncoder(os.Stdout).Encode(typedTmp); err != nil {
		log.Fatalf("Error encoding JSON: %s", err)
	}
}