Ejemplo n.º 1
0
// Loads rewrite rules in a TOML file, specified by the filename argument.
// Returns an array of RewriteRules, or error.
func LoadRewriteRules(filename string) (RewriteRuleArray, error) {
	// load the config file
	tree, err := toml.LoadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("%s: %s", filename, err)
	}

	// curry the filename and position into an error format function
	pos := toml.Position{}
	errorf := func(format string, values ...interface{}) (RewriteRuleArray, error) {
		curriedFormat := filename + " " + pos.String() + ": " + format
		return nil, fmt.Errorf(curriedFormat, values...)
	}

	rules := RewriteRuleArray{}

	if rewriteTree := tree.Get("rewrite"); rewriteTree != nil {
		for _, ruleTree := range rewriteTree.([]*toml.TomlTree) {
			rule := NewRewriteRule()
			matchTree, ok := ruleTree.Get("match").(*toml.TomlTree)
			if !ok {
				pos = ruleTree.GetPosition("")
				return errorf("Expected 'match' subtree for rewrite rule")
			}
			replaceTree, ok := ruleTree.Get("replace").(*toml.TomlTree)
			if !ok {
				pos = ruleTree.GetPosition("")
				return errorf("Expected 'replace' subtree for rewrite rule")
			}
			for _, key := range matchTree.Keys() {
				matchString, ok := matchTree.Get(key).(string)
				if !ok {
					pos = matchTree.GetPosition(key)
					return errorf("Match expression must be a string value")
				}
				matchRegex, err := regexp.Compile(matchString)
				if err != nil {
					pos = matchTree.GetPosition(key)
					return errorf("Error compiling match expression: %s", err)
				}
				rule.Matches[key] = matchRegex
			}
			for _, key := range replaceTree.Keys() {
				replaceString, ok := replaceTree.Get(key).(string)
				if !ok {
					pos = replaceTree.GetPosition(key)
					return errorf("Replace expression must be a string value")
				}
				replaceTempl, err := RewriteTemplate(replaceString)
				if err != nil {
					pos = replaceTree.GetPosition(key)
					return errorf("Error compiling replace expression: %s", err)
				}
				rule.Replacements[key] = replaceTempl
			}
			rules = append(rules, rule)
		}
	}
	return rules, nil
}
Ejemplo n.º 2
0
func NewConfig(dir string) *Config {
	config := &Config{Path: filepath.Join(dir, "gopack.config")}

	t, err := toml.LoadFile(config.Path)
	if err != nil {
		fail(err)
	}

	if deps := t.Get("deps"); deps != nil {
		config.DepsTree = deps.(*toml.TomlTree)
	}

	if deps := t.Get("dev-deps"); deps != nil {
		config.DevDepsTree = deps.(*toml.TomlTree)
	}

	if repo := t.Get("repo"); repo != nil {
		config.Repository = repo.(string)
	}

	return config
}
Ejemplo n.º 3
0
func loadDependencies(filename string) ([]*Dependency, error) {
	tree, err := toml.LoadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("%s %s", filename, err)
	}

	items := tree.Get("dependencies").([]*toml.TomlTree)
	if items == nil {
		return nil, fmt.Errorf("No dependencies to process")
	}

	deplist := make([]*Dependency, 0)
	for idx, item := range items {
		if dep, err := NewDependencyFromToml(item); err != nil {
			return nil, fmt.Errorf("In dependency #%d: %v", idx, err)
		} else {
			deplist = append(deplist, dep)
		}
	}

	return deplist, nil
}