コード例 #1
0
ファイル: hook.go プロジェクト: cshapeshifter/wingo
// readSection loads a particular section from the configuration file into
// the hook groups. One section may result in the same hook being added to
// multiple groups.
func readSection(cdata *wini.Data, section string) error {
	// First lets roll up the match conditions.
	match := cdata.GetKey(section, "match")
	if match == nil {
		return fmt.Errorf("Could not find 'match' in the '%s' hook.", section)
	}

	satisfies := make([]string, len(match.Strings()))
	copy(satisfies, match.Strings())

	// Check each satisfies command to make sure it's a valid Gribble command.
	if cmd, err := checkCommands(satisfies); err != nil {
		return fmt.Errorf("The match command '%s' in the '%s' hook could "+
			"not be parsed: %s", cmd, section, err)
	}

	// Now try to find whether it's a conjunction or not.
	conjunction := true
	conjunctionKey := cdata.GetKey(section, "conjunction")
	if conjunctionKey != nil {
		if vals, err := conjunctionKey.Bools(); err != nil {
			logger.Warning.Println(err)
		} else {
			conjunction = vals[0]
		}
	}

	// Now traverse all of the keys in the section. We'll skip "match" since
	// we've already grabbed the data. Any other key should correspond to
	// a hook group name.
	addedOne := false
	for _, key := range cdata.Keys(section) {
		groupName := Type(key.Name())
		if groupName == "match" || groupName == "conjunction" {
			continue
		}
		if _, ok := groups[groupName]; !ok {
			return fmt.Errorf("Unrecognized hook group '%s' in the '%s' hook.",
				groupName, section)
		}

		consequences := make([]string, len(key.Strings()))
		copy(consequences, key.Strings())

		// Check each consequent command to make sure it's valid.
		if cmd, err := checkCommands(consequences); err != nil {
			return fmt.Errorf("The '%s' command '%s' in the '%s' hook could "+
				"not be parsed: %s", groupName, cmd, section, err)
		}
		hook := hook{
			name:         section,
			satisfies:    satisfies,
			conjunction:  conjunction,
			consequences: consequences,
		}
		groups[groupName] = append(groups[groupName], hook)
		addedOne = true
	}
	if !addedOne {
		return fmt.Errorf(
			"No hook groups were detected in the '%s' hook.", section)
	}
	return nil
}