示例#1
0
func ParseGlobal(rootDir string) (*ConfigFile, error) {
	path := rootDir + "/inspeqtor.conf"
	exists, err := util.FileExists(path)
	if err != nil {
		return nil, err
	}

	if exists {
		util.Debug("Parsing " + path)
		data, err := ioutil.ReadFile(path)
		if err != nil {
			return nil, err
		}

		s := lexer.NewLexer([]byte(data))
		p := parser.NewParser()
		obj, err := p.Parse(s)
		if err != nil {
			return nil, err
		}
		ast := obj.(ast.Config)

		config := ConfigFile{Defaults, map[string]*AlertRoute{}}

		config.Variables = ast.Variables
		if val, has := ast.Variables["log_level"]; has {
			util.SetLogLevel(val)
		}
		parseValue(ast, &config.CycleTime, "cycle_time", 15)
		parseValue(ast, &config.DeployLength, "deploy_length", 300)
		parseValue(ast, &config.ExposePort, "expose_port", 4677)

		for _, v := range ast.Routes {
			ar, err := ValidateChannel(v.Name, v.Channel, v.Config)
			if err != nil {
				return nil, err
			}
			if _, ok := config.AlertRoutes[v.Name]; ok {
				return nil, fmt.Errorf("Duplicate alert config for '%s'", v.Name)
			}
			config.AlertRoutes[v.Name] = ar
		}
		return &config, nil
	}

	util.Info("No configuration file found at " + rootDir + "/inspector.conf")
	return &ConfigFile{Defaults, nil}, nil
}
示例#2
0
func TestBasicParsing(t *testing.T) {
	data, err := ioutil.ReadFile("fixtures/inspeqtor.conf")
	assert.Nil(t, err)

	s := lexer.NewLexer([]byte(data))
	p := parser.NewParser()
	obj, err := p.Parse(s)
	assert.Nil(t, err)
	assert.NotNil(t, obj)

	config := obj.(ast.Config)
	assert.Equal(t, "15", config.Variables["cycle_time"])
	assert.Equal(t, 3, len(config.Routes))
	assert.Equal(t, "b!l$a%rgh^fazz\"", config.Routes["analytics"].Config["password"])
	assert.Equal(t, "smtp.example.com", config.Routes["analytics"].Config["smtp_server"])
	log.Printf("%+v", config)
}